Serverless 2: API Proxy, Integration Requests and DynamoDB actions.

Raul Tavares
4 min readNov 21, 2020

--

This is the second part of a series of Serverless Architecture tutorials (part 1 here) where I share some knowledge about programming and using AWS’ services to build a versatile and serverless web API.

In the previous lesson we have created our first API method, the DynamoDB table and the Insert function, and now it is time to build the Read (all) and Read (unique) functions using DynamoDB direct integration through API Gateway.

Setting Additional Permissions

Instead of creating a Lambda function for this part, let us integrate our API Gateway directly with our DynamoDB table. And for that, we will need to create another role (like the previous one) and another policy to grant one service to communicate with each other.

Go to the IAM dashboard and choose Roles -> Create Role. Then, choose AWS Service as the type, API Gateway as use case and click Next: Permissions.

Leave things as they are and click Next: Tags. In this next screen, just click on Next:Review.

Name the role APIDynamoDBCustom and click on Create role.

Now head back to to the IAM dashboard and choose Policies -> Create Policy.

Choose DynamoDB as Service, search for Scan, GetItem, UpdateItem and DeleteItem and add the three of them to the role. Under Resources, choose Specific and add the ARN of the DynamoDB table. Lastly, click on Review policy.

Give it a name like DynamoDBCustomPermissions and click on Create policy.

On the Policies page, search for the recently created policy and select it.

Inside the policy edit page, go to the tab Policy Usage and under Permissions section, click on Attach. Select the role created (APIDynamoDBCustom) and click on Attach policy. That is all!

GET Method (All)

Go to the API Gateway console and under Actions, click on Create Method. From the small dropdown that will appear, select GET and click on the check mark next to it.

Next, on the Setup page, fill in the form as follows:

· Integration Type: AWS Service
· AWS Region: your-region
· AWS Service: DynamoDB
· HTTP Method: POST

(why POST if I am creating a GET method? This is because DynamoDB queries are like POST APIs, even when you are retrieving items. For your end user, the API call will be GET, but your logic is based on a POST query)

· Action: Scan
· Execution Role: (ARN of the role created in the previous step)

Then click on Save.

Last step, you need to map the table to the request and for that click on the Integration Request section.

Down below, expand the Mapping Templates section, choose the second (recommended) option, and click on the little (+) icon to Add mapping template.

Type application/json and hit the little check mark to open a code block below. Paste the following Scan query code:

{
"TableName":"Users"
}

Check here the official documentation of the Scan action.

And that’s it! Go back to the Method Execution screen, click on Test, then Test again and see your GET request working.

Note that it brings all records of the table. But what if you just want a single record?

GET Method (Single)

Let us create another GET method but now using a URL parameter and for that we need to create a new Resource under the API Gateway.

Go to Actions, then Create Resource.

You can freely create any resource name and its methods, as this would make your URLs more friendly.

For this example, to create a URL parameter you need to use curly brackets on Resource Path, as below:

· Resouce Name: usuario
· Resouce Path: {usuario}

Hit Create Resource and with the resource selected, go to Actions, then Create Method and choose GET from the dropdown.

Fill in the same configuration as the previous method, except:

· Action: GetItem

And then hit Save.

Create a new Mapping Template, using the syntax from the GetItem along with some customizations.

{
"TableName": "Users",
"Key": { "username": { "S": "$input.params(‘usuario’)"} }
/ * the $input.params(‘xxxx’) holds the parameter you specified */
}

And finished! Go back to the Method Execution screen, click on Test.

In the parameter text field input, put any username registered in your database and click on Test down below.

Conclusion

This is it for this article. There are a lot of information to share and in the next tutorial, we will see the Update Item and Delete Item using Lambda Integration Proxy and editing the API response. See you then, thank you!

--

--

Raul Tavares

Writer, technology professional, curious and creative.