4
votes

I'm trying to fetch the content of a webpage from AWS Lambda but it fails silently, CloudWatch logs just contain the following lines (timeout is set to 10secs so that's why duration is 10000ms):

START RequestId: f101849f-1219-411b-8875-8944a76de937 Version: $LATEST
END RequestId: f101849f-1219-411b-8875-8944a76de937 REPORT RequestId: f101849f-1219-411b-8875-8944a76de937 Duration: 10008.39 ms

The code I'm running just takes the URL and reads the content. It works fine from local environment but it doesn't when testing on AWS Lambda:

public class TestHandler implements RequestHandler<Object, String>{

    @Override
    public String handleRequest(Object o, Context context) {
        try {
            Connection con = HttpConnection.connect(new URL("https://www.google.com"));
            con.timeout(40000);
            return con.get().getAllElements().toString();
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }
}

Any AWS restriction that I'm not aware of when dealing with requests?

2
Is your Lambda inside a VPC? Have you tried increasing the timeout of Lambda? - Suraj Bhatia
Is your lambda running synchronously or asynchronously? Is it returning a Promise back to the caller/runtime? - Jorge Garcia

2 Answers

0
votes

Java container takes time to initiate. In your case also you are hitting Lambda and you might be aware that Lambda remain in cold state if no request is coming. It awakes only when request comes. Also, timeout in your Lambda configuration is set 10sec. Please increase it from Lambda UI screen. Also as a java developer, i would suggest you to please remove unnecessary jar or maven dependency also if you can find it. Kindly increase the time first and try again.

0
votes

The issue came from insufficient memory limit that was set to default 128MB. When I increased to 256MB the connection worked as expected and it retrieved the page content. So it turns out AWS kills the lambda when it reaches the max memory limit without any message in CloudWatch (you do get it if it gets killed by timeout though)