3

Hi i need get an item from dynamodb through api gateway and i configure the resources like this:

resources screenshot

i configure the integration method like the next picture:

enter image description here

and the mapping template is like this: enter image description here

but when i test apigateway launch this error

Execution failed due to configuration error:
No match for output mapping and no default output mapping configured.
Endpoint Response Status Code: 200
Gateway response type: API_CONFIGURATION_ERROR with status code: 500

1 Answer 1

7

I followed these two tutorials and performed get on my dynamoDB table.

My table has primary key name "pk". Tutorials: Video & Blog

  1. If i write my dynamo request body like yours it does not fetch any record
{
    "TableName": "apiG",
    "Key": {
    "pk": {
    "S": "key1"
    }
    }
}

But if I form my request like

{
    "TableName": "apiG",
    "PrimaryKey": "pk",
    "KeyConditionExpression": "pk = :v1",
    "ExpressionAttributeValues": {
        ":v1": {
            "S": "key1"
        }
    }
}

I get the desired response from dynamoDB.

  1. Your error looks like you have some mix up in your integration response and method response. In the simplest form, keep your method response as default i.e. "HTTP Status: 200 Models: application/json => Empty" and leave your integration response as API Gateway console creates it for you.

This will make sure your DynamoDB output is sent without modification to your browser/output.

  1. I transformed my dynamo output using following mapping template in integration response.
#set($inputRoot = $input.path('$'))
{
    "content": [
        #foreach($elem in $inputRoot.Items) {
            "key": "$elem.pk.S",
            "value": "$elem.pv.S"
        }#if($foreach.hasNext),#end
    #end
    ]
}

and it produced following output.

{
  "content": [
    {
      "key": "key1",
      "value": "val1"
    }
  ]
}

P.S. i had a dynamo table named 'apiG' with primaryKey 'pk' and following the exported swagger from my experiment. Hope it helps.

openapi: "3.0.1"
info:
  title: "dynamoProxy"
  version: "2020-05-01T06:45:38Z"
servers:
- url: "https://aaaaaaaaaa.execute-api.us-east-2.amazonaws.com/{basePath}"
  variables:
    basePath:
      default: "/test"
paths:
  /db:
    get:
      responses:
        200:
          description: "200 response"
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Empty"
      x-amazon-apigateway-integration:
        credentials: "arn:aws:iam::111111111111:role/apiGddbRole"
        uri: "arn:aws:apigateway:us-east-2:dynamodb:action/Query"
        responses:
          default:
            statusCode: "200"
            responseTemplates:
              application/json: "#set($inputRoot = $input.path('$'))\n{\n    \"content\"\
                : [\n        #foreach($elem in $inputRoot.Items) {\n            \"\
                key\": \"$elem.pk.S\",\n            \"value\": \"$elem.pv.S\"\n  \
                \      }#if($foreach.hasNext),#end\n\t#end\n    ]\n}"
        passthroughBehavior: "when_no_templates"
        httpMethod: "POST"
        requestTemplates:
          application/json: "{\n    \"TableName\": \"apiG\",\n    \"PrimaryKey\":\
            \ \"pk\",\n    \"KeyConditionExpression\": \"pk = :v1\",\n    \"ExpressionAttributeValues\"\
            : {\n        \":v1\": {\n            \"S\": \"key1\"\n        }\n    }\n\
            }"
        type: "aws"
components:
  schemas:
    Empty:
      title: "Empty Schema"
      type: "object"

Cheers!

Sign up to request clarification or add additional context in comments.

1 Comment

I had mixed up my integration response and method response. This is such a good answer ^^^^

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.