简体   繁体   中英

Can I update AWS Lambda function using CloudFormation template?

I want to deploy and update Lambda function using CloudFormation stacks.

My Stack creation workflows are:

  1. upload my Lambda function code(zip file) to 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

However, I have no idea how to update the Lambda function using the template. I have tried to upload the new function code to S3 and save as a new file name. 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.

My questions:

  1. How to update Lambda function using CloudFormation template
  2. Should I use AWS::Lambda::Function or AWS::Serverless::Function resources? I want to have version control supported.

Thank you.

My CloudFormation 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

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 . 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.

changes to a deployment package in Amazon S3 are not detected automatically during stack updates. To update the function code, change the object key or version in the template.

Unfortunately, unless you change the "S3Key" on 'AWS::Lambda::Function' resource on every update, CloudFormation will not see it as a change

Source

When you apply a CloudFormation stack update, it will check if there is an update in properties of any of the deployed resource. 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.

The problem arises when there is an update to Lambda function code, or to any of its dependencies. 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.

The problem arises when there is an update to Lambda function code, or to any of its dependencies. 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.

please refer following blog post

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM