VTL - CRUD Operations - Part 1

REST Direct integration with DynamoDB

It is possible to have full REST API CRUD (create, read, update, delete) operations using direct integration between API Gateway and DynamoDB, a true no-code solution.
To get started, in this first part we will cover Create and Read using a simple strings only example. In later installments, we will cover Update and Delete and more complex data types.
All the JSON and DynamoDB values are strings for this example. All DynamoDB data types will be covered in later snippet examples.

Prerequisites

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"
}
                                


CRUD 'C' is Create

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
    }
}
                                


CRUD 'R' is Read

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"
}
                                


Created by:

Ken Cyr

Ken Cyr

Ken Cyr is an Inventor and Senior Partner Solutions Architect with Amazon Web Services and is based in North Texas. Ken was an early adopter of serverless cloud and enjoys sharing serverless techniques and patterns with others.