DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Cover image for AWS Lambda support Node.js 18 now. Should we update the version of Node.js in the Lambda runtime?
Binh Bui for AWS Community Builders

Posted on • Originally published at codewithyou.com

AWS Lambda support Node.js 18 now. Should we update the version of Node.js in the Lambda runtime?

Recently, AWS Lambda announced that it supports Node.js 18 and it is available in all AWS regions. This is a big news for Node.js developers. It means that we can use the latest version of Node.js in AWS Lambda. However, should we update the version of Node.js in the Lambda runtime? Let's find out.

Why we should update the version of Node.js in the Lambda runtime?

The reason is simple. The new version of Node.js has new features and bug fixes. We should always use the latest version of Node.js in the Lambda runtime. Besides, the new version of Node.js is faster and more secure. It is a good idea to update the version of Node.js in the Lambda runtime.

Some of benefits of using the latest version of Node.js in the Lambda runtime:

Support ES2021 features

Node.js 18 supports ES2021 features. We can use them in the Node.js runtime environment. For example, we can use the optional chaining operator (?.) and the nullish coalescing operator (??).

// index.mjs
export async function handler(event, context) {
  const data = {
    foo: {
      bar: 'baz',
    },
  }
  const value = data?.foo?.bar ?? 'default'
  return value
}
Enter fullscreen mode Exit fullscreen mode

Support top-level await

Node.js 18 supports top-level await. We can use it in the Node.js runtime environment. For example, we can use it to make HTTP requests. I really like this feature because it really useful when we want to re-use same initialization code in multiple Lambda invocations. We can use top-level await to initialize the code once and then re-use it in multiple Lambda invocations.

// index.mjs
const response = await fetch('https://example.com')
const data = await response.json()

export async function handler(event, context) {
  return data
}
Enter fullscreen mode Exit fullscreen mode

AWS SDK v3 support in the Node.js runtime environment

Lambda supports AWS SDK v3 in the Node.js runtime environment. This enables us to use the new AWS SDK v3 in the Node.js runtime environment. We can use the new AWS SDK v3 to make AWS API calls in the Node.js runtime environment. For example, we can use the new AWS SDK v3 to make DynamoDB API calls in the Node.js runtime environment without include the AWS SDK v3 in the Lambda deployment package.

// index.mjs
import { DynamoDBClient, PutItemCommand } from '@aws-sdk/client-dynamodb'
Enter fullscreen mode Exit fullscreen mode

This is a big news for Node.js developers. We can use the new AWS SDK v3 to make AWS API calls in the Node.js runtime environment. We don’t need to use the old AWS SDK v2 anymore. This especially useful when creating functions in the AWS console or inline code in the CloudFormation template.

Node.js 18 language updates

The Lambda Node.js 18 runtime also enables you to take advantage of new Node.js 18 language features. This includes improved performance for class fields and private class methods, JSON import assertions, and experimental features such as the Fetch API, Test Runner module, and Web Streams API.

JSON import assertion

The import assertions feature allows module import statements to include additional information alongside the module specifier. Now the following code is valid:

// index.mjs

// static import with JSON import assertion
import fooData from './foo.json' assert { type: 'json' }

// dynamic import with JSON import assertion
const { default: barData } = await import('./bar.json', { assert: { type: 'json' } })

export const handler = async (event) => {
  console.log(fooData)
  // logs data in foo.json file
  console.log(barData)
  // logs data in bar.json file

  const response = {
    statusCode: 200,
    body: JSON.stringify('Hello from Lambda!'),
  }
  return response
}
Enter fullscreen mode Exit fullscreen mode

Support fetch API in the Node.js runtime environment

The fetch API is a standard API for making HTTP requests in the browser. It is also available in Node.js 16 and above. We can use it to make HTTP requests in the Node.js runtime environment. It is a standard API and we don’t need to install any third-party libraries to use it.
In a previous article, I wrote about how to use the fetch API in the Node.js runtime environment. You can read it here.

// index.mjs
export async function handler(event, context) {
  const response = await fetch('https://example.com')
  const data = await response.json()
  return data
}
Enter fullscreen mode Exit fullscreen mode

How to update the version of Node.js in the Lambda runtime?

We can update the version of Node.js in the Lambda runtime by using the AWS CDK. The following code shows how to update the version of Node.js in the Lambda runtime.

import * as lambda from '@aws-cdk/aws-lambda'

const lambdaFunction = new lambda.Function(this, 'LambdaFunction', {
  runtime: lambda.Runtime.NODEJS_18_X,
  handler: 'index.handler',
  code: lambda.Code.fromAsset('lambda'),
})
Enter fullscreen mode Exit fullscreen mode

Or we can update the version of Node.js in the Lambda runtime by using the AWS CloudFormation. The following code shows how to update the version of Node.js in the Lambda runtime.

Resources:
  LambdaFunction:
    Type: AWS::Lambda::Function
    Properties:
      Runtime: nodejs18.x
      Handler: index.handler
      Code:
        ZipFile: |
          exports.handler = async (event) => {
            return {
              statusCode: 200,
              body: JSON.stringify('Hello from Lambda!'),
            };
          };
Enter fullscreen mode Exit fullscreen mode

Conclusion

Node.js 18 is now supported by Lambda. We should consider updating the version of Node.js to Node.js 18 in the Lambda runtime as soon as possible. For existing Node.js functions, review your code for compatibility with Node.js 18, including deprecations, then migrate to the new runtime by changing the function’s runtime configuration to nodejs18.x.

Thanks for reading. If you have any questions, please leave a comment below. If you like this article, please share it with your friends. You can also follow me on Twitter and LinkedIn.

Top comments (1)

Collapse
 
emil profile image
Emil • Edited on

Prior to Node 18 you could use AWS SDK v3 as well as long as you bundle it with the lambda code. Whats new is that Node 18 lambda runtime provides those packages like it has done it with the prior runtimes and AWS SDK v2.

πŸ” If you came here from Google, why not take a look at some of our top posts?