Skip to content

Latest commit

 

History

History
 
 

apigw-rest-api-dynamodb

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

Amazon API Gateway REST API to Amazon DynamoDB

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.

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.

Requirements

Deployment Instructions

  1. 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
    
  2. Change directory to the pattern directory:

    cd apigw-rest-api-dynamodb
    
  3. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file:

    sam deploy --guided
    
  4. During the prompts:

    • Enter a stack name
    • Enter the desired AWS Region
    • Allow SAM CLI to create IAM roles with the required permissions.

    Once you have run sam deploy --guided mode once and saved arguments to a configuration file (samconfig.toml), you can use sam deploy in future to use these defaults.

  5. Note the outputs from the SAM deployment process. These contain the resource names and/or ARNs which are used for testing.

How it works

This pattern creates an Amazon API Gateway REST API that integrates with an Amazon DynamoDB table named "Music". The API includes an API key and usage plan. The DynamoDB table includes a Global Secondary Index named "Artist-Index". The API integrates directly with the DynamoDB API and supports PutItem and Query actions.

Testing

Once the application is deployed, use Postman to test the API using the following instructions.

  1. Launch the AWS Management Console and open the API Gateway console. Select the API named "api-music".
    • In the Stages section, copy the Invoke URL from the stage named "v1".
    • In the API Keys section, copy the API key named "api-music-apikey".
  2. Launch Postman
  3. Choose the Authorization tab. Choose API Key for the authorization type. Enter x-api-key as the Key name. Enter the API key as the Value.
  4. Invoke the DynamoDB PutItem action to add a new item to the DynamoDB table:
    • Enter the Invoke URL in the address bar. Add the /music path to the URL.
    https://{ApiId}.execute-api.{Region}.amazonaws.com/v1/music
    
    • 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:
    {
    	"artist": "The Beatles",
    	"album": "Abbey Road"
    }
    
    • Choose Send to submit the request and receive a "200 OK" response.
    • Open the DynamoDB console and select the table named "Music" to confirm that the item has been added.
    • Change the values for artist or album and repeat this process to add multiple items to the DynamoDB table.
  5. Invoke the DynamoDB Query action to query items by artist in the DynamoDB table:
    • Enter the Invoke URL in the address bar. Add /music to the URL path.
    • Add /The+Beatles to the URL path. This defines the artist name that you want to query. The + represents a space.
    https://{ApiId}.execute-api.{Region}.amazonaws.com/v1/music/The+Beatles
    
    • 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:
    {
    	"music": [
    		{
    			"id": "9cd966d3-b161-45cf-ab07-ce39178c05b5",
    			"artist": "The Beatles",
    			"album": "Abbey Road"
    		}
    	]
    }
    

Documentation

Cleanup

  1. Delete the stack
    aws cloudformation delete-stack --stack-name STACK_NAME
  2. Confirm the stack has been deleted
    aws cloudformation list-stacks --query "StackSummaries[?contains(StackName,'STACK_NAME')].StackStatus"

Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.

SPDX-License-Identifier: MIT-0