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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何为AWS Lambda创建和压缩docker容器相关的知识,希望对你有一定的参考价值。

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

任何帮助将不胜感激。

答案

说明不明确,但我怀疑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权限才能使应用程序正常工作。

以上是关于如何为AWS Lambda创建和压缩docker容器的主要内容,如果未能解决你的问题,请参考以下文章

AWS CloudFormation:如何为 Lambda 代码指定来自另一个 AWS 账户的存储桶?

如何为 Alexa Skills Kit 和 API.AI 使用单个 AWS Lambda?

AWS Lambda:如何为具有 VPC 访问权限的 lambda 函数设置 NAT 网关

如何为 pymysql lambda 启用 AWS XRAY 跟踪

如何为 AWS API Gateway Custom Authorizer 提供 Lambda 权限?

如何为 AWS API Gateway 自定义授权方配置 CORS?