Accessing Amazon DynamoDB Using Amazon API Gateway Service Proxy
Services Covered
DynamoDB
API Gateway
Lab description
Accessing Amazon DynamoDB Using Amazon API Gateway Service Proxy
Learning Objectives
Create a GET method to scan a DynamoDB table
Create a POST method to put items into a table
Deploy a REST API to stage
Lab date
26-12-2021
Prerequisites
AWS account
Lab steps
-
- Create a DynamoDB table with keys: id (string), author (string), title (string). Call ti Books.
- In API Gateway create a new API with /books path. Then create a method of type GET. Configure it accordingly with your region and execution role that allows dynamodb:PutItem and dynamodb:Scan as follows:
- On the next tab choose Integration Request then Mapping Templates. For the Request body passthrough setting, select When there are no templates defined. Click Add mapping template and in the resulting field above it, enter application/json. Enter the following JSON snippet, then click Save:
{"TableName": "Books"}
- Return back the Method Execution then click Integration Response, Select the arrow icon next to the 200 Method response status entry in the table. Below Mapping Templates click application/json. Paste in the following code, then click Save below:
#set($inputRoot = $input.path('
C
- Create a POST method for /books path. Integration type: Select AWS Service
AWS Region: Select us-west-2
AWS Service: Select DynamoDB
HTTP method: Select POST
Action Type: Ensure Use action name is selected
Action: Enter PutItem - For the Request body passthrough setting, select When there are no templates defined. Click Add mapping template and in the resulting field above it, enter application/json, Enter the following JSON snippet
- For the Integration Response choose Mapping Templates and click application/json. In the template editor, enter an empty set of curly braces {}.
- In the API Gateway console, click Actions: click Deploy API. Copy the Invoke URL at the top of the page and append /books to the end of the URL: