0

I am trying to make a local Java program run in AWS Lambda and make it such that it can be called with a HTTP request. All I need is just to be able to duplicate the functionality of running java locally from the command line through HTTP in AWS so other people in the company can run the code by just sending a HTTP request in Postman(for now, next step is a web form that just makes the request) instead of downloading the jar and launching the Java command line.

I went through the hello world tutorial in the Amazon website and was able to adapt my code, and run it successfully using the test function in the AWS Lambda control panel. I am also able to see the logs in cloudwatch that it ran and also observe the results. So it all works from the Lambda control panel test function.

So instead of command line arguments, I'm giving the arguments in JSON format as follows:

{
  "environment": "dev",
  "username": "Test",
  "password": "Test22",
  "storeId": "TESTMA0001",
  "data": "a,b,c,d"
}

And this works quite well when invoking the lambda from the test function.

However I want to be able to enter this in the body of a HTTP request and have my lambda run so I added an api gateway through the gui in the aws lambda panel, chose HTTP API kind and default options.

Then I send a HTTP GET request to the api endpoint with the body being the same input I used in the testing panel, but whenever I run it, I get internal server error. I turned on access logs for the gateway api, and I get the following, my lambda is not being launched by the api since there is no lambda log being written when I use the API, it gets written when I launch it from the AWS lambda web panel.

{
    "requestId": "KByVuheeoAMEPLA=",
    "ip": "",
    "requestTime": "27/Mar/2020:02:25:40 +0000",
    "httpMethod": "GET",
    "routeKey": "$default",
    "status": "500",
    "protocol": "HTTP/1.1",
    "responseLength": "35"
}

My handleRequest function takes a string, string map as input and returns a string as output:

public class StoreCategoryImporter implements RequestHandler<Map<String,String>, String> {
    @Override
    public String handleRequest(Map<String,String> event, Context context)

I don't even use the context object but it was there in the tutorial so it remained.

I googled for hours and I have not been able to find a solution, any help would be appreciated. I find most AWS tutorials to skip over some crucial details or they don't have it for POJO developers and use js which I don't understand.

Thank you in advance.

4
  • Whey you integrate your lamba with api gateway (using aws_proxy) the event has different format. Your payload will be in event['body']. Commented Mar 27, 2020 at 2:53
  • I tried changing my body to {"body":{"environment":"dev",.......}} etc but it did not work. I find AWS terminology extremely confusing maybe I am not cut out to understand it but could you please dumb it down for me? My questions are: 1. is HTTP api ok for my task or do I have to use REST? 2. Do I need to change my java code at all or will just changing my HTTP request work? Commented Mar 27, 2020 at 3:27
  • Its too much to explain every detail of API Gateway and Lambda. I recommend to spend some time on the docs. StackOverflow is not a tutorial or online teaching platform unfortunately. Commented Mar 27, 2020 at 3:32
  • The docs do not have my use case, either it's just Java Lambda on its own or API Gateway pre-baked functions using javascript. Commented Mar 27, 2020 at 4:23

2 Answers 2

0

To simulate API Gateway event for tests in your lambda you, the lambda console has some pre-set values. In your lambda you go to Configure test event and choose the event you want to simulate, e.g.:

enter image description here

Alternatively, you can print out real event in your lambda to CloudWatch logs, and use that for tests.

Sign up to request clarification or add additional context in comments.

1 Comment

Ok I did this and I got: { "errorMessage": "An error occurred during JSON parsing", etc. The stack trace does not even contain a line that's in my code, so how do I fix it, where do I even include the actual parameters that I want to put in my program, I don't care about any of the stuff in this request.
0

I have solved this problem by more googling and finding a closer solution. Forget about the AWS tutorial, you need to override the handleRequest(APIGatewayProxyRequestEvent, Context) method, not the one that just takes in a Map(String,String) event as described in the hello world tutorial if you want to be able to trigger your lambda through a HTTP request.

Also, the "configure test event" in the AWS GUI is useless in this case. It cannot parse JSON that isn't a primitive. Probably the CLI would work better.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.