9

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?

Sammy
  • 3,395
  • 7
  • 49
  • 95

2 Answers2

7

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

import { APIGatewayProxyEventV2 } from 'aws-lambda'
nponeccop
  • 13,527
  • 1
  • 44
  • 106
3

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

Martin Löper
  • 6,471
  • 1
  • 16
  • 40
  • 1
    Looks like a PR has been [raised and merged already](https://github.com/DefinitelyTyped/DefinitelyTyped/pull/45205). – Mike Patrick Jun 02 '20 at 16:29