Create generic MicronautRequestHandler to handle any AWS Event and APIGatewayProxy request

135 Views Asked by At

I am trying to create Micronaut AWS-Lambda application to handle and support any AWS Event processing in addition to APIGatewayProxy request using MicronautRequestHandler.

Basically the challenge is, I currently have Micronaut REST API application running on Lambda and now the requirement is to make a change to the current application to handle any incoming Events from EventBridge, S3, SQS, etc in the same application. Thus, trying to make it work using MicronautRequestHandler so that I keep utilizing Controller classes along with Event processing.

So far I have tried this in my Demo application and seems to be working from AWS Lambda. However, I just want to double check if this is a valid way to achieve my goal or is there a better way of doing it? Would appreciate your any suggestions to make this better!

Controller

package com.example;

import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;

import java.util.Collections;
import java.util.Map;

@Controller
public class HomeController {

    @Get("/getdata")
    public Map<String, Object> index() {
        return Collections.singletonMap("message", "Hello World!");
    }
}

Function Handler

package com.example;

import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
import io.micronaut.function.aws.MicronautRequestHandler;
import io.micronaut.function.aws.proxy.payload1.ApiGatewayProxyRequestEventFunction;
import io.micronaut.json.JsonMapper;
import jakarta.inject.Inject;

import java.io.IOException;

public class FunctionHandler extends MicronautRequestHandler<Object, Object> {
    @Inject
    JsonMapper objectMapper;

    @Inject
    ProcessSQSmessageService service;

    private ApiGatewayProxyRequestEventFunction handler = new ApiGatewayProxyRequestEventFunction();

    @Override
    public Object execute(Object input) {
        APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent();
        try {
            String strInput = objectMapper.writeValueAsString(input);

            // Want to parse request based on its type using such conditions
            if (strInput.contains("Records"))   {
                // Want to Call ProcessSQSmessageService to process the SQS message.
                System.out.println(service.doSomething());
            } else {

                // Calling controller methods to process the API Gateway request.
                APIGatewayProxyRequestEvent proxyRequest = objectMapper.readValue(strInput, APIGatewayProxyRequestEvent.class);
                return handler.handleRequest(proxyRequest, null);
            }
        } catch (IOException e) {
            response.setStatusCode(500);
        }
        return response;
    }
}

ProcessSQSmessageService

@Singleton
public class ProcessSQSmessageService {

    public String doSomething() {
        return "You Can Do It! Believe In Yourself!";
    }
}
0

There are 0 best solutions below