Home > Lambda Layers With CDK for Nodejs
Lambda Layers With CDK for Nodejs
Lambda layers allow us to store common lambda code only once and connect to multiple lambda functions.
On the surface it looks like very useful feature and we should add layers to every lambda functions. But this leads to misuse and abuse of lambda layers.
There is only single use case for lambda layers. If your function does not fit this use case then you shouldn't use lambda layer.
Use lambda layers when any common code or library needs to be updated frequently.
So for example if you have stripe dependency library or other library that updates multiple times per week. Then its perfect use case for lambda layer.
Not when you have jsonwebtoken dependency library that hasn't been updated in last 2 years at the time of writing this article.
Or any dependency library that updates once per few months. Then you should NOT put it inside lambda layer.
Same goes for having common code in lambda layer. For example short term sale pricing.
So if you have multiple products and you want to set discount to many products for a short period. Don't put common code that won't update for many months.
To read rest of the article please Purchase Subscription
I would recommend to develop CDK project inside development docker container. So we no longer have the issue of "this works on my computer".
And it will not mess with your existing workflow. So checkout my blog post for detailed video explanation.
Then checkout CDK development container settings on github.
So create folder and init cdk project as follows
mkdir lambda-layer && cd $_
cdk init app --language typescript
$_ is variable for final word of previous command. So in this case it was
Don't forget to remove
By default cdk will not commit
Then we are going to create couple of lambda functions as follows
Both lambda functions have same exact code. Except the message would be different.
There is no
uuid package installed and there is no
discount.js file. Both will be added to
our lambda layer.
Now lambda layer path is different for language the lambda is written in. For nodejs you have to store everything inside nodejs folder and zip that folder as follows.
For other languages please refer to aws docs
deployment.zip as follows
mkdir nodejs && cd $_
npm init -y
npm install uuid
rm package.json package-lock.json
zip -r deployment.
Before you zip the nodejs folder make sure to add following code inside
Then add following code inside the
So we are creating a layer from the zip file we created in earlier step. The runtime is set to nodejs version 14.x and give description so we know what is inside this layer.
By giving layer permission with account id. This layer can be added to any lambda function within this account.
You can also create public layer that can be used across multiple aws accounts. You just don't add the above permission for that.
This is IMPORTANT. After you deploy layer for first time or deploy a layer update then you will
layerVersionArn. It will be in the following format
- First Version: arn:aws:lambda:us-east-1:1234567890:layer:uuidLayer222C238D:1
- Second Version: arn:aws:lambda:us-east-1:1234567890:layer:uuidLayer222C238D:2
- Third Version: arn:aws:lambda:us-east-1:1234567890:layer:uuidLayer222C238D:3
So you save the layer version inside
variables.ts and create latest lambda layer as follows
Just to be on safe side I have set its compatible runtime. So by mistake I should not attach to golang runtime or different version of nodejs runtime.
And the layer version arn came from variables.
Now we attach this layer to multiple lambdas as follows
Above is standard lambda function and we have simply attached latest layer.
Only use lambda layer when a common code or repository is updated frequently. Keep an eye on lambda layer version arn because it changes each time you update the layer.
And develop your projects inside docker container and deploy with aws cdk.
Free users cannot comment below so if you have questions then tweet me @apoorvmote. I would really like to hear your brutally honest feedback.
If you like this article please consider purchasing paid