简体   繁体   English

我可以使用 CloudFormation 模板更新 AWS Lambda function 吗?

[英]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:我的堆栈创建工作流程是:

  1. upload my Lambda function code(zip file) to S3将我的 Lambda function 代码(zip 文件)上传到 S3
  2. Use the s3 bucket(where the lambda function code is stored) and the LambdaFileName(zip file name) as CloudFormation parameters to create CloudFormation stacks by deploying the CloudFormation template使用 s3 存储桶(其中存储 lambda function 代码)和 LambdaFileName(zip 文件名)作为 CloudFormation 参数,通过部署 CloudFormation 模板创建 CloudFormation 堆栈

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:我的问题:

  1. How to update Lambda function using CloudFormation template如何使用 CloudFormation 模板更新 Lambda function
  2. Should I use 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 不会将其视为更改

Source 资源

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 无法知道是否有更改并更新受影响的组件。

please refer following blog post请参考以下博客文章

暂无
暂无

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

相关问题 如何使用 CloudFormation 模板更新 AWS Lambda function - How do 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? 如何使用aws中的cloudformation在api网关中请求参数并将其传递给lambda函数? - How can I request parameters in api gateway using cloudformation in aws and pass it down to lambda function? AWS CloudFormation中的Lambda函数 - Lambda Function in AWS CloudFormation 使用 CloudFormation 模板更新 AWS Athena 工作组 - Update AWS Athena workgroup using CloudFormation template 如何使用cloudformation模板传递aws-lambda函数名称以生成cloudwatch警报 - How to pass aws-lambda function name to generate a cloudwatch alarm using cloudformation template 使用 Cloudformation 模板创建两者时,AWS SNS 不会调用 lambda 函数 - AWS SNS is not invoking lambda function when both are created using Cloudformation template 如何在CloudFormation模板中描述AWS Lambda函数测试事件? - How to describe AWS Lambda function test events in CloudFormation template? 如何仅在创建 AWS::Cloudformation::Stack 时调用 AWS::Lambda::Function? - How can I invoke an AWS::Lambda::Function only on the creation of a AWS::Cloudformation::Stack? 使用 cloudformation 模板的 AWS Lambda 的动态环境变量 - Dynamic environment variables for AWS Lambda using cloudformation template
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM