You can run JavaScript code with Node.js in AWS Lambda. Lambda provides runtimes for Node.js that run your code to process events. Your code runs in an environment that includes the AWS SDK for JavaScript, with credentials from an AWS Identity and Access Management (IAM) role that you manage.
Lambda supports the following Node.js runtimes.
Node.js runtimes
Name | Identifier | SDK for JavaScript | Operating system | Architectures |
---|---|---|---|---|
Node.js 14 | nodejs14.x |
2.1001.0 | Amazon Linux 2 | x86_64, arm64 |
Node.js 12 | nodejs12.x |
2.1001.0 | Amazon Linux 2 | x86_64, arm64 |
Node.js 10 | nodejs10.x |
2.1001.0 | Amazon Linux 2 | x86_64 |
Note
For end of support information about Node.js 10, see Runtime support policy.
Lambda functions use an execution role to get permission to write logs to Amazon CloudWatch Logs, and to access other services and resources. If you don't already have an execution role for function development, create one.
To create an execution role
-
Open the roles page in the IAM console.
-
Choose Create role.
-
Create a role with the following properties.
- Trusted entity – Lambda.
- Permissions – AWSLambdaBasicExecutionRole.
- Role name – lambda-role.
The AWSLambdaBasicExecutionRole policy has the permissions that the function needs to write logs to CloudWatch Logs.
You can add permissions to the role later, or swap it out for a different role that's specific to a single function.
To create a Node.js function
-
Open the Lambda console.
-
Choose Create function.
-
Configure the following settings:
- Name – my-function.
- Runtime – Node.js 14.x.
- Role – Choose an existing role.
- Existing role – lambda-role.
-
Choose Create function.
-
To configure a test event, choose Test.
-
For Event name, enter test.
-
Choose Save changes.
-
To invoke the function, choose Test.
The console creates a Lambda function with a single source file named index.js
. You can edit this file and add more files in the built-in code editor. To save your changes, choose Save. Then, to run your code, choose Test.
Note
The Lambda console uses AWS Cloud9 to provide an integrated development environment in the browser. You can also use AWS Cloud9 to develop Lambda functions in your own environment. For more information, see Working with Lambda Functions in the AWS Cloud9 user guide.
The index.js
file exports a function named handler
that takes an event object and a context object. This is the handler function that Lambda calls when the function is invoked. The Node.js function runtime gets invocation events from Lambda and passes them to the handler. In the function configuration, the handler value is index.handler
.
When you save your function code, the Lambda console creates a .zip file archive deployment package. When you develop your function code outside of the console (using an SDE) you need to create a deployment package to upload your code to the Lambda function.
Note
To get started with application development in your local environment, deploy one of the sample applications available in this guide's GitHub repository.
blank-nodejs – A Node.js function that shows the use of logging, environment variables, AWS X-Ray tracing, layers, unit tests and the AWS SDK.
nodejs-apig – A function with a public API endpoint that processes an event from API Gateway and returns an HTTP response.
rds-mysql – A function that relays queries to a MySQL for RDS Database. This sample includes a private VPC and database instance configured with a password in AWS Secrets Manager.
efs-nodejs – A function that uses an Amazon EFS file system in a Amazon VPC. This sample includes a VPC, file system, mount targets, and access point configured for use with Lambda.
list-manager – A function processes events from an Amazon Kinesis data stream and update aggregate lists in Amazon DynamoDB. The function stores a record of each event in a MySQL for RDS Database in a private VPC. This sample includes a private VPC with a VPC endpoint for DynamoDB and a database instance.
error-processor – A Node.js function generates errors for a specified percentage of requests. A CloudWatch Logs subscription invokes a second function when an error is recorded. The processor function uses the AWS SDK to gather details about the request and stores them in an Amazon S3 bucket.
The function runtime passes a context object to the handler, in addition to the invocation event. The context object contains additional information about the invocation, the function, and the execution environment. More information is available from environment variables.
Your Lambda function comes with a CloudWatch Logs log group. The function runtime sends details about each invocation to CloudWatch Logs. It relays any logs that your function outputs during invocation. If your function returns an error, Lambda formats the error and returns it to the invoker.
Node.js has a unique event loop model that causes its initialization behavior to be different from other runtimes. Specifically, Node.js uses a non-blocking I/O model that supports asynchronous operations. This model allows Node.js to perform efficiently for most workloads. For example, if a Node.js function makes a network call, that request may be designated as an asynchronous operation and placed into a callback queue. The function may continue to process other operations within the main call stack without getting blocked by waiting for the network call to return. Once the network call is returned, its callback is executed and then removed from the callback queue.
Some initialization tasks may run asynchronously. These asynchronous tasks are not guaranteed to complete execution prior to an invocation. For example, code that makes a network call to fetch a parameter from AWS Parameter Store may not be complete by the time Lambda executes the handler function. As a result, the variable may be null during an invocation. To avoid this, ensure that variables and other asynchronous code are fully initialized before continuing with the rest of the function’s core business logic.
Starting with Node 14, you can guarantee completion of asynchronous initialization code prior to handler invocations by designating your code as an ES module, and using top-level await. Packages are designated as CommonJS modules by default, meaning you must first designate your function code as an ES module to use top-level await. You can do this in two ways: specifying the type
as module
in the function's package.json
file, or by using the .mjs file name extension.
In the first scenario, your function code treats all .js files as ES modules, while in the second scenario, only the file you specify with .mjs is an ES module. You can mix ES modules and CommonJS modules by naming them .mjs and .cjs respectively, as .mjs files are always ES modules and .cjs files are always CommonJS modules.
For more information and an example, see Using Node.js ES Modules and Top-Level Await in AWS Lambda.
Topics
- Node.js initialization
- AWS Lambda function handler in Node.js
- Deploy Node.js Lambda functions with .zip file archives
- Deploy Node.js Lambda functions with container images
- AWS Lambda context object in Node.js
- AWS Lambda function logging in Node.js
- AWS Lambda function errors in Node.js
- Instrumenting Node.js code in AWS Lambda