Search code examples
aws-sdk-js

What is the Lambda handler Signature for AWS SDK V3 with TypeScirpt


I'm updating an existing project from V2 to V3 of the AWS SDK for JavaScript and also moving our usage from JavaScript to TypeScript.

I'm struggling to define strongly typed handlers for the Lamdas.

The examples I've found are similar to this. I'm guessing that they're using V2 of the SDK.

export const lambdaHandler = async (event: APIGatewayEvent, context: Context): Promise<APIGatewayProxyResult> => {
    return {
        statusCode: 200,
        body: JSON.stringify({
            message: 'hello world',
        }),
    };
};

I've had a look through the V3 source for classes similar to APIGatewayEvent, Context and APIGatewayProxyResult but nothing jumps out at me as filling those roles.

Can someone please tell me how to strongly type these signatures?


Solution

  • AWS introduced the @types/aws-lambda package for use with there Javascript V3 SDK. Here is a typescript example, for your exact use case.

    import { APIGatewayProxyHandler, APIGatewayEvent, APIGatewayProxyResult } from "aws-lambda";
    
    export const handler: APIGatewayProxyHandler = async (event: APIGatewayEvent): Promise<APIGatewayProxyResult> => {
        return {statusCode: 200, body: event.body ?? ''};
    }