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 OverflowAmazon Web Services
docs.aws.amazon.com › aws lambda › developer guide › building lambda functions with typescript › using the lambda context object to retrieve typescript function information
Using the Lambda context object to retrieve TypeScript function information - AWS Lambda
When Lambda runs your function, it passes a context object to the handler. This object provides methods and properties that provide information about the invocation, function, and execution environment. To enable type checking for the context object, you must add the @types/aws-lambda
Amazon Web Services
docs.aws.amazon.com › aws lambda › developer guide › building lambda functions with typescript
Building Lambda functions with TypeScript - AWS Lambda
import { Context, S3Event, APIGatewayProxyEvent } from 'aws-lambda'; export const handler = async (event: S3Event, context: Context) => { // Function code }; The import ... from 'aws-lambda' statement imports the type definitions. It does not import the aws-lambda npm package, which is an unrelated third-party tool. For more information, see aws-lambda ... when using your own custom type definitions. For an example function that defines its own type for an event object, see Example TypeScript Lambda function code.
node.js - Node AWS SDK v3: Types for `event` and `context` arguments in Lambda functions? - Stack Overflow
Sign up to request clarification or add additional context in comments. ... Does this answer imply that the new v3 aws-sdk does not actually provide these types and that the correct solution is to rely on this independent library? 2022-05-16T21:58:58.797Z+00:00 ... @types/aws-lambda is pretty much active so I guess this is still the way to go in 2022. 2022-06-27T06:49:49.23Z+00:00 ... Yes, event the official docs mention to install this library: docs.aws.amazon.com/lambda/latest/dg/typescript... More on stackoverflow.com
amazon web services - How do you elegantly import AWS - Lambda in Typescript? - Stack Overflow
I am building a typescript project on aws lambda. As aws-sdk comes with type definitions already I would expect it also to hold a definition for aws lambda. But I seem to have to install @types/aws- More on stackoverflow.com
Which type do I return with AWS lambda responses in typescript to suite AWS APIGateway Method Response? - Stack Overflow
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: ... import { APIGatewayProxyEvent, Context, APIGatewayProxyResult } from "aws-lambda" ... More on stackoverflow.com
Testing Lambdas Locally - Need Guidance
I'm currently working on a project that involves AWS Lambdas, and I'm looking for some guidance on how to test them locally to speed up my iteration. I want to ensure that everything works smoothly before pushing changes to my AWS environment. ... For additional context, my tech stack is just ... More on reddit.com
Amazon Web Services
docs.aws.amazon.com › aws lambda › developer guide › building lambda functions with typescript › define lambda function handler in typescript
Define Lambda function handler in TypeScript - AWS Lambda
The handler accepts the following arguments: event: Contains the input data passed to your function. context: Contains information about the invocation, function, and execution environment. For more information, see Using the Lambda context object to retrieve TypeScript function information.
Typescript Practices
omakoleg.github.io › typescript-practices › content › lambda.html
AWS Lambda | Typescript Practices
import { DynamoDBStreamHandler, DynamoDBStreamEvent } from "aws-lambda"; export const handleStreamEvent: DynamoDBStreamHandler = async ( event: DynamoDBStreamEvent ): Promise<void> => { await Promise.resolve(1); console.log("Process db event"); }; ... export type APIGatewayProxyHandler = Handler< APIGatewayProxyEvent, APIGatewayProxyResult >; export type DynamoDBStreamHandler = Handler<DynamoDBStreamEvent, void>; // Handler Generic export type Handler<TEvent = any, TResult = any> = ( event: TEvent, context: Context, callback: Callback<TResult> ) => void | Promise<TResult>; // For callback use case export type Callback<TResult = any> = ( error?: Error | string | null, result?: TResult ) => void;
10Pines
blog.10pines.com › 2019 › 07 › 23 › writing-object-oriented-typescript-code-for-aws-lambda
Writing Object-Oriented Typescript Code for AWS Lambda
July 16, 2024 - This is an object-oriented tutorial, but that doesn’t mean we’ll have to change the way this file is written. Think of it as the interface that AWS Lambda expects from our code. This is the default handler that the Serverless Framework makes for us. There are two things about it that are worth noticing: It receives an event, a context and a callback.
npm
npmjs.com › package › @types › aws-lambda
@types/aws-lambda - npm
1 month ago - TypeScript definitions for aws-lambda. Latest version: 8.10.161, last published: 25 days ago. Start using @types/aws-lambda in your project by running `npm i @types/aws-lambda`. There are 923 other projects in the npm registry using @types/aws-lambda.
» npm install @types/aws-lambda
Published Feb 26, 2026
Version 8.10.161
GitHub
github.com › StefH › serverless-aws-lambda-typescript-examples › blob › master › serverless-aws-lambda-typescript-webpack › src › handler.ts
serverless-aws-lambda-typescript-examples/serverless-aws-lambda-typescript-webpack/src/handler.ts at master · StefH/serverless-aws-lambda-typescript-examples
import { Context, Callback } from 'aws-lambda'; import { HttpRestHelper } from './httpRestHelper'; import { Post } from './post'; · exports.handleIt = function(event: any, context: Context, callback: Callback) { let helper: HttpRestHelper = new HttpRestHelper(); ·
Author StefH
Amazon Web Services
docs.aws.amazon.com › aws lambda › developer guide › building lambda functions with node.js › using the lambda context object to retrieve node.js function information
Using the Lambda context object to retrieve Node.js function information - AWS Lambda
When Lambda runs your function, it passes a context object to the handler . This object provides methods and properties that provide information about the invocation, function, and execution environment.
Top answer 1 of 2
5
The aws-sdk does not contain the types for lambda. So you will need both aws-sdk and @types/aws-lambda unfortunately. Also I would suggest to declare the @types/aws-lambda in the devDependencies of your package.json.
import * as AWS from "aws-sdk";
import { Context } from "aws-lambda";
module.exports.hello = async (event:any, context:Context) => {
// eg. if you need a DynamoDB client
// const docClient: AWS.DynamoDB.DocumentClient = new AWS.DynamoDB.DocumentClient({region: 'ap-southeast-2'});
return {
statusCode: 200,
body: JSON.stringify({
message: 'function executed successfully!',
input: event,
}),
};
};
2 of 2
0
You can explicitly only import the type definition
import type { Context } from "aws-lambda";
GitHub
github.com › DefinitelyTyped › DefinitelyTyped › blob › master › types › aws-lambda › handler.d.ts
DefinitelyTyped/types/aws-lambda/handler.d.ts at master · DefinitelyTyped/DefinitelyTyped
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type · ) => void | Promise<TResult>; · /** * {@link Handler} context parameter. * See {@link https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-context.html AWS documentation}. */ export interface Context { callbackWaitsForEmptyEventLoop: boolean; functionName: string; functionVersion: string; invokedFunctionArn: string; memoryLimitInMB: string; awsRequestId: string; logGroupName: string; logStreamName: string; identity?: CognitoIdentity | undefined; clientContext?: ClientContext | undefined; tenantId?: string | undefined; ·
Author DefinitelyTyped
DEV Community
dev.to › elhamnajeebullah › serverless-and-aws-lambda-with-typescript-example-for-beginners-26he
Serverless and AWS Lambda with TypeScript example for beginners - DEV Community
January 9, 2023 - In this example, the function logs the name of the function and the AWS request ID from the context argument, and logs the message of each SNS record from the event argument. Is AWS cost-effective? AWS Lambda automatically scales your functions, so you don't have to worry about capacity planning or infrastructure management.
AWS Cloud Community
docs.powertools.aws.dev › lambda › typescript › latest
Homepage - Powertools for AWS Lambda (TypeScript)
You can use Powertools for AWS Lambda in both TypeScript and JavaScript code bases.
AWS Cloud Community
docs.powertools.aws.dev › lambda › typescript › 1.4.1 › core › logger
Logger - AWS Lambda Powertools for TypeScript
October 25, 2022 - This is a Jest sample that provides the minimum information necessary for Logger to inject context data: ... If you don't want to declare your own dummy Lambda Context, you can use ContextExamples.helloworldContext from @aws-lambda-powertools/commons.
Top answer 1 of 4
75
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,
})
}
}
2 of 4
3
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;
};
GitConnected
levelup.gitconnected.com › how-to-use-typescript-for-aws-lambda-in-3-steps-1996243547eb
How to Use TypeScript for AWS Lambda in 3 Steps | by Zijing Zhao | Level Up Coding
April 14, 2022 - Unit Test and Integration Test for AWS Lambda/NodeJS in TypeScript · When we are using NodeJS as the runtime for AWS Lambdas, JavaScript is the default language. However, due to the lack of typing checking in JavaScript, from time to time, buggy code is deployed to Lambda inadvertently. Such as a small typo like this: exports.lambdaHandler = async (event, context) => { const queries = event.queytStringParameters; // ...
AWS
docs.aws.amazon.com › powertools › typescript › stage › api › functions › _aws-lambda-powertools_logger.middleware_middy.injectLambdaContext.html
injectLambdaContext | API Reference
const logger = new Logger({ serviceName: 'serverlessAirline' }); export const handler = middy(() => { logger.appendKeys({ key1: 'value1' }); logger.info('This is an INFO log with some context'); }).use(injectLambdaContext(logger, { resetKeys: true, })); @param target - The Logger instance(s) to use for logging @param options - Options for the middleware such as clearing state or resetting Copy