Use TypeScript for my Lambda features
Utilizing Typescript on your lambda can have some advantages equivalent to:
- Code completion trace whereas programming together with your IDE
- Compilation time errors checks
- 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:
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:
│ └── occasion.json
│ ├── app.js
│ ├── package deal.json
│ └── assessments
│ └── unit
│ └── test-handler.js
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
@kind/aws-lambda, which comprises AWS sorts for the code completion and typing checks
@kind/node, which is used for built-in sorts
Upon getting put in all dependencies you possibly can run:
This command ought to create a file known as
tsconfig.json like this one:
TypeScript compiler wants
Change the code
Okay, now that the configuration is completed and let’s change the code.
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 (
): Promise<APIGatewayProxyResult> => const queries = JSON.stringify(occasion.queryStringParameters); return
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
The aim of the operate is to cross an enter which is a
Individual object and show the knowledge contained as an output.
from "aws-lambda/set off/api-gateway-proxy";interface Individual
export const lambdaHandler = async (
): Promise<APIGatewayProxyResult> => const particular person: Individual = JSON.parse(occasion.physique); return
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.
package deal.json file and add this step into the script part:
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:
Description: Pattern Lambda Operate in Typescript
while you end run:
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.