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

问题描述 投票:2回答:1

我正在尝试创建然后压缩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

任何帮助将不胜感激。

amazon-web-services docker amazon-s3 aws-lambda
1个回答
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权限才能使应用程序正常工作。

© www.soinside.com 2019 - 2024. All rights reserved.