0

I'm getting started with a lambda function for java and I am working through the HelloWorldFunction that was generated by sam init

When running the sample I get only timeouts.

What should I check? What have I missed?

$ sam local invoke HelloWorldFunction --no-event
Invoking helloworld.App::handleRequest (java8)
2019-09-18 12:07:23 Found credentials in shared credentials file: ~/.aws/credentials

Fetching lambci/lambda:java8 Docker container image......
Mounting /Users/********/Documents/github/sam-app/.aws-sam/build/HelloWorldFunction as 
/var/task:ro,delegated inside runtime container
START RequestId: 8a420a00-ef81-4921-9a9e-508111fc5c8a Version: $LATEST
Function 'HelloWorldFunction' timed out after 20 seconds

It's the sample that is generated with sam init.

package helloworld;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;

/**
 * Handler for requests to Lambda function.
 */
public class App implements RequestHandler<Object, Object> {

public Object handleRequest(final Object input, final Context context) {
    Map<String, String> headers = new HashMap<>();
    headers.put("Content-Type", "application/json");
    headers.put("X-Custom-Header", "application/json");
    try {
        final String pageContents = this.getPageContents("https://checkip.amazonaws.com");
        String output = String.format("{ \"message\": \"hello world\", \"location\": \"%s\" }", pageContents);
        return new GatewayResponse(output, headers, 200);
    } catch (IOException e) {
        return new GatewayResponse("{}", headers, 500);
    }
}

private String getPageContents(String address) throws IOException{
    URL url = new URL(address);
    try(BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()))) {
        return br.lines().collect(Collectors.joining(System.lineSeparator()));
    }
}

}

I believe my issue is the proxy in my corporate environment.

How do I set the proxies in the java code?

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
Phil Lachmann
  • 211
  • 4
  • 14
  • We really need more information - what does your code look like? Have you tried to deploy to Lambda? – stdunbar Sep 18 '19 at 17:30
  • try `context.callbackWaitsForEmptyEventLoop = false` as the first line within your handleRequest function. – Dylan Sep 18 '19 at 19:01
  • Unrelated, but please see the bottom of [What are tags, and how should we use them](https://stackoverflow.com/help/tagging); putting explicit tags in the title is redundant. – Dave Newton Sep 18 '19 at 19:04
  • Thanks Dave Newton! – Phil Lachmann Sep 18 '19 at 19:13
  • Take a look at [this post](https://stackoverflow.com/questions/120797/how-do-i-set-the-proxy-to-be-used-by-the-jvm). You won't need that when running in the "real" Lambda environment but it may help now. – stdunbar Sep 18 '19 at 19:59

0 Answers0