Developing on AWS Lambda (Part 4): Switch to the AWS NodeJS SDK v3!
In Part 1 of Developing on AWS Lambda, I covered the basics of developing Lambda functions with the AWS NodeJS SDK. This post was written with the NodeJS SDK version 2 in mind. In December 2020, there was a new major release of the SDK: Version 3. I’ll cover what you should know about V3.
SDK V3!
This new version of the SDK provides 3 major enhancements and a new way to interact with AWS services via the Command API. Let’s get started:
Fully modular modules and dependencies
With SDK V2, we have to import/require the entire AWS SDK like this:
const AWS = require ("aws-sdk");
Our package.json
would have an entry like this:
"dependencies" : {
"aws-sdk": "^2.913.0"
}
This meant that the entire AWS SDK V2 needed to be installed as a dependency, running somewhere around 8MB in size. With SDK V3, you can now import just the dependencies you need to use. For example, if you only work with DynamoDB, you can write this:
// ES5
const { DynamoDBClient } = require ("@aws-sdk/client-dynamodb");// ES6
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
This is supported by individual dependencies in our package.json
file:
"dependencies" : {
"@aws-sdk/client-dynamodb": "^3.3.3"…