AWS Lambda context object in TypeScript - AWS Lambda

AWS Lambda context object in TypeScript

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.

Context methods
  • getRemainingTimeInMillis() – Returns the number of milliseconds left before the execution times out.

Context properties
  • functionName – The name of the Lambda function.

  • functionVersion – The version of the function.

  • invokedFunctionArn – The Amazon Resource Name (ARN) that's used to invoke the function. Indicates if the invoker specified a version number or alias.

  • memoryLimitInMB – The amount of memory that's allocated for the function.

  • awsRequestId – The identifier of the invocation request.

  • logGroupName – The log group for the function.

  • logStreamName – The log stream for the function instance.

  • identity – (mobile apps) Information about the Amazon Cognito identity that authorized the request.

    • cognitoIdentityId – The authenticated Amazon Cognito identity.

    • cognitoIdentityPoolId – The Amazon Cognito identity pool that authorized the invocation.

  • clientContext – (mobile apps) Client context that's provided to Lambda by the client application.

    • client.installation_id

    • client.app_title

    • client.app_version_name

    • client.app_version_code

    • client.app_package_name

    • env.platform_version

    • env.platform

    • env.make

    • env.model

    • env.locale

    • Custom – Custom values that are set by the client application.

  • callbackWaitsForEmptyEventLoop – Set to false to send the response right away when the callback runs, instead of waiting for the Node.js event loop to be empty. If this is false, any outstanding events continue to run during the next invocation.

You can use the @types/aws-lambda npm package to work with the context object.

Example index.ts file

The following example function logs context information and returns the location of the logs.

Note

Before using this code in a Lambda function, you must add the @types/aws-lambda package as a development dependency. This package contains the type definitions for Lambda. When @types/aws-lambda is installed, the import statement (import ... from 'aws-lambda') 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 in the DefinitelyTyped GitHub repository.

import { Context } from 'aws-lambda'; export const lambdaHandler = async (event: string, context: Context): Promise<string> => { console.log('Remaining time: ', context.getRemainingTimeInMillis()); console.log('Function name: ', context.functionName); return context.logStreamName; };