AWS Lambda Layers – How to Automatically Use the Latest Layer in AWS Lambda

amazon-lambdaamazon-web-servicesdeployment

We have a server-less stack that relies on multiple AWS Lambdas to perform tasks. To help make code re-usable we have multiple Lambda Layers shared across the Lambdas.

Our issue is, when we make a change to a layer, it creates a new version of the layer. All Lambdas that use this layer do not automatically use the latest version. They are however smart enough to detect that there is a new version and no longer run (throwing an exception that it needs to use the latest layer)

For 50+ Lambdas, The AWS portal has a terrible flow for updating layers. One-by-one, manually open every Lambda, remove the layer, add the new layer etc.

Is there a way to update all Lambdas to use the latest version of all necessary layers using the CLI or some other automated method?

Best Answer

Every time you update the layer loop through your Lambda functions and update their config. You can use AWS CLI to do that:

aws lambda update-function-configuration --layers ...

A better way though is to have a CI/CD deployment for your layers and lambdas and have dependencies configured. Whenever a Layer pipeline is updated it should trigger a re-deployment of all the Lambdas that depend on that layer. All modern CI/CD tools can do that - GitLab, Jenkins, GoCD, or even AWS CodePipeline.

That way all you have to do it push new layer code to the git repository and your CI/CD tool will take care of the rest.

Hope that helps :)

Related Topic