AWS Lambda
開発者ガイド

Java の AWS Lambda Context オブジェクト

Lambda で関数が実行されると、コンテキストオブジェクトはハンドラに渡されます。このオブジェクトは、呼び出し、関数、および実行関数に関する情報を示すメソッドおよびプロパティを提供します。

コンテキストメソッド

  • 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) used to invoke the function. Indicates if the invoker specified a version number or alias.

  • getMemoryLimitInMB() – Returns the amount of memory configured on 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 provided to the Lambda invoker by the client application.

  • getLogger() – 関数のロガーオブジェクトを返します。

以下の例は、コンテキスト情報を記録するハンドラ関数を示します。

例 ContextLogger.java

package example; import java.io.InputStream; import java.io.OutputStream; import com.amazonaws.services.lambda.runtime.Context; public class ContextLogger { public static void myHandler(InputStream inputStream, OutputStream outputStream, Context context) { int letter; try { while((letter = inputStream.read()) != -1) { outputStream.write(Character.toUpperCase(letter)); } Thread.sleep(3000); // Intentional delay for testing the getRemainingTimeInMillis() result. } catch (Exception e) { e.printStackTrace(); } // Print info from the context object System.out.println("Function name: " + context.getFunctionName()); System.out.println("Max mem allocated: " + context.getMemoryLimitInMB()); System.out.println("Time remaining in milliseconds: " + context.getRemainingTimeInMillis()); System.out.println("CloudWatch log stream name: " + context.getLogStreamName()); System.out.println("CloudWatch log group name: " + context.getLogGroupName()); } }

Dependencies

  • aws-lambda-java-core

Build the code with the Lambda library dependencies to create a deployment package. For instructions, see Java の AWS Lambda デプロイパッケージ.