This pattern creates an Amazon API Gateway REST API that integrates with an Amazon DynamoDB table.
Learn more about this pattern at Serverless Land Patterns: http://serverlessland.com/patterns/apigw-dynamodb-cdk.
Important: this application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the AWS Pricing page for details. You are responsible for any AWS costs incurred. No warranty is implied in this example.
- Create an AWS account if you do not already have one and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources.
- AWS CLI installed and configured
- Git Installed
- AWS Serverless Application Model (AWS SAM) installed
-
Create a new directory, navigate to that directory in a terminal and clone the GitHub repository:
git clone https://github.com/aws-samples/serverless-patterns
-
Change directory to the pattern directory:
cd apigw-rest-api-dynamodb-cdk/cdk
-
Install dependencies
npm install
-
Deploy the stack to your default AWS account and region. The output of this command should give you the HTTP API URL.
cdk deploy
This pattern creates an Amazon API Gateway REST API that integrates with an Amazon DynamoDB table. The API integrates directly with the DynamoDB API and supports PutItem and Query actions. The API Integration has read and write access to the DynamoDB. There is no authentication on the API endpoint
Upon deployment, you will see the API endpoint URL in the output. It will take the format:
https://${API_ID}.execute-api.${REGION_NAME}.amazonaws.com
Once the application is deployed, use Postman to test the API using the following instructions.
-
Launch Postman
-
Invoke the DynamoDB PutItem action to add a new item to the DynamoDB table:
- Enter the API URL with the prod stage as the path:.
https://${API_ID}.execute-api.${REGION_NAME}.amazonaws.com/prod/id
- Select POST as the HTTP method from the drop-down list to the left of the address bar.
- Choose the Body tab. Choose raw and select JSON from the drop-down list. Enter the following into the text box:
{ "pk": "foo", "data": "blah blah blah" }
- Choose Send to submit the request and receive a "200 OK" response.
- Open the DynamoDB console and select the table which was created to confirm that the item has been added.
- Change the values for
pk
ordata
in the POST body and repeat this process to add multiple items to the DynamoDB table.
-
Invoke the DynamoDB Query action to query items by artist in the DynamoDB table:
- Enter the Invoke URL in the address bar. Add /prod/foo to the URL path.
- Add /foo to the URL path. This defines the ID that you want to query.
https://${API_ID}.execute-api.${REGION_NAME}.amazonaws.com/prod/${pk}
- Select GET as the HTTP method from the drop-down list to the left of the address bar.
- Choose the Body tab. Choose none.
- Choose Send to submit the request and receive a "200 OK" response with a list of the matching results. Example:
{ "Count": 1, "Items": [ { "pk": { "S": "foo" }, "data": { "S": "blah blah blah" } } ], "ScannedCount": 1 }
- Tutorial: Build an API Gateway REST API with AWS integration
- How do I use API Gateway as a proxy for another AWS service?
- Using Amazon API Gateway as a proxy for DynamoDB
- Setting up data transformations for REST APIs
- Amazon API Gateway API request and response data mapping reference
- API Gateway mapping template and access logging variable reference
Run the given command to delete the resources that were created. It might take some time for the CloudFormation stack to get deleted.
cdk destroy
Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: MIT-0