Create an AWS Lambda using Typescript
AWS Lambda does not natively support TypeScript. In this article, you are going to learn how to create a Typescript Lambda.
TypeScript is a superset of JavaScript, meaning all valid JavaScript code is also valid TypeScript code. As the name says Type+Script, adds optional static typing to the language.
TypeScript saves you time catching errors and providing fixes before you run code, which makes this a great framework to write your lambda function.
So you write code in TypeScript and then it’s converted in Node.js.
A Node.js lambda has better spin-up times than C# or Java.
Let's go for what really matters, write the lambda using Typescript.
Requirements
Project setup
- Open Visual Studio Code;
- View, Command Palette;
- Type “aws: Create new SAM Application”;
- Select the language (nodejs12.x);
- AWS Sam Hello World;
- Type a name for your project and that's it (mine is lambda)
Typescript setup
Navigate to your Hello-world folder and open the terminal there.
Install typescript globally:
npm i -g typescript
Check version installed:
tsc -v
Initialize typescript in the project (tsconfig.json will be created):
tsc --init
tsconfig.json file:
{"compilerOptions": {"module": "CommonJS","target": "ES2017","noImplicitAny": true,"preserveConstEnums": true,"outDir": "./built","sourceMap": true},"include": ["handler/**/*"],"exclude": ["node_modules", "**/*.spec.ts"]}
Basically, what we have here:
- sourceMap: Needed for debugging;
- target: JavaScript target runtime;
- module: CommonJS module specification is the standard used in Node.js for working with modules;
- esModuleInterop: With flag
esModuleInterop
we can import CommonJS modules in compliance withes6
modules spect; - outDir: output folder with JavaScript files after conversion from TypeScript;
- include: TypeScript code;
Project structure
- Create a folder “handler” inside “hello-world”;
- Create a new file “app.ts”;
- Add a TypeScript code:
- Delete app.js file and tests folder (I am not covering the tests in this article, but you can convert them to TypeScript);
Result
Adapt the template.yaml
If you noticed, we created an output folder for our TypeScript files. You need to change the path of CodeUri for our lambda function from the template.yaml. This path must point to JavaScript files and not TypeScript files.
Dependencies & devDependencies
Now let’s install all the devDependencies that we need.
Dependencies vs devDependencies
To specify the packages your project depends on, you must list them as "dependencies"
or "devDependencies"
in your package's package.json
file.
When you run npm install
, npm will download dependencies and devDependencies that are listed in package.json
"dependencies"
: Packages required by your application in production."devDependencies"
: Packages that are only needed for local development and testing.
For a deep understanding of this please check the official documentation.
DevDependencies
In order to use Typescript with AWS Lambda, you need to install the following dependencies:
npm install typescript @types/aws-lambda @types/node -save-dev
All the packages are self-explanatory. Basically, we are installing typescript, aws lambda types, and node types only in dev environment.
I encourage you to search all the packages in the npm repository and read about them.
Script
Edit package.json and add a new script command:
“compile”: “tsc”
Full package.json currently (I removed the test packages):
Install packages
npm install
After all of this setup, it is time to start see something 😏
npm run compile
Start API
sam local start-api
Now you can invoke your lambda using an app like Postman or simply open the URL in the browser.
http://127.0.0.1:3000/hello?a=1
Seems that it is working 😎 but let’s try to invoke using our event created by SAM.
sam local invoke -e events/event.json
API running locally :)
Debug
This is a really important step, Ok we can invoke the lambda what is nice, but can we debug the TypeScript code from lambda? well… yes!
Debug: Direct-Invoke type aws-sam
Set a breakpoint in your lambda function (TypeScript code)
On the left panel select Debug and then Start debugging (green button on top)
This debug configuration is coming from your launch.json. It was created by SAM when you set up your app.
Debug: Attach to SAM CLI
This is another strategy, you can invoke your lambda and then debug it.
Add the following JSON code to your launch.json. It was added a new way to debug your lambda code, attach to SAM CLI.
Invoke your lambda:
sam local invoke -e events/event.json — debug-port 8065 HelloWorldFunction
Now follow the same process as before, select on the left panel debug button and then from dropdown select “Attach to SAM CLI”.
And that’s it 🐐
Now if you want you can deploy your lambda
sam deploy --guided
Conclusion
In this article, you learned how to create, invoke and debug and TypeScript lambda. Actually, it was a little bit challenged in the beginning and I was not finding a good solution, I saw some using Webpack, but in the end, I wasn’t able to debug it. Well, keep it simple, with this article you don’t need any external dependency.
Let me know you if you have any doubt/comment 🐐
I know you want the source code 👌
References
- https://aws.amazon.com/serverless/sam/
- https://code.visualstudio.com/
- https://www.typescriptlang.org/docs/handbook/tsconfig-json.html
- https://docs.npmjs.com/specifying-dependencies-and-devdependencies-in-a-package-json-file
- https://nodejs.org/en/
- https://hub.docker.com/search/?type=edition&offering=community
- https://kangax.github.io/compat-table/es6/
- https://github.com/jafreitas90/AWS/tree/master/Lambda/typescript/lambda