简体   繁体   English

如何使用 CloudFormation 模板更新 AWS Lambda function

[英]How do I update AWS Lambda function using CloudFormation template

I want to deploy and update my lambda function code using CloudFormation Template.我想使用 CloudFormation 模板部署和更新我的 lambda function 代码。

My deployment workflow are:我的部署工作流程是:

  1. Compress my lambda function code into a zip file called Lambda将我的 lambda function 代码压缩到名为Lambda的 zip 文件中
  2. Enabling s3 versioning of the s3 bucket called LambdaS3启用名为LambdaS3的 s3 存储桶的 s3 版本控制
  3. Upload the zip file into s3 bucket called LambdaS3将 zip 文件上传到名为LambdaS3的 s3 存储桶中
  4. Upload the CloudFormation template CFtemplate as below into the s3bucket LambdaS3将如下 CloudFormation 模板CFtemplate上传到 s3bucket LambdaS3
  5. Create the CloudFormation stack by entering "LambdaS3" as parameter LambdaS3 , "Lambda" as parameter Lambdafilename and the version of the zip file as the parameter LambdafileVersion通过输入“LambdaS3”作为参数LambdaS3 、“Lambda”作为参数Lambdafilename并将 zip 文件的版本作为参数LambdafileVersion来创建 CloudFormation 堆栈

My lambda code update workflow are:我的 lambda 代码更新工作流程是:

  1. Compress my updated lambda function code into a zip file called Lambda将我更新的 lambda function 代码压缩到名为 zip 的文件Lambda
  2. Upload the updated zip file into the s3 bucket called LambdaS3将更新后的 zip 文件上传到名为LambdaS3的 s3 存储桶中
  3. Update the CloudFormation stack by entering the updated version of the zip file as the parameter LambdafileVersion通过输入 zip 文件的更新版本作为参数LambdafileVersion来更新 CloudFormation 堆栈
  • What I expected: deployment and update will be successful我所期望的:部署和更新会成功
  • Actual result: Get the message from AWS "There was an error creating this change set The submitted information didn't contain changes. Submit different information to create a change set."实际结果:从 AWS 获取消息“创建此更改集时出错。提交的信息不包含更改。提交不同的信息以创建更改集。” during updating the stack, while deployment is successful.在更新堆栈期间,部署成功。

My template is as below我的模板如下

AWSTemplateFormatVersion: "2010-09-09"
Metadata: ""
Description: ""
Parameters:

  LambdaS3:
    Description: Api Gateway Authorizer Lambda S3Bucket Name
    Type: String

  Lambdafilename:
    Description: Api Gateway Authorizer Lambda file Name (Latest)
    Type: String

  LambdafileVersion:
    Description: Lambda zip file version
    Type: String

Transform: AWS::Serverless-2016-10-31

Resources:
  LambdaFunction:
    DeletionPolicy: "Delete"
    Type: "AWS::Serverless::Function"
    Properties:
      Description: ""
      FunctionName: "LambdaFunction"
      Handler: "lambda_function.lambda_handler"
      CodeUri:
        Bucket: !Ref LambdaS3
        Key: !Sub '${Lambdafilename}.zip'
        Version: !Ref LambdafileVersion
      MemorySize: 512
      Role: !GetAtt IAMRole2.Arn
      Runtime: "python3.8"
      Timeout: 20
      Tracing: "PassThrough"
      AutoPublishAlias: live
      DeploymentPreference:
        Type: Linear10PercentEvery10Minutes

This does not work because you are using CodeDeploy.这不起作用,因为您使用的是 CodeDeploy。 If you want to update functions they way you are trying, then you have to remove the following from your code:如果您想按照您尝试的方式更新函数,则必须从代码中删除以下内容:

      AutoPublishAlias: live
      DeploymentPreference:
        Type: Linear10PercentEvery10Minutes

Follow up:跟进:

Instead of removing DeploymentPreference property, adding AutoPublishCodeSha256 is the right solution.而不是删除DeploymentPreference属性,添加AutoPublishCodeSha256是正确的解决方案。

According to AWS docs,根据 AWS 文档,

  • "This property addresses a problem that occurs when an AWS SAM template has the following characteristics: the DeploymentPreference object is configured for gradual deployments (as described in Deploying serverless applications gradually), the AutoPublishAlias property is set and doesn't change between deployments, and the CodeUri property is set and doesn't change between deployments." “此属性解决了当 AWS SAM 模板具有以下特征时出现的问题:DeploymentPreference object 配置为逐步部署(如逐步部署无服务器应用程序中所述),AutoPublishAlias 属性已设置且不会在部署之间更改,以及CodeUri 属性已设置并且在部署之间不会更改。”

  • "This scenario can occur when the deployment package stored in an Amazon Simple Storage Service (Amazon S3) location is replaced by a new deployment package that contains updated Lambda function code, but the CodeUri property remains unchanged (as opposed to the new deployment package being uploaded to a new Amazon S3 location and the CodeUri being changed to the new location)." "This scenario can occur when the deployment package stored in an Amazon Simple Storage Service (Amazon S3) location is replaced by a new deployment package that contains updated Lambda function code, but the CodeUri property remains unchanged (as opposed to the new deployment package being上传到新的 Amazon S3 位置,并且 CodeUri 被更改到新位置)。”

The described scenario is exactly my case.所描述的场景正是我的情况。 And after adding AutoPublishCodeSha256 property, my stack can be updated with the existance of DeploymentPreference property.添加AutoPublishCodeSha256属性后,可以使用DeploymentPreference属性的存在来更新我的堆栈。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以使用 CloudFormation 模板更新 AWS Lambda function 吗? - Can I update AWS Lambda function using CloudFormation template? 如何为使用 CloudFormation 模板创建的 AWS Lambda function 添加触发器? - How to add triggers for a AWS Lambda function created using a CloudFormation template? 如何在CloudFormation模板中描述AWS Lambda函数测试事件? - How to describe AWS Lambda function test events in CloudFormation template? 如何使用 CloudFormation 更新 AWS::AutoScaling::AutoScalingGroup? - How do I update an AWS::AutoScaling::AutoScalingGroup using CloudFormation? 如何使用cloudformation模板传递aws-lambda函数名称以生成cloudwatch警报 - How to pass aws-lambda function name to generate a cloudwatch alarm using cloudformation template 如何将 AWS Lambda 转换回 CloudFormation 模板 - How to convert AWS Lambda back into CloudFormation template 我如何运行AWS Lambda函数以使我知道CloudFormation已完成整个堆栈的创建 - How do I run a AWS Lambda function to let me know that CloudFormation has completed the entire stack creation 我们如何使用Java编写的AWS Lambda函数访问和响应CloudFormation自定义资源? - How do we access and respond to CloudFormation custom resources using an AWS Lambda function written in Java? 在 Cloudformation 模板中,如何在 IoT 规则中引用动态生成的 Lambda function ARN? - In a Cloudformation template, how do I reference a dynamically generated Lambda function ARN in an IoT Rule? 如何使用 lambda function 目标为 cloudwatch 事件制作 cloudformation 模板? - How do I make a cloudformation template for a cloudwatch event with a lambda function target?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM