Using AWS SDK for JavaScript with Node.js Lambda Functions for Cold Start Benefit

In this article, we are going to learn AWS SDK for JavaScript Version 3 and Using AWS SDK for JavaScript with Node.js Lambda Functions for Cold Start and Concurrency Benefits.

AWS SDK for JavaScript with Node.js Lambda Functions

The AWS SDK for JavaScript v3 API Reference Guide provides a JavaScript API for AWS services. By the end of the article, we will learn AWS SDK for JS v3 and how interactions developed in NodeJS Lambda functions for Cold Start and Concurrency Benefits.

I have just published a new course — AWS Lambda & Serverless — Developer Guide with Hands-on Labs.

What is AWS SDK ?

AWS SDK stands for Software Development Kit is for interacting AWS resources from your codebase. AWS SDK — software development kit simplifies use of AWS Services by providing a set of libraries that are consistent and familiar for developers. It provides support for APIs of AWS. And there are several programming languages AWS SDK packages that you can use, like Java, NodeJS, JavaScript, .NET, Go and so on..

Most common use case of AWS SDKs, when you have infrastructure exist on AWS, you can access these infrastructure resource from your development repositories. For example, if you have DynamoDB database under your AWS account, you can perform crud operations in your application code with using AWS SDK libraries.

AWS SDK for JavaScript Version 3 and using SDK with Node.js

The AWS SDK for JavaScript v3 API Reference Guide provides a JavaScript API for AWS services. We can use the JavaScript API to build libraries or applications for Node.js.

AWS SDK for JavaScript Version 3

The AWS SDK for JavaScript v3 is a rewrite of v2 with some great new features. As with version 2, it enables you to easily work with aws resources, but has a modular architecture with a separate package for each service. It also includes many frequently requested features, such as a first-class TypeScript support and a new middleware stack.

Using the AWS SDK with Node.js

Node.js is a cross-platform runtime for running server-side JavaScript applications. We will use Node.js to write on-demand AWS Lambda functions.

We have 2 main resources, when developing our interactions;

You can find example codes about DynamoDB interactions with the SDK.

Example of DynamoDB from the SDK

Create a new Node.js project. Inside of the project, run: yarn add @aws-sdk/client-dynamodb. Adding packages into package.json file. Create a new file called index.js, create a DynamoDB service client and send a request.

const { DynamoDBClient, ListTablesCommand } = require(“@aws-sdk/client-dynamodb”);(async () => {
const client = new DynamoDBClient({ region: “us-west-2” });
const command = new ListTablesCommand({});
try {
const results = await client.send(command);
console.log(results.TableNames.join(“\n”));
} catch (err) {
console.error(err);
}
})();

As you can see that package is here @aws-sdk/client-dynamodb. This is only DynamoDB package, so by this way, it can reduce the function library package size with only installing required packages.

Also you can find all examples with searhing aws resource for example search for “eventbridge”: https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-eventbridge/index.html

See example usage how we can publish message — send a request to EventBridge. As you can see that we have seen AWS SDK for JavaScript for version 3 both Developer Guide and API Reference Guide.

What’s new in AWS SDK for JavaScript Version 3 and Cold Start Benefit

we are going to learn What’s new in The AWS SDK for JavaScript v3 and why we are using Version 3 ? and understand the Lambda function Cold Start Benefit when using V3.

Basically, The AWS SDK for JavaScript v3 is a rewrite of v2 with some great new features. So we can say that Version 3 of the SDK for JavaScript (V3) contains the following new features.

  • Modularized packages: Users can now use a separate package for each service.
  • New middleware stack: Users can now use a middleware stack to control the lifecycle of an operation call.

In addition, the SDK is written in TypeScript, which has many advantages, such as static typing.

Lets focus on Modularized packages:

Version 2 of the SDK for JavaScript (V2) required you to use the entire AWS SDK, see code block;

var AWS = require(“aws-sdk”);

Loading the entire SDK isn’t an issue if your application is using many AWS services. However, if you need to use only a few AWS services, it means increasing the size of your application with code you don’t need or use. But In V3, you can load and use only the individual AWS Services you need. See DynamoDB example:

import {DynamoDB} from “@aws-sdk/client-dynamodb”;

This gives you access to Amazon DynamoDB (DynamoDB).

Not only can you load and use individual AWS services, but you can also load and use only the service commands you need. See example code block:

import {
DynamoDBClient,
ListTablesCommand
} from @aws-sdk/client-dynamodb”

This gives you access to DynamoDB client and the ListTablesCommand command. So this package management with v3, we install only required package, and this comes very great benefit which is size of code decrease dramatically.

Lets give an example: If we compare to Listing Tables from DynamoDB with using V2 and V3.

