AWS API Gateway integration with DynamoDB

Hareesh Veduraj
4 min readMay 2, 2020

--

API Gateway handles all the tasks involved in accepting and processing up to hundreds of thousands of concurrent API calls. Most of the times we have used API Gateway to invoke lambda functions that talks to a data store.

Here we will look into an example where we will develop a Serverless application without lambda. Yes, that’s true we can create the app by having API Gateway to directly communicate with DynamoDB without having a lambda function in between.

Most of us would have not known the inbuilt integration methods provided by API Gateway which would help us replace the lambda.

Let’s go step by step:

Step1:

Create the dynamodb table. Here in this example, we are going to create a url_store table. Also add a secondary index, so that we can extend the table to return the results by created_user.

Create DynamoDb Table

Step 2:

Create the IAM Roles needed for API Gateway. Always follow the best practice, let the users have minimum necessary permissions needed. So lets create one role for read and another role for CRUD operations.

Create a new Role selecting the API Gateway service.

Create Role

Now add the necessary permissions. For this create a read policy for DynamoDB and attach the policy to this role.

For DynamoDb Read, the access level select ‘BatchGetItem’,’GetItem’,’Query’. These minimum permissions should be enough for the read user.

For DynamoDb CRUD operations, the access level select ‘BatchWriteItem’,’PutItem’,’PutItem’,’DeleteItem’ should be fine

Add Policy: DynamoDBReadRole
Add Policy: DynamoDB CRUD Role

Step 3:

Now since we have created the necessary IAM Roles let’s go ahead and create the API Gateway.

a)Choose REST API End Point

Create API Gateway

b). Once the API is created we can configure the endpoint.

Select the integration type as AWS Services. Usually we go ahead with Lambda function here is the trick.

Select the appropriate region

In the AWS Services dropdown, you can see a lot of AWS services being supported by API Gateway. From this, select DynamoDB.

Select the HTTP method as ‘POST’. This point is very important, regardless of whether your API endpoint is GET/POST/DELETE , the integration from API Gateway to DynamoDB should always be ‘POST’.

Action can be written as ‘UpdateItem’. This is a DynamoDB operation.

Execution Role ARN should be filled with the Role details we have created in Step 2

API Gateway Endpoint Setup

C). Configure the integration request.

This is one important feature provided by API Gateway which will help us eliminate the need for a lambda function. Normally we will use lambda function to accept the user input and convert it into DynamoDB request. The same we can achieve by using ‘mapping templates’ inside integration request.

Click on integration request and add mapping templates.

Add mapping templates

d). Mapping template configuration

In this template we can provide the details needed to convert the user input to necessary dynamodb statement. Here in the below example, we accept the url,created_by from the user and store it against the id provided. There is a condition check provided to put the entry only if the id doesn’t exist.

Mapping Template

Now we are ready to test our API.

Test the API

Hurraayyy

You can see the success response and the same entry can be verified in Dynamo DB.

We can also modify the response returned from DynamoDB using mapping templates inside Integration response and can provide custom response codes.

Next Steps:

Here I have provided only POST operation which can be extended to GET,UPDATE,DELETE. Please try out the same.

--

--