Not able to inject com.amazonaws.services.lambda.runtime.Context into Micronaut serverless function "AWS Lambda"

Viewed 62

Unable to inject AWS Lambda context into MicronautRequestHandler

public class FunctionRequestHandler extends MicronautRequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
    private Logger logger = LoggerFactory.getLogger(FunctionRequestHandler.class);

    @Inject
    private BeanProvider<Context> contextProvider;

   
    @Override
    public APIGatewayProxyResponseEvent execute(APIGatewayProxyRequestEvent input) {
        APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent();
        logger.info("starting...");
        logger.info("functionArn:{}", this.applicationContext.getAttributes());
        logger.info("functionArn:{}", contextProvider.get().getInvokedFunctionArn());
        response.setStatusCode(200);
        return response;
    }
}

What's the best way to read context information?

Updated with error information:

{
  "errorMessage": "No bean of type [com.amazonaws.services.lambda.runtime.Context] exists. Make sure the bean is not disabled by bean requirements (enable trace logging for 'io.micronaut.context.condition' to check) and if the bean is enabled then ensure the class is declared a bean and annotation processing is enabled (for Java and Kotlin the 'micronaut-inject-java' dependency should be configured as an annotation processor).",
  "errorType": "io.micronaut.context.exceptions.NoSuchBeanException",
  "stackTrace": [
    "io.micronaut.context.DefaultBeanContext.resolveBeanRegistration(DefaultBeanContext.java:2805)",
    "io.micronaut.context.DefaultBeanContext.getBean(DefaultBeanContext.java:1617)",
    "io.micronaut.inject.provider.BeanProviderDefinition$1.get(BeanProviderDefinition.java:81)",
    "com.example.FunctionRequestHandler.execute(FunctionRequestHandler.java:25)",
    "java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)",
    "java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)",
    "java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)",
    "java.base/java.lang.reflect.Method.invoke(Unknown Source)"
  ]
}
1 Answers

From the Micronaut docs

The Lambda Context is registered as a singleton each time the handler gets invoked. You can inject it by using:

@Any BeanProvider<com.amazonaws.services.lambda.runtime.Context> context

If present in the Lambda Context, the following singletons are registered, and you can inject them:

@Any BeanProvider<CognitoIdentity> cognitoIdentity
@Any BeanProvider<ClientContext> clientContext
@Any BeanProvider<LambdaLogger> lambdaLogger
Related