Accessing AWS Lambda Context inside Function using SpringBootRequestHandler?

Viewed 589

I need access to the AWS Lambda's Context for my lambda function. I am using SpringBootRequestHandler to receive the request, which should call a class implementing the Function interface. Below is a code sample of the two classes:

public class A extends SpringBootRequestHandler<S3Event, String> {
    @Override
    public Object handleRequest(S3Event event, Context context) {
        return super.handleRequest(event, context);
    }
}
@Component("b")
public class B implements Function<S3Event, String> {

    @Override
    public String apply(S3Event string) {
        // this is where I need the context
        // this function should be called by handleRequest
    }
}

I am using spring-cloud-starter-function-web to call the apply method in class B running locally, but this only allows directly calling class B, while class A's handleRequest is never executed. I have tried setting a static Context variable in class B and setting it at the start of handleRequest, but given that handleRequest will not execute locally, this does not work.

The three questions I have are:

  1. In an actual AWS environment, would the idea of setting a static variable work? As in, is the flow for the AWS function truly A::handleRequest -> B::apply?
  2. If #1 is no, then how might I pass the context to this apply function?
  3. If neither 1 or 2 is possible, is there a design that is that allows context to be passed?
1 Answers

To use the Lambda Context in the Function class you just need to use Autowired annotation with the com.amazonaws.services.lambda.runtime.Context class, for example:

@Autowired
private Context context;
Related