【问题标题】:Upload local file on parameter ZipFile AWS::Lambda::Function在参数 ZipFile AWS::Lambda::Function 上上传本地文件
【发布时间】:2019-07-19 11:37:36
【问题描述】:

我有一个带有 AWS::Lambda::Function 资源的 CloudFormation 模板,我正在尝试将本地 zip 文件作为代码上传,但它没有上传。 Lambda 函数是在没有代码文件的情况下创建的。

    Resources:
  mastertestingLambdaDataDigestor:
    Properties:
      Code:
        ZipFile: fileb:///home/dariobenitez/Proyectos/dataflow/templates/lambda_template.zip
      FunctionName: mastertesting_Kinesis2DynamoDB_Datapipeline
      Handler: handler.kinesis_to_dynamodb
      Role: SOMEROLE
      Runtime: python3.6
    Type: AWS::Lambda::Function

当我尝试使用 CLI 部署相同的功能时,zip 文件路径参数有效。有什么想法吗?

非常感谢!

【问题讨论】:

    标签: aws-lambda amazon-cloudformation


    【解决方案1】:

    您不能在此处指定文件路径。您必须输入功能代码本身。它被限制为 4096 字节。如果你的代码比较大,需要先上传到S3,然后使用S3BucketS3Key

    例子:

    mastertestingLambdaDataDigestor:
      Properties:
        Code:
          ZipFile: >
            def handler(event, context):
              pass
        FunctionName: mastertesting_Kinesis2DynamoDB_Datapipeline
        Handler: handler.kinesis_to_dynamodb
        Role: SOMEROLE
        Runtime: python3.6
      Type: AWS::Lambda::Function
    

    另一个选项是使用aws cloudformation package。它将为您上传 zip 文件并将您的模板转换为具有正确路径的模板。为此,您必须将 zip 文件路径直接放在 Code 中。例如:

    Resources:
      mastertestingLambdaDataDigestor:
        Properties:
          Code: /home/dariobenitez/Proyectos/dataflow/templates/lambda_template.zip
          FunctionName: mastertesting_Kinesis2DynamoDB_Datapipeline
          Handler: handler.kinesis_to_dynamodb
          Role: SOMEROLE
          Runtime: python3.6
        Type: AWS::Lambda::Function
    

    然后运行:

    aws cloudformation package --template-file my-template.yaml --s3-bucket my-bucket
    

    它应该输出如下内容:

    Resources:
      mastertestingLambdaDataDigestor:
        Properties:
          Code:
            S3Bucket: my-bucket
            S3Key: fjklsdu903490f349034g
          FunctionName: mastertesting_Kinesis2DynamoDB_Datapipeline
          Handler: handler.kinesis_to_dynamodb
          Role: SOMEROLE
          Runtime: python3.6
        Type: AWS::Lambda::Function
    

    然后您应该使用此模板来部署您的堆栈。

    【讨论】:

      【解决方案2】:

      以下模式适合我:

      1. 在 lambda 定义中添加一些虚拟代码

        Properties: Code: ZipFile: > def handler(event, context): pass

      2. 使用 Cloudformation 进行打包

        $ aws cloudformation package --template-file\ /src/apigateway/cf.yml --s3-bucket <SOME_BUCKET>\ --output-template-file packaged-template.json

      3. 更新 lambda 代码

      aws lambda update-function-code \ --function-name my-function \ --zip-file fileb://my-function.zip

      1. 因为 Cloudformation 从不关心 lambda 函数中的代码。因此,即使我们重新部署 Cloudformation,您的 lambda 代码仍将与您使用 CLI 更新的代码保持一致。

      如果我们只想上传 lambda 代码并使用上传的引用更新我们的 Cloudformation,我们可以使用 Cloudformation package 命令。

      1. 在 Cloudformation 中加入类似的内容

        Properties: Code:

      2. 然后运行

        $ aws cloudformation package --template-file\ /src/apigateway/cf.yml --s3-bucket <SOME_BUCKET>\ --output-template-file packaged-template.json

      3. 更新 Cloudformaion

        $ grep S3 packaged-template.json -B 2 Properties: Code: S3Bucket: <SOME_BUCKET> S3Key: 77807d1ae2c2e590e0b928ac579c3aee

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-08-26
        • 1970-01-01
        • 2019-02-11
        • 1970-01-01
        • 2022-10-14
        • 2020-06-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多