Skip to content

LambdaContext

reference
1 min readUpdated

Kind: Interface

Source: src/adapter/aws-lambda/types.ts

Part of: Adapter

Handler context parameter. See ://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-context.html AWS documentation.

LambdaContext models the context parameter passed to an AWS Lambda handler. It contains invocation metadata, runtime settings, optional Cognito identity and client context data, and a method for checking the remaining execution time.

Properties

PropertyType
callbackWaitsForEmptyEventLoopboolean
functionNamestring
functionVersionstring
invokedFunctionArnstring
memoryLimitInMBstring
awsRequestIdstring
logGroupNamestring
logStreamNamestring
identity`CognitoIdentity
clientContext`ClientContext

Diagram

mermaid
graph LR
  Handler["Lambda handler"] --> Context["LambdaContext"]
  Context --> Invocation["Invocation metadata"]
  Context --> Runtime["Runtime settings"]
  Context --> Identity["CognitoIdentity | undefined"]
  Context --> Client["ClientContext | undefined"]
  Context --> Remaining["getRemainingTimeInMillis()"]

Usage

ts
import type { LambdaContext } from './types';

export async function handler(
  event: { action?: string },
  context: LambdaContext,
) {
  console.log({
    requestId: context.awsRequestId,
    functionName: context.functionName,
    invokedFunctionArn: context.invokedFunctionArn,
    remainingTime: context.getRemainingTimeInMillis(),
  });

  if (context.getRemainingTimeInMillis() <= 0) {
    throw new Error('Lambda execution time has expired.');
  }

  context.callbackWaitsForEmptyEventLoop = false;

  return {
    requestId: context.awsRequestId,
    action: event.action,
  };
}

AI Coding Instructions

  • Accept LambdaContext as the handler context parameter when adapter code needs AWS Lambda invocation metadata.
  • Call getRemainingTimeInMillis() before work that may exceed the Lambda execution limit.
  • Treat identity and clientContext as optional values and check for undefined before reading their properties.
  • Set callbackWaitsForEmptyEventLoop only when the handler should return before the Node.js event loop is empty.
  • Keep AWS-specific context handling inside the Lambda adapter boundary.

Was this page helpful?

Download as PDF
LambdaContext — Hono (narrator proof)