Lädt...


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


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

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
}

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
}

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'

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
}

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
}

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'),
})

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!'),
            };
          };

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.

...

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


📈 75.54 Punkte
🔧 Programmierung

🔧 Tìm Hiểu Về RAG: Công Nghệ Đột Phá Đang "Làm Mưa Làm Gió" Trong Thế Giới Chatbot


📈 35.78 Punkte
🔧 Programmierung

🔧 Enhancing AWS Lambda with AWS Lambda Powertools: A Complete Guide to CRUD Operations in DynamoDB


📈 35.5 Punkte
🔧 Programmierung

🔧 Spring Boot 3 application on AWS Lambda - Part 6 Develop application with AWS Lambda Web Adapter


📈 35.5 Punkte
🔧 Programmierung

🔧 Spring Boot 3 application on AWS Lambda - Part 5 Introduction to AWS Lambda Web Adapter


📈 35.5 Punkte
🔧 Programmierung

🔧 Connecting AWS Lambda with Amazon RDS using AWS CDK and Node.js


📈 30.56 Punkte
🔧 Programmierung

🔧 Reducing Cold Starts on AWS Lambda with Java Runtime - Future Ideas about SnapStart, GraalVM and Co


📈 29.17 Punkte
🔧 Programmierung

🔧 How to optimize your lambda functions with AWS Lambda power tuning


📈 28.8 Punkte
🔧 Programmierung

🔧 Day 22 of 100 Days of Cloud: Mastering AWS Lambda and Lambda Layers


📈 28.8 Punkte
🔧 Programmierung

🔧 Turbocharge your Lambda Functions with AWS Lambda Powertools for Python


📈 28.8 Punkte
🔧 Programmierung

🔧 Lambda Internals: Why AWS Lambda Will Not Help With Machine Learning


📈 28.8 Punkte
🔧 Programmierung

🔧 Supercharge Your AWS Lambda Game With Lambda Powertools


📈 28.8 Punkte
🔧 Programmierung

📰 Get started quickly with AWS Trainium and AWS Inferentia using AWS Neuron DLAMI and AWS Neuron DLC


📈 26.82 Punkte
🔧 AI Nachrichten

🔧 Deploy nest app to AWS Lambda using cloud-formation stack and AWS CDK


📈 24.46 Punkte
🔧 Programmierung

🔧 Deploy Your First Web App on AWS with AWS Amplify, Lambda, DynamoDB and API Gateway


📈 24.46 Punkte
🔧 Programmierung

🔧 Parse UserParameters sent from AWS CodePipeline to AWS Lambda in Go


📈 24.46 Punkte
🔧 Programmierung

🔧 AWS LetsEncrypt Lambda or Why I Wrote a Custom TLS Provider for AWS Using OpenTofu and Go


📈 24.46 Punkte
🔧 Programmierung

🔧 AWS ECS vs. AWS Lambda: Top 5 Main Differences


📈 24.46 Punkte
🔧 Programmierung

🔧 Deploy Puppeteer and Chrome on AWS Lambda With Layers and AWS CDK


📈 24.46 Punkte
🔧 Programmierung

🔧 Hacking AWS Account via AWS Lambda SSRF


📈 24.46 Punkte
🔧 Programmierung

🔧 A tale of invocation - Using AWS Lambda to transfer files from AWS S3 to Azure Blob Storage


📈 24.46 Punkte
🔧 Programmierung

🔧 Automating AWS EC2 and RDS Instance Management with AWS Lambda


📈 24.46 Punkte
🔧 Programmierung

🔧 Building an AI powered and Serverless meal planner with OpenAI, AWS Step functions, AWS Lambda and CDK


📈 24.46 Punkte
🔧 Programmierung

🔧 Deploying AWS Lambda Functions with API Gateway to DynamoDB using AWS SAM on LocalStack


📈 24.46 Punkte
🔧 Programmierung

🔧 Build AWS Lambda Layers with AWS CDK


📈 24.46 Punkte
🔧 Programmierung

🔧 Spring Boot 3 application on AWS Lambda - Part 9 Develop application with Spring Cloud Function AWS


📈 24.46 Punkte
🔧 Programmierung

🔧 AWS Inspector for AWS Lambda


📈 24.46 Punkte
🔧 Programmierung

🔧 Serverless Application using AWS Lambda ,Api Gateway,AWS Amplify


📈 24.46 Punkte
🔧 Programmierung

matomo