[英]Can I update AWS Lambda function using CloudFormation template?
I want to deploy and update Lambda function using CloudFormation stacks.我想使用 CloudFormation 堆栈部署和更新 Lambda function。
My Stack creation workflows are:我的堆栈创建工作流程是:
However, I have no idea how to update the Lambda function using the template.但是,我不知道如何使用模板更新 Lambda function。 I have tried to upload the new function code to S3 and save as a new file name.
我尝试将新的 function 代码上传到 S3 并另存为新文件名。 And use the new file as parameters to update stacks.
并使用新文件作为参数来更新堆栈。 But CloudFormation can not detect any changes due to the file is a zip file.
但是由于文件是 zip 文件,CloudFormation 无法检测到任何更改。
My questions:我的问题:
AWS::Lambda::Function
or AWS::Serverless::Function
resources?AWS::Lambda::Function
还是AWS::Serverless::Function
资源? I want to have version control supported. Thank you.谢谢你。
My CloudFormation template is as below.我的 CloudFormation 模板如下。
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
Transform: AWS::Serverless-2016-10-31
Resources:
# LambdaFunction:
# DeletionPolicy: "Delete"
# Type: "AWS::Lambda::Function"
# Properties:
# Description: ""
# FunctionName: "LambdaFunction"
# Handler: "lambda_function.lambda_handler"
# Code:
# S3Bucket: !Ref LambdaS3
# S3Key: !Sub '${Lambdafilename}.zip'
# MemorySize: 512
# Role: !GetAtt IAMRole2.Arn
# Runtime: "python3.8"
# Timeout: 20
# TracingConfig:
# Mode: "PassThrough"
LambdaFunction:
DeletionPolicy: "Delete"
Type: "AWS::Serverless::Function"
Properties:
Description: ""
FunctionName: "LambdaFunction"
Handler: "lambda_function.lambda_handler"
CodeUri:
Bucket: !Ref LambdaS3
Key: !Sub '${Lambdafilename}.zip'
MemorySize: 512
Role: !GetAtt IAMRole2.Arn
Runtime: "python3.8"
Timeout: 20
Tracing: "PassThrough"
AutoPublishAlias: live
DeploymentPreference:
Type: Linear10PercentEvery10Minutes
CodeUri
takes Version . CodeUri
采用Version 。 So if your bucket is version, any change to its objects will, even if same name, result in different version.因此,如果您的存储桶是版本,则对其对象的任何更改,即使名称相同,也会导致不同的版本。 So once you specify new version as a parameter, your function will get updated.
因此,一旦您将新版本指定为参数,您的 function 就会得到更新。
changes to a deployment package in Amazon S3 are not detected automatically during stack updates.在堆栈更新期间不会自动检测到对 Amazon S3 中部署 package 的更改。 To update the function code, change the object key or version in the template.
要更新 function 代码,请更改模板中的 object 密钥或版本。
Unfortunately, unless you change the "S3Key" on 'AWS::Lambda::Function' resource on every update, CloudFormation will not see it as a change不幸的是,除非您在每次更新时更改“AWS::Lambda::Function”资源上的“S3Key”,否则 CloudFormation 不会将其视为更改
When you apply a CloudFormation stack update, it will check if there is an update in properties of any of the deployed resource.当您应用 CloudFormation 堆栈更新时,它将检查任何已部署资源的属性中是否有更新。 When there is an update in Lambda resource property like memory, timeout, environment vars, etc, there is no problem as a CloudFormation will pick these changes and update accordingly.
当 Lambda 资源属性(如 memory、超时、环境变量等)有更新时,没有问题,因为 CloudFormation 会选择这些更改并相应地更新。
The problem arises when there is an update to Lambda function code, or to any of its dependencies.当更新 Lambda function 代码或其任何依赖项时,就会出现问题。 Since these are not resource property updates, there is no way for CloudFormation to know if there is a change, and update the affected components.
由于这些不是资源属性更新,CloudFormation 无法知道是否有更改并更新受影响的组件。
The problem arises when there is an update to Lambda function code, or to any of its dependencies.当更新 Lambda function 代码或其任何依赖项时,就会出现问题。 Since these are not resource property updates, there is no way for CloudFormation to know if there is a change, and update the affected components.
由于这些不是资源属性更新,CloudFormation 无法知道是否有更改并更新受影响的组件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.