简体   繁体   中英

How do I update AWS Lambda function using CloudFormation template

I want to deploy and update my lambda function code using CloudFormation Template.

My deployment workflow are:

  1. Compress my lambda function code into a zip file called Lambda
  2. Enabling s3 versioning of the s3 bucket called LambdaS3
  3. Upload the zip file into s3 bucket called LambdaS3
  4. Upload the CloudFormation template CFtemplate as below into the 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

My lambda code update workflow are:

  1. Compress my updated lambda function code into a zip file called Lambda
  2. Upload the updated zip file into the s3 bucket called LambdaS3
  3. Update the CloudFormation stack by entering the updated version of the zip file as the parameter LambdafileVersion
  • 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." 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. 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.

According to AWS docs,

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

  • "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)."

The described scenario is exactly my case. And after adding AutoPublishCodeSha256 property, my stack can be updated with the existance of DeploymentPreference property.

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