AWS Lambda using Typescript | Better Programming

Use TypeScript for my Lambda features

Hello everybody, immediately I want to speak about the way to write a easy AWS Lambda utilizing TypeScript. We all know that NodeJs is a standard language to put in writing a Lambda operate, however JavaScript may get irritating as a result of lack of kind checking and debugging your code.

Utilizing Typescript on your lambda can have some advantages equivalent to:

  1. Code completion trace whereas programming together with your IDE
  2. Compilation time errors checks
  3. Information concerning the construction of your enter and ouputs

Okay, sufficient discuss. Let’s bounce to the instance!

Earlier than you start making a pattern lambda challenge utilizing SAM CLI software:

sam init

In your terminal you need to see one thing like this:

Choose the selection primary after which, one once more
When the method is completed you need to have a structured folder like this:

├── occasions
│ └── occasion.json
├── hello-world
│ ├── app.js
│ ├── package deal.json
│ └── assessments
│ └── unit
│ └── test-handler.js
└── template.yaml

To start with, start with renaming hello-word folder to src and delete assessments folder (we is not going to use it for this tutorial). If you end, go inside your terminal and begin to add some dependencies.

Begin with AWS dependency utilizing the command:

npm set up aws-sdk

After that it’s time to typescript dependencies:

npm set up --save-dev typescript @sorts/aws-lambda @sorts/node
  1. typescript
  2. @kind/aws-lambda, which comprises AWS sorts for the code completion and typing checks
  3. @kind/node, which is used for built-in sorts

Upon getting put in all dependencies you possibly can run:

tsc --init

This command ought to create a file known as tsconfig.json like this one:

"module": "CommonJS",
"goal": "ES2017",
"noImplicitAny": true,
"preserveConstEnums": true,
"outDir": "./constructed",
"sourceMap": true

TypeScript compiler wants tsconfig.json to determine the way to rework TypeScript to JavaScript.

Change the code

Okay, now that the configuration is completed and let’s change the code.
Rename app.js to app.ts and delete the code inside.

Then, write the Lamba handler like this:

from "aws-lambda/set off/api-gateway-proxy";
export const lambdaHandler = async (
occasion: APIGatewayProxyEvent
): Promise<APIGatewayProxyResult> =>
const queries = JSON.stringify(occasion.queryStringParameters); return
statusCode: 200,
physique: `Queries: $queries`

This can be a easy handler that reads the question string parameters of my operate and shows them for output. Cool proper?

Okay, however not let’s use some TypeScript options to create a Individual interface:

interface Individual 
identify: string
surname: string
age: quantity

The aim of the operate is to cross an enter which is a Individual object and show the knowledge contained as an output.

Let’s code:

from "aws-lambda/set off/api-gateway-proxy";
interface Individual
identify: string
surname: string
age: quantity
export const lambdaHandler = async (
occasion: APIGatewayProxyEvent
): Promise<APIGatewayProxyResult> =>
const particular person: Individual = JSON.parse(occasion.physique); return
statusCode: 200,
physique: JSON.stringify('Individual' : particular person)

Deploy and check

Now it’s time to check the Lambda operate. To start with, it’s important to compile the Lambda and verify if every thing works.

Go to package deal.json file and add this step into the script part:

"compile": "tsc"

and execute typing:

npm run compile

If every thing works, the Lambda operate is compiled. Let’s check! Go to template.yml and alter it to fit your app. Mine is like this:

AWSTemplateFormatVersion: '2010-09-09'
Remodel: AWS::Serverless-2016-10-31
Description: Pattern Lambda Operate in Typescript
Timeout: 3
Sort: AWS::Serverless::Operate
CodeUri: src/constructed
Handler: app.lambdaHandler
Runtime: nodejs14.x
Sort: Api
Path: /hey
Methodology: get

while you end run:

sam construct

If no errors are displayed let’s modify the file occasion.json positioned into occasions folder and change the sector physique on this manner:

"physique": ""identify": "Bob", "surname":"Brown","age":21",

Run your Lambda operate in your native machine:

sam native invoke -e occasions/occasion.json

If every thing works you need to see in your terminal output like this:

Your lambda is now able to be deployed to AWS.

The pattern code could be discovered at:

I discover actually easy and enjoyable utilizing TypeScript for my Lambda features. It helps me to keep away from errors — like writing methodology names and looking in tons of API doc. And the compiler helps me to put in writing the right sorts and keep away from potentials issues earlier than deploying.

More Posts