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?
You can use the Node. js runtime to run TypeScript code 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.
@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.
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.
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
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 .
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.
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.
[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
The accepted answer is out of date. aws-lambda
now exports APIGatewayProxyEventV2
as well:
import { APIGatewayProxyEventV2 } from 'aws-lambda'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With