AWS API Gateway Integration Response Mapping Template using request body

0

I have an API Gateway resource setup with the below flow:

POST $request -> Method Request -> Integration Request -> Step Function
$response <- Method Response <- Integration Response

Currently, $response comes back as the response body from invoking the Step Function correctly. However, I want to know if there is a way I can use mapping templates to grab some values from the body of the request. For example, my request body looks something like this:

{
  "Value": {
    "Item1": "one",
    "Item2": "two"
  }
}

I would like to be able to grab the 2 properties under "Value" and return a response that looks something like this:

{
  "Item1": "one",
  "Item2": "two"
}

I have tried using the following mapping template:

{
  "Item1": $input.json('$.Value.Item1'),
  "Item2": $input.json('$.Value.Item2')
}

However, I have realised that the value of $input actually comes from the response of the Step Function (I had assumed it was the input to the entire resource).

Is there a way to access the request body in the response mapping template? The only alternative I can think of at this point is to add a Lambda Function between the API Gateway and Step Function to return the custom response after invoking the Step Function, but I would prefer to avoid this if possible.

1 Answer
0

Response mapping template can only map from the Integration Response to the Method Response.

Have you tried adding the input to the request as part of the payload that is passed onto the Step Function and have the Step Function return it back in the Integration Response? You should then be able to use the data from the incoming request in your mapping template, since it is now part of the integration response.

profile pictureAWS
EXPERT
answered a year ago
  • That is what I thought. Unfortunately, the Step Function is executed asynchronously, so I can't get a response back from the execution. I only get a JSON payload direct from the Step Function to say that execution has started (with the ARN of the SF). Unless there's another option that I'm missing?

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions