Home > Mobile >  Approaches to updating AWS Lambda's and Layer's simultaneously, without causing a breaking
Approaches to updating AWS Lambda's and Layer's simultaneously, without causing a breaking

Time:09-15

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?

CodePudding user response:

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

  • Related