Amazon Lambda context object in C# - Amazon Lambda
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Amazon Lambda context object in C#

When Lambda runs your function, it passes a context object to the handler. This object provides properties with information about the invocation, function, and execution environment.

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.

  • RemainingTime (TimeSpan) – The number of milliseconds left before the execution times out.

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

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

  • Logger The logger object for the function.

You can use information in the ILambdaContext object to output information about your function's invocation for monitoring purposes. The following code provides an example of how to add context information to a structured logging framework. In this example, the function adds AwsRequestId to the log outputs. The function also uses the RemainingTime property to cancel an inflight task if the Lambda function timeout is about to be reached.

[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))] namespace GetProductHandler; public class Function { private readonly IDatabaseRepository _repo; public Function() { this._repo = new DatabaseRepository(); } public async Task<APIGatewayProxyResponse> FunctionHandler(APIGatewayProxyRequest request, ILambdaContext context) { Logger.AppendKey("AwsRequestId", context.AwsRequestId); var id = request.PathParameters["id"]; using var cts = new CancellationTokenSource(); try { cts.CancelAfter(context.RemainingTime.Add(TimeSpan.FromSeconds(-1))); var databaseRecord = await this._repo.GetById(id, cts.Token); return new APIGatewayProxyResponse { StatusCode = (int)HttpStatusCode.OK, Body = JsonSerializer.Serialize(databaseRecord) }; } finally { cts.Cancel(); return new APIGatewayProxyResponse { StatusCode = (int)HttpStatusCode.InternalServerError, Body = JsonSerializer.Serialize(databaseRecord) }; } } }