Using the Lambda context object to retrieve Java function information - AWS Lambda

Using the Lambda context object to retrieve Java function information

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.

  • getFunctionName() – Returns the name of the Lambda function.

  • getFunctionVersion() – Returns the version of the function.

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

  • getMemoryLimitInMB() – Returns the amount of memory that's allocated for the function.

  • getAwsRequestId() – Returns the identifier of the invocation request.

  • getLogGroupName() – Returns the log group for the function.

  • getLogStreamName() – Returns the log stream for the function instance.

  • getIdentity() – (mobile apps) Returns information about the Amazon Cognito identity that authorized the request.

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

  • getLogger() – Returns the logger object for the function.

The following example shows a function that uses the context object to access the Lambda logger.

package example; import; import; import; import java.util.Map; // Handler value: example.Handler public class Handler implements RequestHandler<Map<String,String>, Void>{ @Override public Void handleRequest(Map<String,String> event, Context context) { LambdaLogger logger = context.getLogger(); logger.log("EVENT TYPE: " + event.getClass()); return null; } }

The function logs the class type of the incoming event before returning null.

Example log output
EVENT TYPE: class java.util.LinkedHashMap

The interface for the context object is available in the aws-lambda-java-core library. You can implement this interface to create a context class for testing. The following example shows a context class that returns dummy values for most properties and a working test logger.

Example src/test/java/example/
package example; import; import; import; import; public class TestContext implements Context{ public TestContext() {} public String getAwsRequestId(){ return new String("495b12a8-xmpl-4eca-8168-160484189f99"); } public String getLogGroupName(){ return new String("/aws/lambda/my-function"); } public String getLogStreamName(){ return new String("2020/02/26/[$LATEST]704f8dxmpla04097b9134246b8438f1a"); } public String getFunctionName(){ return new String("my-function"); } public String getFunctionVersion(){ return new String("$LATEST"); } public String getInvokedFunctionArn(){ return new String("arn:aws:lambda:us-east-2:123456789012:function:my-function"); } public CognitoIdentity getIdentity(){ return null; } public ClientContext getClientContext(){ return null; } public int getRemainingTimeInMillis(){ return 300000; } public int getMemoryLimitInMB(){ return 512; } public LambdaLogger getLogger(){ return new TestLogger(); } }

For more information on logging, see Log and monitor Java Lambda functions.

Context in sample applications

The GitHub repository for this guide includes sample applications that demonstrate the use of the context object. Each sample application includes scripts for easy deployment and cleanup, an AWS Serverless Application Model (AWS SAM) template, and supporting resources.

Sample Lambda applications in Java
  • java17-examples – A Java function that demonstrates how to use a Java record to represent an input event data object.

  • java-basic – A collection of minimal Java functions with unit tests and variable logging configuration.

  • java-events – A collection of Java functions that contain skeleton code for how to handle events from various services such as Amazon API Gateway, Amazon SQS, and Amazon Kinesis. These functions use the latest version of the aws-lambda-java-events library (3.0.0 and newer). These examples do not require the AWS SDK as a dependency.

  • s3-java – A Java function that processes notification events from Amazon S3 and uses the Java Class Library (JCL) to create thumbnails from uploaded image files.

  • Use API Gateway to invoke a Lambda function – A Java function that scans a Amazon DynamoDB table that contains employee information. It then uses Amazon Simple Notification Service to send a text message to employees celebrating their work anniversaries. This example uses API Gateway to invoke the function.