4
votes

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.

2
HI, i am also got struck at same usecase. Did you able to find out the value for S3ObjectVersion so that lambda will get latest s3 zip file ?Private
@Private Unfortunately not.Vingtoft
Okay thanks for your response. I asked the same in aws forum too, hoping i will get any positive response.Private
@Private please consider to share if you find a solution :-)Vingtoft
Sure. I will share..Private

2 Answers

3
votes

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

A quick hack that works well if you have a lot of lambdas in your CF stack so you want to manually select which lambdas to update from the source, and have lambda code that will work equally well under multiple runtimes. Just change the runtime version in your stack (say from "Runtime: nodejs8.10" to "Runtime: nodejs10.x". CodePipeline/CloudFormation will update just that function.