Building Lambda functions with TypeScript
You can use the Node.js runtime to run TypeScript code in Amazon Lambda. Because Node.js doesn't run TypeScript code natively, you must first transpile your TypeScript code into JavaScript. Then, use the JavaScript files to deploy your function code to Lambda. Your code runs in an environment that includes the Amazon SDK for JavaScript, with credentials from an Amazon Identity and Access Management (IAM) role that you manage.
Lambda supports the following Node.js runtimes.
Node.js | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Name | Identifier | SDK | Operating system | Architectures | Deprecation (Phase 1) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Node.js 18 |
|
3.188.0 |
Amazon Linux 2 |
x86_64, arm64 |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Node.js 16 |
|
2.1374.0 |
Amazon Linux 2 |
x86_64, arm64 |
Mar 11, 2024 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Node.js 14 |
|
2.1374.0 |
Amazon Linux 2 |
x86_64, arm64 |
Nov 27, 2023 |
Setting up a TypeScript development environment
Use a local integrated development environment (IDE), text editor, or Amazon Cloud9 to write your TypeScript function code. You can’t create TypeScript code on the Lambda console.
To transpile your TypeScript code, set up a compiler such as esbuildtsc
) , which is bundled with the TypeScript distribution
When using esbuild, consider the following:
-
There are several TypeScript caveats
. -
You must configure your TypeScript transpilation settings to match the Node.js runtime that you plan to use. For more information, see Target
in the esbuild documentation. For an example of a tsconfig.json file that demonstrates how to target a specific Node.js version supported by Lambda, refer to the TypeScript GitHub repository . -
esbuild doesn’t perform type checks. To check types, use the
tsc
compiler. Runtsc -noEmit
or add a"noEmit"
parameter to your tsconfig.json file, as shown in the following example. This configurestsc
to not emit JavaScript files. After checking types, use esbuild to convert the TypeScript files into JavaScript.
Example tsconfig.json
{ "compilerOptions": { "target": "es2020", "strict": true, "preserveConstEnums": true, "noEmit": true, "sourceMap": false, "module":"commonjs", "moduleResolution":"node", "esModuleInterop": true, "skipLibCheck": true, "forceConsistentCasingInFileNames": true, "isolatedModules": true, }, "exclude": ["node_modules", "**/*.test.ts"] }
Topics
- Amazon Lambda function handler in TypeScript
- Deploy transpiled TypeScript code in Lambda with .zip file archives
- Deploy transpiled TypeScript code in Lambda with container images
- Amazon Lambda context object in TypeScript
- Amazon Lambda function logging in TypeScript
- Amazon Lambda function testing in TypeScript
- Amazon Lambda function errors in TypeScript
- Tracing TypeScript code in Amazon Lambda