[英]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:我的部署工作流程是:
Lambda
Lambda
的 zip 文件中LambdaS3
LambdaS3
的 s3 存储桶的 s3 版本控制LambdaS3
LambdaS3
的 s3 存储桶中CFtemplate
as below into the s3bucket LambdaS3
CFtemplate
上传到 s3bucket LambdaS3
LambdaS3
, "Lambda" as parameter Lambdafilename
and the version of the zip file as the parameter LambdafileVersion
LambdaS3
、“Lambda”作为参数Lambdafilename
并将 zip 文件的版本作为参数LambdafileVersion
来创建 CloudFormation 堆栈My lambda code update workflow are:我的 lambda 代码更新工作流程是:
Lambda
Lambda
LambdaS3
LambdaS3
的 s3 存储桶中LambdafileVersion
LambdafileVersion
来更新 CloudFormation 堆栈 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.