5

I have a lambda, written in Java, that accepts a Request Object of the structure

{
  "id": "be1c320a-144f-464d-b32c-38ec7fb4445b",
  "userId": "foobar"
}

When I call this Lambda through the test interface with such an object, it works fine.

I want to create an API where a GET request to

/users/foobar/items/be1c320a-144f-464d-b32c-38ec7fb4445b

i.e. of the form

/users/{userId}/items/{id}

calls this Lambda.

I have created the API resources /users, {userId}, items, and {id} appropriately.

And I have created the GET method (on /users/{userId}/items/{id})and associated it to the lambda.

When I test the API, it invokes the lambda, but with null values in the request. I can see it package the path as {"id":"be1c320a-144f-464d-b32c-38ec7fb4445b","userId": "foobar"} in the logs, but that's not being sent in the body.

I have tried creating a template map (and have tried RTFM), but cannot see how to map path parameters to a body.

How do I achieve this mapping?

2 Answers 2

2

I think your Request Object structure may not be properly configured. There may be a few ways to configure this. Here is some information that has helped me.

How to pass a querystring or route parameter to AWS Lambda from Amazon API Gateway - Demonstrates this mapping (albeit with python). However, taking the top response, if you enable "Use Lambda Proxy integration", you can similarily do this with Java as so:

@Override
public Object handleRequest(APIGatewayProxyRequestEvent input, Context context) {

    Map<String, String> pathParameters = input.getPathParameters();
    String id = pathParameters.get("id");
    String userId = pathParameters.get("userId");

    // Handle rest of request..

}

This is a tuturial using the serverless framework to create an Api with Java. This tutorial similarily accesses the pathParameters by parsing the input rather than using the APIGatewayProxyRequestEvent java class.

@Override
public Object handleRequest(Map<String, Object> input, Context context) {

    try {
        // get the 'pathParameters' from input
        Map<String,String> pathParameters =  (Map<String,String>)input.get("pathParameters");
        String id = pathParameters.get("id");
        String userId = pathParameters.get("userId");

    } catch (Exception ex) {
        logger.error("Error in retrieving product: " + ex);

    }
}
0

Use a mapping template.

First, in the Method Request section, you should see userId and id as Request Paths

Then, in the Integration Request, do not choose Proxy Integration.

Then in the Mapping Templates section, add a new mapping template for application/json of the form

{
  "id"     : "$method.request.path.id",
  "userId" : "$method.request.path.user_id"
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.