Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating aws lambda layer dependencies

The situation is that I have a load of aws lambda functions (using node js 8.10) that all do something very different and they're all deployed using CloudFormation.

They all share a few functions which are very complex.

At the moment, if the common code changes, which is fairly frequent, I replicate the common code between each of the projects (including source control) and then redeploy each of the functions. This has always felt wrong.

Now we have lambda layers - yay! or yay?

Great, so now I can maintain the code in a single repo - tick But the rest of the process is not really any better and possibly worse...

If I put the layer in a CloudFormation template and export the ARN for import into the lambda function templates then the exported ARN is only ever for version 1 of the layer.

I could form the ARN without the version using the Sub function and then add the version in the lamda function CloudFormation templates. But, whenever there's a change to the common code, I'd still need to update all downstream lambda function CloudFormation templates to add the latest version.

I could script it but it's still a massive PITA and doesn't really save much effort. I'd need to get the latest of each lambda function project update the version number, commit back to the repo, pr, merge, blah blah blah.

Is there no other way of always using the latest version of a layer?

like image 903
Russell Keane Avatar asked Dec 13 '18 18:12

Russell Keane


People also ask

How do I add dependencies to AWS Lambda?

The process of adding dependencies to an AWS Lambda consists of two steps. First, we have to install the dependencies in the source code directory. Later, we have to package our lambda function into a zip file that also contains all of the dependency files.

How do you update Lambda function runtime?

To change the runtime, you create a new container image. When you use a . zip file archive for the deployment package, you choose a runtime when you create the function. To change the runtime, you can update your function's configuration.

Can Lambda update environment variables?

You can't change the configuration (which includes environment variables) or function code in a published Lambda function version. To change the environment variables of a published Lambda function version, you must first change the current, unpublished function version ($LATEST). Then, publish a new function version.


1 Answers

Using Serverless to deploy and CloudFormation Outputs can help with this situation.

  1. Define your layer in it's own service. Create an Output resource (but don't create an export name).
resources:
  Outputs:
    MYOUTPUTNAME:
      Value:
        Ref: MYLAYERLambdaLayer # LambdaLayer is a required suffix
  1. Reference the output as the layer for whatever function requires it
functions:
  ...other required function keys in serverless
  layers:
    - ${cf:NAME_OF_STACK.MYOUTPUTNAME}
  1. Anytime you redeploy the layer, you must force redeploy the entire stack of functions that reference the layer (sls deploy --force). Redeploying just the function will not update the output reference.

Note: if you use Output Export Names, you will run into an error redeploying the layer service as the current version is referenced. Therefore, it's better to use a reference to the stack output which doesn't cause this error.

like image 194
knappsacks Avatar answered Sep 19 '22 05:09

knappsacks