We can say that, In V2, it using the whole package and The aws-sdk package adds about 40 MB to your application. If we Replacing var AWS = require(“aws-sdk”) with import {DynamoDB} from “@aws-sdk/client-dynamodb” reduces that overhead to about 3 MB. Restricting the import to just the DynamoDB client and ListTablesCommand command reduces the overhead to less than 100 KB.

So this is very important when it comes to manage your Lambda Function Concurrency and Throttling issues. Because cold start time will reduce dramatically if you use AWS SDK JavaScript V3 into your NodeJS Lambda functions. And also this is the main reason that most of enterprise companies choose NodeJS as a Lambda runtime to gain cold start benefit of this case.

How we can use AWS SDK V3 when interacting any AWS Services ?

In order to use AWS SDK V3 commands, you should import the commands and the required AWS Services package clients, and run the command using the .send method using the async/await pattern.

V3 provides a set of commands for each AWS Service package to enable you to perform operations for that AWS Service. After you install an AWS Service, you can browse the available commands in your project’s node-modules/@aws-sdk/client-PACKAGE_NAME/commands folder. You must import the commands you want to use.

For example, the following code loads the DynamoDB service, and the CreateTableCommand command.

import { DynamoDBClient, CreateTableCommand } from "@aws-sdk/client-dynamodb"; // ES Modules import
// const { DynamoDBClient, CreateTableCommand } = require("@aws-sdk/client-dynamodb"); // CommonJS import
const client = new DynamoDBClient(config);
const command = new CreateTableCommand(input);
const response = await client.send(command);

To call these commands in the recommend async/await pattern, use the following syntax.

CLIENT.send(new XXXCommand)

As you can see that we have understood that What’s new in The AWS SDK for JavaScript v3 and why we are using Version 3 ? and understand the Lambda function Cold Start Benefit when using V3.

Importance of ECMAScript 6 (ES6) on AWS SDK JS V3

As you know that we have decided to use AWS SDK JavaScript v3 when interacting AWS resources from our NodeJS Lambda functions. ES6 brings new syntax and new features to make your code more modern and readable, and do more. ES6 requires you use Node.js version 13.x or higher. we have already downloaded latest versions.

First of all we should understand that there are 2 type of module aproaches in NodeJS:

  • CommonJS
  • ES 6

And in some cases they are using different syntaxes. For example import statement:

  • // ES5 example
    const { DynamoDBClient, BatchExecuteStatementCommand } = require(“@aws-sdk/client-dynamodb”);
  • // ES6+ example
    import { DynamoDBClient, BatchExecuteStatementCommand } from “@aws-sdk/client-dynamodb”;

Using Node.js ES modules and top-level await in AWS Lambda

Actually, there is an article is written by Dan Fox, Principal Specialist Solutions Architect, Serverless. In this article Dan Fox is developing and example project and compares to NodeJS ES Modules with CommonJS modules with using top-level await statements.

And he explains how NodeJS ES Modules can help performance of Lambda functions. This is especially useful when used with Provisioned Concurrency to reduce cold start times.

AWS SDK for JavaScript Version 3

To summarize, with using ES6 modules and top-level await statements we can gain 50% performance advantage and its very crucial when it comes to manage Lambda Provisioned Concurrency.
As you can see that this is the best practice to use NodeJS runtime in Lambda functions and using ES6 modules provides serious performance benefits.

If we summarize that,

  • We will use NodeJS runtime when developing Lambda Functions
  • In Lambda functions we will interact with existing AWS resources for example perform CRUD operations interacting with DynamoDB
    in these cases we will use AWS SDK JavaScript V3.
  • We use V3 because it has very important advantage about decreasing code size and modularized structure — this help a lot performance of lambda cold start issues.
  • And lastly we use ECMAScript 6 (ES6) modules when developing our NodeJS lambda functions in order to use latest modern syntaxes.
    To do that we will add “type” : “module” from the package.json in your project environment.

Step by Step Design AWS Architectures w/ Course

I have just published a new course — AWS Lambda & Serverless — Developer Guide with Hands-on Labs.

In this course, we will learn almost all the AWS Serverless Services with all aspects. We are going to build serverless applications with using AWS Lambda, Amazon API Gateway, Amazon DynamoDB, Amazon Cognito, Amazon S3, Amazon SNS, Amazon SQS, Amazon EventBridge, AWS Step Functions, DynamoDB and Kinesis Streams. This course will be 100% hands-on, and you will be developing a real-world application with hands-on labs together and step by step.

Source Code

Get the Source Code from Serverless Microservices GitHub — Clone or fork this repository, if you like don’t forget the star. If you find or ask anything you can directly open issue on repository.

--

--

Mehmet Ozkaya
AWS Lambda & Serverless — Developer Guide with Hands-on Labs

Software Architect | Udemy Instructor | AWS Community Builder | Cloud-Native and Serverless Event-driven Microservices https://github.com/mehmetozkaya