Upgrading AWS Lambda to Node@18
So you have AWS Lambda in production, using one of Node 14 or 16 versions, and you want to upgrade to the new Node@18 Runtimes?
Objective
We want readers to know what they have to do when upgrading their AWS Lambda to the Node@18 runtime via a checklist.
Checklist
- Upgrade your local NodeJS runtime to version 18 either by directly downloading from nodejs.org, or using
nvm
. As at the time of writing this, node18.14.2
is the LTS version. - Run
npm install
to get the new version ofpackage-lock.json
- Add an
engines
field to thepackage.json
to prompt when an unsupported node version is used to access the project. - Update your AWS Lambda config to use the
nodejs18.x
runtime. If you usecloudformation
, this means changing to"Runtime": "nodejs18.x"
. - Update your CI/CD pipelines to build with node version
18.14.2
. - If you use Webpack, I’ve found webpack
5.75
works great for building project written for node@18. - If your project uses aws-sdk v2, you will want to consider that the AWS lambda
nodejs18.x
runtime comes built-in with version3.188.0
of the aws-sdk, which is v3, so you need to make a decision about your projects. Don’t worry, I’ve got you.
Upgrade aws-sdk to V3
You can find aws-sdk v3 packages by searching for
@aws-sdk/client-
on npm.
If you upgrade your aws-sdk from v2 to v3, then you can take advantage of using the exact 3.188.0
version of the @aws-sdk/*
packages built-in with the AWS Lambda nodejs18.x
runtime.
This means that you can bundle your code to not include the @aws-sdk/*
packages with webpack externals or equivalent in other bundlers, keeping your bundle size small, which has its advantages.
{
externals: {
"@aws-sdk/client-ses": "@aws-sdk/client-ses"
}
}
However, if you must use a higher sdk version than the built-in 3.188.0
version, then you will want to include your sdk code in your bundle, which will increase your bundle size, and you may run into bundling errors such as it not being able to resolve the aws-crt
module, which can be fixed by installing the aws-crt
module.
Staying with aws-sdk V2
If you stay with the v2 of the aws-sdk, then you will have to bundle it with your code. This will increase your bundle size.
End-to-end test everything
So you’ve successfully upgraded your AWS Lambda runtime to nodejs18.x
, and you have successfully deployed it to the cloud. Now, you need to test everything, to be sure nothing is broken.
Thanks for reading this. If there are steps you have found that I have not included in this article, please let me know the comments.
Have fun, coding!