Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Cloudformation | Configure Lambda to Use Latest Version of Code in S3 Bucket

Im using codepipeline, codebuild and cloudformation on AWS.

My flow is:

  1. Push a commit to github, this triggers the codepipeline
  2. Codebuild uploads (zipped) lambda functions to S3 bucket
  3. Cloudformation configure lambda functions

Cloudformation (simplified):

CreateDoctorLambda:
    Type: AWS::Lambda::Function
    Properties:
      Runtime: python3.6
      Handler: lambda_function.lambda_handler
      Role:
        Fn::GetAtt:
          - LambdaExecutionRole
          - Arn
      Code:
          S3Bucket: !Ref LambdaFunctionS3Bucket
          S3Key: CreateDoctor.zip
          S3ObjectVersion: Latest <-- This value is invalid

Problem: When I update the code for lambda functions (this new code is zipped and uploaded to the S3 bucket during codebuild), the change is not deployed to the existing lambda functions.

According to AWS documentation:

To update a Lambda function whose source code is in an Amazon S3 bucket, you must trigger an update by updating the S3Bucket, S3Key, or S3ObjectVersion property. Updating the source code alone doesn't update the function.

Question: Is there any way to tell Cloudformation to use the latest version of the code stored in S3? Using S3ObjectVersion: Latest will result in an error.

like image 266
Vingtoft Avatar asked Aug 16 '18 10:08

Vingtoft


1 Answers

Its just an alternative workflow, but maybe it will solve your problem:

  1. Instead of saving the artifact with the same name, you must configure CodePipeline or CodeBuild to generate a different name for the artifact based, for example, in the deploy time;
  2. At your CloudFormation Action you pass the artifact name as a parameter for the template (https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/continuous-delivery-codepipeline-parameter-override-functions.html) and it will redeploy the function based on new code.
like image 105
Gustavo Tavares Avatar answered Oct 12 '22 13:10

Gustavo Tavares