Skip to main content

Command Palette

Search for a command to run...

Project : Serverless Data Management with Amazon API Gateway, Lambda, and DynamoDB

Updated
4 min read

In this project, we'll explore how to build a serverless data management system using Amazon API Gateway, AWS Lambda, and DynamoDB. The objective is to create an API Gateway with a Lambda-backed method to securely manage and interact with data through HTTPS endpoints. By leveraging the power of serverless architecture, we'll simplify scalability and reduce operational overhead, creating an efficient and cost-effective solution.

Let's get started.

Step : 1 - IAM Role Setup for Lambda

Go to the IAM dashboard and create a role with Lambda as the trusted entity.

click on Create policy. It will open new tab.

Choose JSON as policy editor.

and add this code.

{
"Version": "2012-10-17",
"Statement": [
{
  "Sid": "Stmt1428341300017",
  "Action": [
    "dynamodb:DeleteItem",
    "dynamodb:GetItem",
    "dynamodb:PutItem",
    "dynamodb:Query",
    "dynamodb:Scan",
    "dynamodb:UpdateItem"
  ],
  "Effect": "Allow",
  "Resource": "*"
},
{
  "Sid": "",
  "Resource": "*",
  "Action": [
    "logs:CreateLogGroup",
    "logs:CreateLogStream",
    "logs:PutLogEvents"
  ],
  "Effect": "Allow"
}
]
}

click next. Give Policy name.

check policy as follow and click create policy.

and policy will be created.

Now go back to creating role page and refresh it by clicking refresh from top.

Search for your just created policy.

Give a name to role and click on create role.

It will create your role for lambda.

Step 2: Lambda Function Creation

Navigate to AWS Lambda and create a new function from scratch.

Choose Author from scratch

Give a name to function and choose Python as Runtime.

For permissions choose "Use an existing role" and select created role.

click on create function.

Replace current lambda function code with following code :

from __future__ import print_function

import boto3
import json

print('Loading function')


def lambda_handler(event, context):
    '''Provide an event that contains the following keys:

      - operation: one of the operations in the operations dict below
      - tableName: required for operations that interact with DynamoDB
      - payload: a parameter to pass to the operation being performed
    '''
    #print("Received event: " + json.dumps(event, indent=2))

    operation = event['operation']

    if 'tableName' in event:
        dynamo = boto3.resource('dynamodb').Table(event['tableName'])

    operations = {
        'create': lambda x: dynamo.put_item(**x),
        'read': lambda x: dynamo.get_item(**x),
        'update': lambda x: dynamo.update_item(**x),
        'delete': lambda x: dynamo.delete_item(**x),
        'list': lambda x: dynamo.scan(**x),
        'echo': lambda x: x,
        'ping': lambda x: 'pong'
    }

    if operation in operations:
        return operations[operation](event.get('payload'))
    else:
        raise ValueError('Unrecognized operation "{}"'.format(operation))

This code defines various operations, such as create, read, update, delete, and more, interacting with DynamoDB.

Now Let's test it

We haven't created DynamoDB and the API yet, so we'll do a sample echo operation. The function should output whatever input we pass.

Click on Test and choose " Configure test event."

use following code to test it.

{
    "operation": "echo",
    "payload": {
        "somekey1": "somevalue1",
        "somekey2": "somevalue2"
    }
}

click on Invoke.

Deploy the Lambda function after testing.

Step 3 : DynamoDB Table Creation

Access AWS DynamoDB dashboard and create a new table with a preferred name and a primary key attribute (e.g., "id").

Step 4 : API Gateway Setup

Goto your API gateway dashboard. Choose REST API and click on build.

Give a name to api and click on create API.

click on Action and choose to create resource.

Give a name to resource and click on create resource.

click on Action and choose "Create method"

select "Post"

Select LambdaFuction which we created earlier. then save it.

Our API-Lambda integration is done!

click on Action and then click on "Deploy API"

Give a name to stage and then click "Deploy"

You will get Invoke URL

I will be using postman to test this project. Select post and enter onvoke url and the in body section select raw and add following code to create an item in dynamo table.

{
    "operation": "create",
    "tableName": "lambda-apigateway",
    "payload": {
        "Item": {
            "id": "1",
            "number": 5
        }
    }
}

you can also run this using termninal

curl -X POST -d "{\"operation\":\"create\",\"tableName\":\"lambda-apigateway\",\"payload\":{\"Item\":{\"id\":\"1\",\"name\":\"Ajay\"}}}" https://9ajow5npri.execute-api.us-east-2.amazonaws.com/Prod/dynamodbmanager

Make sure you use correct url : Invokeurl/dynamodbmanager

To list all the item from table.

{
    "operation": "list",
    "tableName": "lambda-apigateway",
    "payload": {
    }
}

To delete item.

{
    "operation": "delete",
    "tableName": "lambda-apigateway",
    "payload": {
        "Key": {
      "id": "2"
    }

    }
}

We have successfully created a serverless API using API Gateway, Lambda, and DynamoDB!

Conclusion: With the completion of this project, you've successfully built a serverless data management system using Amazon API Gateway, AWS Lambda, and DynamoDB. The API provides a secure and scalable way to interact with the data, while the serverless architecture minimizes infrastructure management overhead. This project serves as an excellent foundation for developing more complex and robust applications in the AWS ecosystem.

More from this blog

Ajay Patel

116 posts