Migrating from AWS SDK v2 to v3 in Node.js AWS Lambda Functions
Upgrading from AWS SDK v2 to v3 in a Node.js Lambda function can bring significant benefits, such as improved performance and smaller package sizes. AWS SDK v3 is fully modular, meaning you import only the packages you need, which leads to faster cold starts and more efficient Lambda execution.
This guide will walk you through the process of upgrading your Lambda function, including automating the conversion process using a bash script.
Step 1: Install AWS SDK JS Codemod
First, you need to install the AWS SDK JS Codemod, a tool that helps you automatically convert your code from AWS SDK v2 to v3.
yarn add -D aws-sdk-js-codemod
Or, if you prefer npm:
npm install -D aws-sdk-js-codemod
Step 2: Create a Bash Script to Automate the Process
Instead of manually searching for files that use AWS SDK v2, we’ll automate the process by creating a bash script that scans your codebase for AWS SDK imports and runs the codemod on those files.
Create the following bash script, and save it as convert-to-v3.sh
:
#!/bin/bash
cd src
grep -rl "aws-sdk" . | while read -r file ; do
echo "Running codemod on $file"
npx aws-sdk-js-codemod -t v2-to-v3 "$file"
done
echo "AWS SDK v2 to v3 migration complete."
Once the script is in place, run the following commands to execute it:
chmod +x convert-to-v3.sh
./convert-to-v3.sh
Step 3: Review and Remove AWS SDK v2
After running the script, review the changes to ensure the migration was successful. If everything looks good, remove the aws-sdk
package:
yarn remove aws-sdk
Here’s an example of what your file might look like after the conversion:
Before:
const AWS = require('aws-sdk');
const S3 = new AWS.S3();
After:
const { S3Client, GetObjectCommand } = require('@aws-sdk/client-s3');
const s3Client = new S3Client();
Step 4: Handle Native Modules (e.g. bcrypt)
If your Lambda function uses native modules like bcrypt
, you’ll need to ensure they are compiled for the correct architecture. This can be done using Docker.
Run the following command to install bcrypt
in a Lambda-compatible environment:
docker run -v $(pwd):/var/task --platform linux/amd64 --rm node:18 bash -c "cd /var/task && yarn add bcrypt"
This will ensure that bcrypt
is compiled for the correct Linux x86_64
architecture used by AWS Lambda.
Step 5: Deploy and Test the Lambda Function
Finally, after the migration, package your Lambda function and deploy it to AWS. Here’s a quick summary of the steps:
- Zip the function:
zip -r my-lambda.zip dist/ node_modules/ package.json yarn.lock
- Deploy to AWS Lambda using the AWS CLI:
aws lambda update-function-code --function-name YOUR_LAMBDA_FUNCTION_NAME --zip-file fileb://my-lambda.zip
After deployment, test the function thoroughly to ensure everything works as expected.
Conclusion
Migrating to AWS SDK v3 can reduce the size of your Lambda functions and improve performance. By automating the migration with a bash script and handling native dependencies like bcrypt
using Docker, you can smoothly transition to the more modern AWS SDK v3.