Just wrote a (simple) type definition for lambda function & typescript

Surprisingly I cannot find such a thing somewhere. Here are the definitions useful for synchronous handlers: type Event, type Context, and type Response. I just covered some basic usage so don’t expect too much. And don’t expect it is correct.

import type http from 'http'
interface Event {
  path: string
  httpMethod: string
  headers: http.IncomingHttpHeaders & {
    'client-ip': string
  }
  multiValueHeaders: { [index: string]: string[] }
  queryStringParameters: { [index: string]: string }
  multiValueQueryStringParameters: { [index: string]: string[] }
  body: string
  isBase64Encoded: boolean
}
interface Context {
  callbackWaitsForEmptyEventLoop: boolean
  functionVersion: string
  functionName: string
  memoryLimitInMB: string
  logGroupName: string
  logStreamName: string
  clientContext: {
    custom: {
      netlify: string
    }
    identity?: {
      url: string
      token: string
    }
    user?: any
  }
  invokedFunctionArn: string
  awsRequestId: string
}
interface Response {
  statusCode: number
  isBase64Encoded?: boolean
  headers?: { [index: string]: string }
  multiValueHeaders?: { [index: string]: string[] }
  body?: string
}
type Handler = (event: Event, context: Context) => Promise<Response>

The type information is from mostly the doc. Some (like Event.headers['client-ip']) are from an echo function:

export const handler: Handler = async (
  event: Event,
  context: Context
): Promise<Response> => {
  return {
    statusCode: 200,
    body: JSON.stringify({event, context})
  }
}

if you don’t want import type http from 'http', just replace or remove http.IncomingHttpHeaders at line#5.