Skip to content

๐Ÿš€ Using top-level await in AWS Lambda with TypeScript, esbuild and Serverless Framework

Notifications You must be signed in to change notification settings

oieduardorabelo/top-level-await-in-aws-lamba-with-typescript

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

7 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿš€ Top-level await in AWS Lamba with TypeScript

Articles

Summary

This repository demonstrates Node.js ES modules and top-level await in AWS Lambda while developing your code in TypeScript.

How does that work?

We use serverless-esbuild to transpile our TypeScript code to JavaScript.

The secret sauce is to configure esbuild to output Node.js ES modules files with .mjs extension with the following esbuild options:

format: "esm"
outputFileExtension: ".mjs"
platform: "node"
target: "esnext"

With that in place, you can have a fully typed AWS Lambda code in TypeScript and take advantage of executing await outside the handler function, improving the response time for the service consumers.

As Dan Fox (Principal Specialist Solutions Architect Serverless) explained in his article, we can see around ~45% performance improvements:

https://aws.amazon.com/blogs/compute/using-node-js-es-modules-and-top-level-await-in-aws-lambda/

๐Ÿ“ธ In The Media