如何防止多次调用 AWS Lambda 函数容器

How to Prevent Invoking AWS Lambda Function Container Multiple Times

[更新 1:] 当我将超时增加到 1 分钟时,CloudWatch 在脚本两次成功 运行 之后在中间显示以下内容,然后是一次:

REPORT RequestId: xxx-xxx   Duration: 7670.23 ms    Billed Duration: 7671 ms    Memory Size: 128 MB Max Memory Used: 36 MB  
RequestId: xxx-xxx Error: Runtime exited without providing a reason
Runtime.ExitError

原版post

我有一个 Lambda 函数 运行 通过自定义容器映像。其中单个 Python 脚本的要点如下:

# imports

def lambda_handler(event, context):
    # read a JSON file from S3, check an FTP server for some info, and prepare a JSON response
    # if file not found or some other error, handle exception, prepare appropriate JSON response
    # return JSON response

# Here be helper functions

if __name__ == '__main__':
    lambda_helper(None, None)
    # also tried response = lambda_helper(None, None)

这是阶跃函数中的第一个状态,它将由 CloudWatch Events 定期触发,因此不需要任何输入。

它是从容器中调用的

CMD ["python", "script.py"]

当我从控制台测试此功能时,我在 CloudWatch 中看到了所有预期的日志消息,包括最后一条指示成功执行的日志消息,但是此过程本身重复了几次,总体上被视为失败 (顶部的红色横幅)。

它会在 3 秒后超时,因为这是默认限制,但不会在脚本 运行 成功几次之前超时。没有内存问题(使用了 20-30 MB,超出 128 MB)或其他错误。

在早期版本中,对 lambda_handler 的调用包含在 sys.exit() 中,但在阅读了一些关于它干扰 Lambda 处理函数的方式的帖子后,我删除了它。当时唯一的区别是我可以在 CloudWatch 中看到 JSON 响应,而现在我只能看到日志消息。

我已经阅读了大量的线程和文档,但我仍然无法解决这个问题。任何帮助将不胜感激。

回答我自己的问题。

显然,如果您希望使用不同于 AWS 提供的基础映像,您必须在“AWS 运行时接口客户端”中执行您的代码。我最初使用 Python-Alpine 图像并尝试按照 this blog post, but the build process ran into errors for reasons that I haven't looked into. I then created another image from Debian Buster based on instructions from here 末尾的步骤进行操作,并且在对 Dockerfile 进行一些“清理”以适合我的应用程序之后,它成功了。