Why and how you should use AWS SDK for JavaScript (v3) on Node.js 18

Why and how you should use AWS SDK for JavaScript (v3) on Node.js 18

The Node.js 18.x runtime was promoted to Active Long Term Support (LTS) on October 25th, 2022, and is now available in AWS Lambda. The AWS SDK for JavaScript (v3) is included by default in AWS Lambda Node.js 18 runtime as a convenience for developers building simpler functions.

You can read about features in Node.js 18 in Node.js 18 is now available! from the Node.js team. To learn how to use these features in AWS Lambda, please read Node.js 18.x runtime now available in AWS Lambda. You can also look through operator guide for managing AWS SDKs in Lambda functions.

We recommend using AWS SDK for JavaScript (v3) on Node.js 18 runtime.

Why should I adopt v3?

On December 15th, 2020, we announced the general availability of the AWS SDK for JavaScript (v3).

In v3, the modular packages reduce the bundle size of your application by ~75% when compared to v2, which helps improve application performance. The middleware stack allows you to easily customize SDK behavior, where you can add your custom asynchronous actions and/or remove default ones. The v3 has first-class TypeScript support, which saves you time catching errors and providing fixes before you run your code.

To get started with v3, visit our Developer Guide or API Reference. You can also visit our self-guided workshop, which builds a simple note taking application using JavaScript SDK v2 and provides step-by-step migration instructions to v3. To test your universal JavaScript code in Node.js, browser and react-native environments, visit our code samples repo.

How can I migrate to v3?

The experimental collection of codemod scripts in can help migrate your existing AWS SDK for JavaScript (v2) application to use v3 APIs. You can run the transform as follows:

$ npx aws-sdk-js-codemod -t v2-to-v3 PATH...

For example, consider you have the following code, which creates a Amazon DynamoDB client from v2 and calls listTables operation:

// example.ts
import AWS from "aws-sdk";

const region = "us-west-2";
const client = new AWS.DynamoDB({ region });
client.listTables({}, (err, data) => {
  if (err) console.log(err, err.stack);
  else console.log(data);
});

You can run our v2-to-v3 transform on example.ts as follows:

$ npx aws-sdk-js-codemod -t v2-to-v3 example.ts

The transform will convert the DynamoDB import to v3, create v3 client and call listTables operation as follows:

// example.ts
import { DynamoDB } from "@aws-sdk/client-dynamodb";

const region = "us-west-2";
const client = new DynamoDB({ region });
client.listTables({}, (err, data) => {
  if (err) console.log(err, err.stack);
  else console.log(data);
});

Feedback

We’ve implemented transforms for common use-cases. If your code doesn’t transform correctly, please create a bug report or feature request with example input code and observed/expected output code. If your specific use case is already reported in an existing issue, show your support by an up-vote.

This content was originally published here.