paint-brush
Notes on Migrating AWS SDK v2 to v3 (Javascript) - A Quick Guide to Help You Outby@lregalado
7,109 reads
7,109 reads

Notes on Migrating AWS SDK v2 to v3 (Javascript) - A Quick Guide to Help You Out

by Lorenzo RegaladoJuly 22nd, 2023
Read on Terminal Reader
Read this story w/o Javascript

Too Long; Didn't Read

The AWS SDK for JavaScript (v2) will be put into maintenance mode this year 2023. I share some tips that will save you time rule migrating to AWS SDK (v3)
featured image - Notes on Migrating AWS SDK v2 to v3 (Javascript) - A Quick Guide to Help You Out
Lorenzo Regalado HackerNoon profile picture

The AWS SDK for JavaScript (v2) will be put into maintenance mode this year (2023). The v3 has been available for some time, and it’s the version included on the newer lambda runtime for Node18, which is also the current LTS node version.


So, If you want to update your lambdas to the Node18 Lambda runtime, you have to add the SDK v2 to your current lambda function or stay in Node16 ( you could roll your own runtime, but that’s another topic).


The benefits of the new SDK version are more modularity, including what you need, smaller package size as is shipped directly on the newer lambda image, Typescript support, and a custom middleware stack to customize some SDK behavior (which I have yet to use).


Migrating isn’t too difficult; you can follow this doc. However, I encountered some details that weren’t obvious at first that will save you some googling/prompting time.

Lambda

v2:

const AWS = require('aws-sdk');
const lambda = new AWS.Lambda(options);
const result = await lambda.invoke(invokeParams).promise();
const resultPayload = JSON.parse(result.Payload)


v3:

const { LambdaClient, InvokeCommand } = require('@aws-sdk/client-lambda');
const { toUtf8 } = require('@aws-sdk/util-utf8-node');

const command = new InvokeCommand(invokeParams);
const result = await lambda.send(command);
const resultPayload = JSON.parse(toUtf8(result.Payload));// Now the result payload is a Uint8 array that needs to be decoded


At the time of writing, the package @aws-sdk/util-utf8-node is marked as deprecated in favor of @aws-sdk/util-utf8. But in the lambda runtime, nodejs18.x isn’t included yet, so if you want to use it, you have to add it to your project production dependencies.

DynamoDB

v2:

const AWS = require('aws-sdk');
dynamoDbClient = new AWS.DynamoDB.DocumentClient(options);

await dynamoDbClient.put(params).promise();
await dynamoDbClient.get(params).promise();
await dynamoDbClient.update(params).promise();
await dynamoDb.delete(params).promise();
await dynamoDb.scan(params).promise();
await dynamoDb.query(params).promise();


v3:

const { DynamoDBClient } = require('@aws-sdk/client-dynamodb');
const {
  DynamoDBDocument, GetCommand, PutCommand, DeleteCommand, QueryCommand, UpdateCommand, ScanCommand,
} = require('@aws-sdk/lib-dynamodb');

const dynamoDbClient = new DynamoDBClient(options);
const docClient = DynamoDBDocument.from(dynamoDbClient);

await docClient.send(new PutCommand(params));
await docClient.send(new GetCommand(params));
await docClient.send(new UpdateCommand(params));
await docClient.send(new DeleteCommand(params));
await docClient.send(new ScanCommand(params));
await docClient.send(new QueryCommand(params));


If you used to process some streams from a DynamoDB table and used the unmarshall function, v2:

const AWS = require('aws-sdk');
const {unmarshall} = AWS.DynamoDB.Converter;


v3:

const { unmarshall } = require('@aws-sdk/util-dynamodb');

S3

v2:

const AWS = require('aws-sdk');
const s3Client = new AWS.S3(options);

s3Client.upload(params, callBack);


v3:

const { S3Client } = require('@aws-sdk/client-s3');
const { Upload } = require('@aws-sdk/lib-storage');
const s3Client = new S3Client(options);

const parallelUploads3 = new Upload({
      client: s3Client,
      params
    });
const result = await parallelUploads3.done();

SNS

v2:

const AWS = require('aws-sdk');
const snsClient = new AWS.SNS(options);

await snsClient.publish(data).promise();


v3:

const { SNSClient, PublishCommand } = require('@aws-sdk/client-sns');
const snsClient = new SNSClient(options);

await snsClient.send(new PublishCommand(data));