Search code examples
aws-api-gatewaydynamodb-queries

API Gateway dynamodb query using composite key - SerializationException


I'm testing using API Gateway to query Dynamodb tables directly, using a composite key. I've setup the resource /goalcohort/{goalid} and a query parameter cohortId.

I've added cohortId to the URL query string parameter in the Method Execution (not sure if this is the correct approach. All video examples only use the Partition key and not both PK and SK).

Then, I have added this mapping template to the Post query Integration request: ''' { "TableName": "cohort", "KeyConditionExpression": "#goalId = :goalId AND #cohortId = :cohortId", ExpressionAttributeNames={ "#goalId": "goalId", "#cohortId": "cohortId" }, ExpressionAttributeValues={ ":goalId": { "S": "$input.params('goalid')" }, ":cohortId": { "S": "$input.params('cohortId')" } } } ''' (I know the passing parameter has a typo of goalid, not GoalId - hence the expressionattrbutevalue showing goalid (lowercase i) - I will fix once I've resolved this issue).

If I run the query in Dynamobd against the table, it works: enter image description here

If I try to run the API Gateway test with the same parameters: enter image description here

The log shows the parameters entered ok: ''' Sat Dec 31 19:03:32 UTC 2022 : Endpoint request body after transformations: { "TableName": "cohort", "KeyConditionExpression": "#goalId = :goalId AND #cohortId = :cohortId", ExpressionAttributeNames={ "#goalId": "goalId", "#cohortId": "cohortId" }, ExpressionAttributeValues={ ":goalId": { "S": "3615fbac-1413-4cb2-9f8f-a3b745862cb0" }, ":cohortId": { "S": "1a198952-6693-4409-b3ac-12069492eb2c" } } } Sat Dec 31 19:03:32 UTC 2022 : Sending request to https://dynamodb.us-east-2.amazonaws.com/?Action=Query '''

But, the response body is showing: {"__type":"com.amazon.coral.service#SerializationException"}

Log: Sat Dec 31 19:03:32 UTC 2022 : Sending request to https://dynamodb.us-east-2.amazonaws.com/?Action=Query Sat Dec 31 19:03:32 UTC 2022 : Received response. Status: 400, Integration latency: 6 ms Sat Dec 31 19:03:32 UTC 2022 : Endpoint response headers: {Server=Server, Date=Sat, 31 Dec 2022 19:03:32 GMT, Content-Type=application/x-amz-json-1.0, Content-Length=60, Connection=keep-alive, x-amzn-RequestId=SRP9NKEU3C4KBJTQOM69TJR4QJVV4KQNSO5AEMVJF66Q9ASUAAJG, x-amz-crc32=3948637019} Sat Dec 31 19:03:32 UTC 2022 : Endpoint response body before transformations: {"__type":"com.amazon.coral.service#SerializationException"} Sat Dec 31 19:03:32 UTC 2022 : Method response body after transformations: {"__type":"com.amazon.coral.service#SerializationException"} Sat Dec 31 19:03:32 UTC 2022 : Method response headers: {X-Amzn-Trace-Id=Root=1-63b08784-73f59a3e1691136c5e00259f, Content-Type=application/json} Sat Dec 31 19:03:32 UTC 2022 : Successfully completed execution Sat Dec 31 19:03:32 UTC 2022 : Method completed with status: 200

What am I doing wrong? All help appreciated.


Solution

  • I believe there is an error in your mapping template since its not a valid json. Try using with this json as your mapping template.

    {
       "TableName":"cohort",
       "KeyConditionExpression":"#goalId = :goalId AND #cohortId = :cohortId",
       "ExpressionAttributeNames":{
          "#goalId":"goalId",
          "#cohortId":"cohortId"
       },
       "ExpressionAttributeValues":{
          ":goalId":{
             "S":"$input.params('goalid')"
          },
          ":cohortId":{
             "S":"$input.params('cohortId')"
          }
       }
    }