
REST Direct integration with DynamoDB
1) An API Gateway with REST API with the following API operations: POST /item/{stringkey}, GET /item/{stringkey} 2) DynamoDB table 'SimpleTable1' with a Partition key 'stringkey' of type String. These snippets use the following simple JSON document:
{
"partitionkey": "item123",
"timestamp": "2015-12-21T17:42:34Z",
"binary": "dGhpcyB0ZXh0IGlzIGJhc2U2NC1lbmNvZGVk",
"boolean": "true",
"null": "null",
"number": "1437136300"
}
To create a record in DynamoDB using direct integration the API operation (POST /item/{stringkey}) integration must call the DynamoDB API PutItem Action. The following POST URI: https://dynamodb.us-east-1.amazonaws.com/?Action=PutItem is called with the below JSON integration Request Body:
{
"TableName": "SimpleTable1",
"Item": {
"partitionkey": {
"S": "item123"
},
"timestamp": {
"S": "2015-12-21T17:42:34Z"
},
"binary": {
"S": "dGhpcyB0ZXh0IGlzIGJhc2U2NC1lbmNvZGVk"
},
"boolean": {
"S": "true"
},
"null": {
"S": "null"
},
"number": {
"S": "1437136300"
}
}
}
To transform the simple JSON from the Request body into the JSON above used for DynamoDB PutItem API use the following Velocity Template in the Integration Request Mapping Template:
#set ($body-objects = $util.parseJson($input.json('$')))
#set ($body-objects.partitionkey = $input.params('stringkey'))
{
"TableName": "SimpleTable1",
"Item" : {
#foreach($mapEntry in $body-objects.entrySet())
"$mapEntry.key" : {"S": "$util.escapeJavaScript( $mapEntry.value)"}
#if($foreach.hasNext),#end
#end
}
}
To read the same record from DynamoDB using direct integration the API operation (GET /item/{stringkey}) integration must call the DynamoDB API GetItem Action. The following POST URI: https://dynamodb.us-east-1.amazonaws.com/?Action=GettItem is called with the below JSON integration Request Body
{
"TableName": "SimpleTable1",
"Key": {
"partitionkey": {
"S": "item123"
}
}
}
To create the above JSON, the following Velocity Template is used in the Integration Request Mapping Template:
{
"TableName": "SimpleTable1",
"Key": {
"partitionkey": {"S": "$input.params('stringkey')"}
}
}
The DynamoDB GetItem request produces the following JSON Response:
{
"Item": {
"partitionkey": {
"S": "item123"
},
"number": {
"S": "1437136300"
},
"timestamp": {
"S": "2015-12-21T17:42:34Z"
},
"null": {
"S": "null"
},
"boolean": {
"S": "true"
},
"binary": {
"S": "dGhpcyB0ZXh0IGlzIGJhc2U2NC1lbmNvZGVk"
}
}
}
To transform the JSON response from DynamoDB format to the desired original JSON used to creaete the record, the following Velocity Template is used in the Integration Response Mapping Template:
#set($responseData = $input.path('$.Item'))
{#foreach($mapEntry in $responseData.entrySet())
"$mapEntry.key": "$mapEntry.value.get("S")"##
#if($foreach.hasNext),#end##
#end
}
The API response is the original JSON document:
{
"partitionkey": "item123",
"timestamp": "2015-12-21T17:42:34Z",
"binary": "dGhpcyB0ZXh0IGlzIGJhc2U2NC1lbmNvZGVk",
"boolean": "true",
"null": "null",
"number": "1437136300"
}