简体   繁体   中英

How To Convert Java Lambda from AWS API Gateway to Load Balancer

I have a Lambda function in Java which is invoked by AWS Api Gateway. The Lambda function accepts PolicyNo in request body and returns the same PolicyNo in response. Below is the Lambda code.

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.LambdaLogger;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;

public class AssuranceDataRequestHandler implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {

    public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent apiGatewayProxyRequestEvent, Context context) {
        APIGatewayProxyResponseEvent apiGatewayProxyResponseEvent = new APIGatewayProxyResponseEvent();
        try {
            LambdaLogger logger = context.getLogger();
            final AmazonCloudWatch cw = AmazonCloudWatchClientBuilder.defaultClient();
            apiGatewayProxyRequestEvent.getPathParameters();
            String requestString = apiGatewayProxyRequestEvent.getBody();
            logger.log("API Gateway requestString : " + requestString);
            JSONParser parser = new JSONParser();
            JSONObject requestJsonObject = (JSONObject) parser.parse(requestString);
            String policyNo = null;

            if (requestJsonObject != null) {
                if (requestJsonObject.get("policyNo") != null) {
                    policyNo = requestJsonObject.get("policyNo").toString();
                }        
            }
            PolicyNo policyNoBo = new PolicyNo();
            policyNoBo.setPolicyNo();
            ObjectMapper mapper = new ObjectMapper();
            try {
                responseMessage = mapper.writeValueAsString(policyNoBo);
             /// responseMessage = new JSONObject(salesVideoResponse).toJSONString();
              System.out.println("ResultingJSONstring = " + responseMessage);
              //System.out.println(json);
            } catch (JsonProcessingException e) {
               e.printStackTrace();
            }
            generateResponse(apiGatewayProxyResponseEvent, responseMessage);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return apiGatewayProxyResponseEvent;
    }


    private void generateResponse(APIGatewayProxyResponseEvent apiGatewayProxyResponseEvent, String requestMessage) {
        apiGatewayProxyResponseEvent.setHeaders(Collections.singletonMap("timeStamp", String.valueOf(System.currentTimeMillis())));
        apiGatewayProxyResponseEvent.setStatusCode(200);
        apiGatewayProxyResponseEvent.setBody(requestMessage);
    }
}

I need to change this implementation to Load Balancer where below is the URL and I need to fetch PolicyNo from the url.

https://XXXXXXX/api/hk/health/policy/v1/policies/{policyNo}/coverage/benefit/

In summary, I need to convert my Lambda function to invoke from Load Balancer.

You can add an Application Load balancer as a trigger for lambda function, it is similar how you add API Gateway Trigger. Just be caution while creating a target group... you must select Lambda function instead of an ec2-instance.(refer screenshot) No change in the code is required, if your Lambda was working with the API gateway interface it shall workout fine with the ALB.

for more help you can refer to: https://aws.amazon.com/blogs/networking-and-content-delivery/lambda-functions-as-targets-for-application-load-balancers/

Let me know if you face any issues while implementing the same.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM