Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript for AWS HTTP API Gateway Lambda Handlers

Currently I use the following to type the lambda functions for RestApi:

import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';
export const handler = async (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => {}

This doesn't correctly work with the new AWS Gateway HTTP API, where the HTTP method can be fetched using event.requestContext.http.method.

Are there other types that I should use?

like image 495
Sammy Avatar asked May 26 '20 14:05

Sammy


People also ask

Can you use TypeScript in AWS Lambda?

You can use the Node. js runtime to run TypeScript code in AWS Lambda.

How do I specify a handler in AWS Lambda?

This function handler name reflects the function name ( lambda_handler ) and the file where the handler code is stored ( lambda_function.py ). To change the function handler name in the Lambda console, on the Runtime settings pane, choose Edit.

How to convert TypeScript to JavaScript in AWS Lambda?

@type/aws-lambda, which contains AWS types for the code completion and typing checks This command should create a file called tsconfig.json like this one: TypeScript compiler needs tsconfig.json to figure out how to transform TypeScript to JavaScript. Ok, now that the configuration is finished and let’s change the code.

What is @AWS-SDK and @types/AWS- Lambda?

aws-sdk: depending on whether you are using AWS SDK in your lambda @types/aws-lambda: this is very vital for the code completion and typing checks TypeScript compiler needs tsconfig.json to figure out how to transform TypeScript to JavaScript.

Why use typescript to write Lambda handlers?

We mean to get que ry StringParameters, but the queries ends up undefined as a result of carelessness. We want to utilize TypeScript to write lambda handlers. With TypeScript, we will have the following benefits: Vanilla code completion hints while programmingCompilation time error checks to avoid redundant deployments

How do I work with AWS Lambda proxy integrations?

Working with AWS Lambda proxy integrations for HTTP APIs. A Lambda proxy integration enables you to integrate an API route with a Lambda function. When a client calls your API, API Gateway sends the request to the Lambda function and returns the function's response to the client. For examples of creating an HTTP API, see Creating an HTTP API .


Video Answer


2 Answers

Are there other types that I should use?

I do not think so. The types are available via DefinitelyTyped. [1] Looking through some of the issues regarding the "aws-lambda" types, you will notice that the API Gateway types are not updated frequently. [2]

Furthermore, the payload format version changed for API Gateway, see [3]:

The payload format version specifies the format of the data that API Gateway sends to a Lambda integration, and how API Gateway interprets the response from Lambda. If you don't specify a payload format version, the AWS Management Console uses the latest version by default. If you create a Lambda integration by using the AWS CLI, AWS CloudFormation, or an SDK, you must specify a payloadFormatVersion. The supported values are 1.0 and 2.0.

I guess you are using the latest version which is 2.0. Version 2.0 provides the HTTP method as property requestContext.http.method.

Version 1.0 provides the HTTP method as property requestContext.httpMethod.

Solution

You can either 1.) write typings for the new payload format version and submit them to DefinitelyTyped via PR for package "@types/aws-lambda" or 2.) set your API Gateway to use version 1.0.

Honestly, I do not know if using the payload version 1.0 is possible for HTTP APIs. Maybe AWS is enforcing the latest version on the new APIs since there is no need to support the older format.

References

[1] https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/aws-lambda/trigger/api-gateway-proxy.d.ts
[2] https://github.com/DefinitelyTyped/DefinitelyTyped/issues/38720#issuecomment-586051966
[3] https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html

like image 134
Martin Löper Avatar answered Oct 18 '22 09:10

Martin Löper


The accepted answer is out of date. aws-lambda now exports APIGatewayProxyEventV2 as well:

import { APIGatewayProxyEventV2 } from 'aws-lambda'
like image 20
nponeccop Avatar answered Oct 18 '22 08:10

nponeccop