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.

Project setup

  • Open Visual Studio Code;
  • View, Command Palette;
  • Type “aws: Create new SAM Application”;
Image for post
Image for post
Create AWS Lambda
  • Select the language (nodejs12.x);
Image for post
Image for post
Select AWS lambda language
  • AWS Sam Hello World;
  • Type a name for your project and that's it (mine is lambda)
Image for post
Image for post
Node Lambda project

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
Image for post
Image for post
Typescript config file

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:

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);
TypeScript lambda project structure
TypeScript lambda project structure
TypeScript lambda project structure

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.

Lambda function CodeUri
Lambda function CodeUri
Lambda function CodeUri

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.

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):

package.json
TypeScript lambda project structure
TypeScript lambda project structure
Project structure

Install packages

npm install

After all of this setup, it is time to start see something 😏

npm run compile
Image for post
Image for post
JavaScript files converter from TypeScript
sam local start-api
Image for post
Image for post

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

Image for post
Image for post

Seems that it is working 😎 but let’s try to invoke using our event created by SAM.

sam local invoke -e events/event.json
Image for post
Image for post
sam local invoke with event

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!

Set a breakpoint in your lambda function (TypeScript code)

On the left panel select Debug and then Start debugging (green button on top)

How to debug the lambda
How to debug the lambda
How to debug the lambda
Breakpoint set on TypeScript code
Breakpoint set on TypeScript code
Breakpoint set on TypeScript code

This debug configuration is coming from your launch.json. It was created by SAM when you set up your app.

Debug file launch.json
Debug file launch.json
Debug file launch.json

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
Lambda function ready to debug
Lambda function ready to debug
Lambda function ready to debug

Now follow the same process as before, select on the left panel debug button and then from dropdown select “Attach to SAM CLI”.

Image for post
Image for post
Debug 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

Get the Medium app