AWS API Gateway + Lambda + DynamoDB

Conor O'Dwyer
The Startup
Published in
6 min readSep 8, 2020

In this tutorial, I will walk you through how to set up endpoints using API Gateway to trigger Lambda functions which interact with your DynamoDB database using the boto3 Python library. I will upload a future tutorial explaining how to write a Lambda function to communicate with RDS instead of DynamoDB using pymysql and Lambda layers.

1. AWS Account Setup

If you already have an AWS account setup, please skip ahead to section 2. If you do not, you can sign up for an account here. You will be required to enter payment details but you get 12 months of free tier access when signing up and nothing in this tutorial will exceed free tier limits.

Note: If you have exceeded your free tier usage for your account already, you may be charged for the usage of the AWS services described in this tutorial.

2. Set up DynamoDB

After logging in to the AWS Console, search for DynamoDB from the list of services. Click “Create table” in the Getting Started screen.

Note: DynamoDB instances are specific to the AWS region selected in the top right of the AWS console window. Ensure you have the correct region selected before creating your table (this is typically the region that is geographically closest to you).

In the Create DynamoDB table window, enter a name for your table and a primary key. You can also optionally specify a sort key if you want a composite primary key for your table. In this example, I have created a Playlist table with SongName as the primary key and Artist as the sort key. You can accept the default settings.

Once your table is created, select the Items tab for your table and create a few items to be stored in your database table. When creating an item in your table, you can also specify more fields on the item in addition to the two key fields. In this example, some of the items additionally specify the Album field. You have now successfully created your DynamoDB table which we can query from your Lambda function.

3. Create your Lambda function

Now it’s time to set up your Lambda function. Open the Lambda service and ensure the Functions dashboard is selected. I recommend creating your Lambda function in the same region as the DynamoDB you have already created.

Select “Create function” and give your function a name, I have called the function ‘get-playlist’ in this tutorial. Use Python 3.8 as the Runtime and leave “Create a new role with basic Lambda permissions” as the Execution role. Create your function.

Add the following as your function code and Save. When the function is executed, the lambda_handler method is performed and returns a HTTP response which will be consumed by API Gateway. The code imports the boto3 library and creates the dynamodb resource, the scan() function is then called on the Playlist table to return all data from the table and sets the list of items in the table as the HTTP response body.

import json
import boto3
dynamodb = boto3.resource('dynamodb')
def lambda_handler(event, context):

table = dynamodb.Table('Playlist')
body = table.scan()
items = body['Items']
return {
'statusCode': 200,
'headers': {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token',
'Access-Control-Allow-Credentials': 'true',
'Content-Type': 'application/json'
},
'body': json.dumps(items)
}

The Lambda function will not work just yet, we must give the function permissions to perform actions on our DynamoDB table. In your Lambda function, select the Permissions tab. You will see a default Execution role has been created for you. Click on the Role name to open the role in the IAM console. In the IAM console, attach the ‘AmazonDynamoDBFullAccess’ policy to your role and save.

Now you can test your Lambda function. In the top right of your Lambda function console window, select the ‘Select a test event’ drop down menu and click ‘Configure test events’. AWS comes with many test event templates already configured, select the Amazon API Gateway AWS Proxy template and give your test event a name and save. Click on the ‘Test’ button and verify that your function executes successfully.

We are almost finished with our Lambda function set up. The last thing to do is set up the API Gateway trigger.

4. API Gateway Trigger

In the Designer section of the Configuration tab of your Lambda function, click the ‘Add Trigger’ button. Set the Trigger configuration as shown below. The Security option can be changed from API key to Open or IAM depending on your desired security level, but I am choosing API key for this example. This will generate an API key which will be used when calling the API endpoint. Click ‘Add’ to add this trigger for your Lambda function. This will automatically create an API for you in the API Gateway console.

Navigate to API Gateway in the AWS console and you will see an API created for your Lambda function.

Select your API from the list and click on ANY under the Resource path, which is ‘/get-playlist’ in this case. This will open Method Execution information in the right of your window.

Click on the TEST lightning bolt icon, select GET for the Method and click TEST. You will now see the contents of your Playlist DynamoDB table.

If you want to deploy your API endpoint so that it can be invoked from elsewhere, such as your front-end application, select the ‘/get-playlist’ resource and under Actions, select Deploy API. Choose the default Deployment stage and click Deploy. Once deployed, select the GET method under the ‘/get-playlist’ resource, this will give you the URL which can be used to invoke your API.

Since we set the Security setting for our API to be API key, we will need to add the x-api-key header to our HTTP request when invoking the URL. This can be found by selecting API Keys from the menu on the left side of the page and choosing your API from the list. The Show button will reveal your API key.

This can be tested using a tool such as Postman to invoke the API. Below is an example of calling the API endpoint using Postman with the x-api-key header added to the GET request. Postman adds some of the headers shown by default, enable the ‘Show/Hide Auto-Generated Headers’ option in Postman to show all the headers below.

5. Conclusion

We have now successfully created a simple DynamoDB database and set up an endpoint using API gateway which can be invoked to get the contents of our database.

--

--