'How to get Context for logging when using APIGatewayProxyRequestEvent in aws-lambda

My project is written in spring-cloud-function and deployed in aws-lambda. I have a requirement wherein I am supposed to log the events.

A little search told me to use

com.amazonaws.services.lambda.runtime.Context for logging by doing this:

context.getLogger().log("log event here");

I have a spring cloud function which receives APIGatewayProxyRequestEvent as the input and APIGatewayProxyResponseEvent as the output parameter

I searched again and found to get the context, this can be wrapped with org.springframework.messaging.Message

so I wrote the function like this:

public Function<Message<APIGatewayProxyRequestEvent>, APIGatewayProxyResponseEvent> saveEmployee(){
return request -> {
            Context context = request.getHeaders().get("aws-context", Context.class);
context.getLogger().log("employee save request---: " + request);
    //do something

However the context evaluates to null and I get NullPointerException

Can someone point to what might be going wrong? or how to fetch context?

Thanks in advance



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source