Albert B
Albert B

Reputation: 23

Approaches to updating AWS Lambda's and Layer's simultaneously, without causing a breaking change

We provision our infrastructure through Terraform and have set up an AWS Lambda along with a Layer to hold its dependencies (we are not using any frameworks like Serverless or SAM). We are using an alias to specify which Lambda version we are invoking. Currently I am working through creating a CI/CD pipeline, using the aws lambda CLI, and running into an issues.

Consider the scenario where we remove certain code from the Lambda function and remove a dependency from the Layer in the same MR/PR. When merging, we package up the function code and the dependency code into zip files and push them to S3. Now when deploying these changes the current steps are:

  1. Publish the new Layer version (aws lambda publish-layer-version)
  2. Publish the new Lambda code (aws lambda update-function-code).
  3. Update the function to use the new Layer (aws lambda update-function-configuration)
  4. Update the alias to point to this updated function (aws lambda update-alias).

However, between steps 3.) and 4.) there is a point were the alias is pointing to the old function code (with the removed dependency still there) but the new Layer (with the dependency removed). This is far from ideal. However, swapping the steps gives a similar issue where the Lambda and Layer are not aligned.

Has anyone else had a similar issue and managed to overcome it? Spinning up a temporary Lambda during this deployment phase (however this would involve modifying the Terraform)?

Can you update layers of a Lambda without affecting the alias?

Upvotes: 2

Views: 2671

Answers (2)

Gaurav Sharma
Gaurav Sharma

Reputation: 2203

Considering you are using GitHubActions.

  1. Update the lambda code. You can use the GitHub action provider appleboy/[email protected] to deploy your lambda
  2. You can use theserverfault/[email protected] Which is pretty much convenient way to update your lambda layers and also refresh all the dependant lambdas to point to this latest published layer version. You can find the documentation here and Github Marketplace.

Otherwise you can use the custom scripts:

  1. aws lambda publish-layer-version to publish layer
  2. aws lambda update-function-code to update function
  3. aws lambda update-function-configuration to update the layer to the latest version.

However, consider looking at theserverfault/[email protected]. It takes care of N-number of lambdas without having to write any iterations and custom scripts.

Upvotes: 0

t_krill
t_krill

Reputation: 390

After updating the Lambda Function code itself, use Lambda Function Versions to move between versions behind the alias.

New Steps:

  1. Publish the new Layer version (aws lambda publish-layer-version)
  2. Publish the new Lambda code (aws lambda update-function-code).
  3. Update the function to use the new Layer (aws lambda update-function-configuration).
  4. Publish the new Lambda Function Version (publish-version). Record version.
  5. Update the alias to point to this updated function version (aws lambda update-alias --function-version).

You may have to publish an initial version of the function to start this process.

Documentation

Upvotes: 5

Related Questions