java localstack lambada - how to run lambada and see logs

Viewed 867

I am trying to run lambada using localStack and to see the log ...

so thee running class looks like :

        public class LambdaLoader implements RequestHandler<Object, String> {
      @Override
      public String handleRequest(Object input, Context context) {
          LambdaLogger logger = context.getLogger();
        logger.log("\"started\"");
            return "Complete";
      }

I am running it

public class LambdaLoaderIT {

  @Test
  void handleRequest() throws InterruptedException, IOException {
    AwsClientBuilder.EndpointConfiguration endpointConfiguration =
      new AwsClientBuilder.EndpointConfiguration(
        "http://localhost:4566", Regions.US_EAST_1.getName());
    AWSLambda lambdaClient = createLambdaClient(endpointConfiguration);

    createLambda(lambdaClient);

  }

  private AWSLambda createLambdaClient(
    AwsClientBuilder.EndpointConfiguration endpointConfiguration) {
    return AWSLambdaClientBuilder.standard()
      .withEndpointConfiguration(endpointConfiguration)
      .withCredentials(
        new AWSStaticCredentialsProvider(
          new BasicAWSCredentials("dummyAccessKey", "dummySecretKey")))
      .build();
  }

  private void createLambda(AWSLambda clientLambda) throws IOException {
    CreateFunctionRequest functionRequest = new CreateFunctionRequest();
    functionRequest.setHandler("com.ssp.coreTeam.LambdaLoader::handleRequest");
    functionRequest.setFunctionName("handleRequest");
    functionRequest.setTimeout(900);
    functionRequest.setRuntime("java11");
    functionRequest.setRole("arn:aws:lambda:us-east-1:000000000000:function:handleRequest");

    FunctionCode code = new FunctionCode();
    File file = new File("target/my-lambda-0.0.0-SNAPSHOT.jar");
    FileInputStream fileInputStream = new FileInputStream(file);
    byte[] bytes = IoUtils.toByteArray(fileInputStream);
    code.setZipFile(ByteBuffer.wrap(bytes));
    functionRequest.setCode(code);
    Environment environment = new Environment();
    environment.setVariables(Map.of("LAMBDA_ENV","dev"));
    functionRequest.setEnvironment(environment);
    CreateFunctionResult function = clientLambda.createFunction(functionRequest);
    System.out.println(function);
  }

in addition, this is how I have configured lambada in the docker-compose file (notice LAMBDA_EXECUTOR=local ):

  localstack:
    image: 'localstack/localstack'
    ports:
      - '4566:4566'
    environment:
      - SERVICES=lambda,ssm
      - DEBUG=1
      - DATA_DIR=${DATA_DIR- }
      - PORT_WEB_UI=${PORT_WEB_UI- }
      - LAMBDA_EXECUTOR=local
      - KINESIS_ERROR_PROBABILITY=${KINESIS_ERROR_PROBABILITY- }
      - DOCKER_HOST=unix:///var/run/docker.sock
      - HOST_TMP_FOLDER=${TMPDIR}
    volumes:
      - "${TMPDIR:-/tmp/localstack}:/tmp/localstack"
      - "/var/run/docker.sock:/var/run/docker.sock"

How can I see the logs and what happened there?

1 Answers

You've already set the DEBUG to 1, so the logs are there.

To read them, use the standard Docker Compose facilities for logs. In your case it should be something like:

docker-compose logs localstack

I would also recommend you to use a small library to inject AWS clients in your tests, named aws-junit5. It would greatly simplify your tests. It supports Lambda clients for both AWS Java SDK 1.x and 2.x. The usage is pretty straightforward:

@ExtendWith(Lambda.class)
class AmazonDynamoDBInjectionTest {
    @AWSClient(endpoint = Endpoint.class) // Endpoint configuration
    private AWSLambda client;

    @Test
    void test() {
        CreateFunctionRequest functionRequest = new CreateFunctionRequest();
        
        functionRequest.setHandler("com.ssp.coreTeam.LambdaLoader::handleRequest");
        functionRequest.setFunctionName("handleRequest");
        functionRequest.setTimeout(900);
        functionRequest.setRuntime("java11");
        functionRequest.setRole("arn:aws:lambda:us-east-1:000000000000:function:handleRequest");

        FunctionCode code = new FunctionCode();
        File file = new File("target/my-lambda-0.0.0-SNAPSHOT.jar");
        FileInputStream fileInputStream = new FileInputStream(file);
        byte[] bytes = IoUtils.toByteArray(fileInputStream);

        code.setZipFile(ByteBuffer.wrap(bytes));
        functionRequest.setCode(code);

        Environment environment = new Environment();

        environment.setVariables(Map.of("LAMBDA_ENV","dev"));
        functionRequest.setEnvironment(environment);

        // Just use client here, it will be auto-injected!
        CreateFunctionResult function = client.createFunction(functionRequest);

        // Rest of your test
        System.out.println(function);
    }
}

There is even an example of CI/CD with GitHub, which is very similar to what you're doing.

Related