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 回答
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
专家
已回答 2 年前
  • 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?

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则