如何为AWS Lambda创建和压缩docker容器

时间:2018-03-01 09:18:10

标签: amazon-web-services docker amazon-s3 aws-lambda

我正在尝试创建然后压缩Docker容器以上传到S3以由AWS Lambda函数运行。我试图处理一篇文章,但说明很稀疏(https://github.com/abhisuri97/auto-alt-text-lambda-api)。

我已经安装了Docker和Amazon Linux映像,但我不知道如何创建包含github repo的Docker容器,然后将其压缩以便Lambda可以访问它。

这是我试图从其他教程中拼凑出来的东西:

git clone https://github.com/abhisuri97/auto-alt-text-lambda-api.git

cd auto-alt-text-lambda-api

docker run -v -it amazonlinux:2017.12

zip -r -9 -q ~/main.zip

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

说明不明确但我怀疑Docker的引用仅用于测试。您不需要Docker来运行AWS Lambda函数。您需要AWS API Gateway API才能通过HTTPS执行Lambda功能。

我建议使用AWS无服务器应用程序模式(https://docs.aws.amazon.com/lambda/latest/dg/serverless_app.html)从CloudFormation堆栈开始。

为zip文件创建一个S3存储桶,并创建类似于以下内容的CloudFormation模板:

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31

Resources:
  LambdaFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: application.predict
      Runtime: python2.7
      Events:
        HttpGet:
          Type: Api
          Properties:
            Path: 'auto-alt-text-api'
            Method: get

将Lambda函数打包为:

aws cloudformation package --template-file template.yaml --output-template-file template-out.yaml --s3-bucket <your-bucket> --s3-prefix <your-prefix>

然后使用:

进行部署

aws cloudformation deploy --template-file template-out.yaml --stack-name auto-alt-text-lambda-api-stack --capabilities CAPABILITY_IAM

您可能必须向模板添加IAM角色和Lambda权限才能使应用程序正常工作。