Use aws-lambda types, it have types for most of the events.
Example handlers:
import { SQSHandler, SNSHandler, APIGatewayProxyHandler } from 'aws-lambda';
export const sqsHandler: SQSHandler = async (event, context) => {
}
export const snsHandler: SNSHandler = async (event, context) => {
}
export const apiV2Handler: APIGatewayProxyHandler = async (event, context) => {
return {
body: 'Response body',
statusCode: 200
}
}
Answer from nirvana124 on Stack Overflownode.js - Node AWS SDK v3: Types for `event` and `context` arguments in Lambda functions? - Stack Overflow
How do I declare a TypeScript AWS Lambda handler for Lambda Function URLs? - Stack Overflow
Which type do I return with AWS lambda responses in typescript to suite AWS APIGateway Method Response? - Stack Overflow
What fields/properties do 'event' and 'context' have in a Python Lambda invoked by API Gateway?
Since November 2023 there is also LambdaFunctionURLHandler type in the @types/aws-lambda npm package. Here's the source on Github.
To get type checking in TypeScript use:
import { LambdaFunctionURLHandler } from 'aws-lambda';
export const handler: LambdaFunctionURLHandler = async (event) => {
// handler code
}
You can also specify the type of the handler like this in JavaScript with JSDoc:
/** @type import('aws-lambda').LambdaFunctionURLHandler */
export const handler = async (event) => {
// handler code
}
which especially useful if you are editing JS code in a type-aware code editor, but also uploading the Lambda code directly to AWS without transpiling.
tldr; The type of Lambda function URL event is the same as the API Gateway event.
Quoting from the AWS Lambda developer guide
When a client calls your function URL, Lambda maps the request to an event object before passing it to your function. Your function's response is then mapped to an HTTP response that Lambda sends back to the client through the function URL.
The request and response event formats follow the same schema as the Amazon API Gateway payload format version 2.0.
The @types/aws-lambda package provides types for using TypeScript inside of an AWS Lambda function. For AWS Lambda functions invoked using API Gateway's Lambda Proxy integration type take the following steps:
Install the package
$ yarn add --dev @types/aws-lambda
Or if you prefer npm:
$ npm i --save-dev @types/aws-lambda
Then in your handler file:
import { APIGatewayProxyEvent, Context, APIGatewayProxyResult } from "aws-lambda"
export async function hello (event: APIGatewayProxyEvent, context: Context): Promise<APIGatewayProxyResult> {
return {
statusCode: 200,
body: JSON.stringify({
message: 'Hello world',
input: event,
})
}
}
After days of research I found the answer so close ;)
you return Promise<APIGateway.MethodResponse>
import { APIGateway } from "aws-sdk";
export async function readCollection (event, context, callback): Promise<APIGateway.MethodResponse> {
console.log(event); // Contains incoming request data (e.g., query params, headers and more)
const data = [
{
id: "a7b5bf50-0b5b-11e9-bc65-6bfc39f23288",
name: "some thing",
uri: `/notifications/a7b5bf50-0b5b-11e9-bc65-6bfc39f23288`
}
]
const response = {
statusCode: "200",
headers: {
},
body: JSON.stringify({
status: "ok",
data: data
})
};
return response;
};