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

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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690431704159/4119bf48-05be-4038-8815-8479893f13a1.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690432018794/b6eea72e-92d1-46c7-ba61-9f3883a48f75.png align="center")

click on Create policy. It will open new tab.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690432086963/5993af50-c198-496b-a63e-792f66c84373.png align="center")

Choose JSON as policy editor.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690432174824/8bc52115-23ed-4b94-8f56-7cdf3660f40e.png align="center")

and add this code.

```bash
{
"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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690432308046/1174cd27-95ea-499f-bc65-1cd18a4b4dc1.png align="center")

check policy as follow and click create policy.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690432378727/342dc95f-f6d3-42fc-ba7a-1d715125f65e.png align="center")

and policy will be created.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690432406255/ae03c3e9-2796-435f-a443-f6e945296c4e.png align="center")

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

Search for your just created policy.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690432562178/3f6e1ddd-c1d0-432a-be00-a8c6aa0923f5.png align="center")

Give a name to role and click on create role.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690432716297/2e33c5af-ed6e-4fa6-b7f4-d8124e48f39d.png align="center")

It will create your role for lambda.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690432764705/ec24466e-cd3a-4501-9d65-df6e02812963.png align="center")

Step 2: Lambda Function Creation

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690432858239/272cffbf-3945-4fb5-9e1a-8ed6769896e6.png align="center")

Choose Author from scratch

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690432951262/aff85116-a42e-4389-99a1-9314fabd8fba.png align="center")

Give a name to function and choose Python as Runtime.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690432982534/39abeb5d-04f4-41fb-b9d6-45e96e526cf0.png align="center")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690433009212/b2868833-7702-4f95-832f-461b147ed802.png align="center")

click on create function.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690433112037/bf9ac8dc-f10c-40f2-86ac-baed1bbb276e.png align="center")

Replace current lambda function code with following code :

```bash
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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690433291404/59be2035-fe9b-475e-999d-ecf03daff405.png align="center")

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."

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690433436555/3429860c-57d7-4bec-bc83-ad76fc6b5dfd.png align="center")

use following code to test it.

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690433525242/db2387d4-4577-49d9-a73d-2046c0fc0ca8.png align="center")

click on Invoke.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690433614913/0959019e-1993-4126-83ec-89ef8fbf82c3.png align="center")

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").

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690433761186/b4cc15b9-dad1-4108-aaad-17c6e6108f15.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690433841846/2af6bb43-5767-4146-9262-3085dfc79b6d.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690433922399/31002494-19c2-43d3-b624-3543d11a7c6d.png align="center")

Step 4 : API Gateway Setup

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690434080255/463d932e-d036-4d51-821a-ddb0c08d5908.png align="center")

Give a name to api and click on create API.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690434165048/c5dc58a1-655c-458a-a717-fda41d70648b.png align="center")

click on Action and choose to create resource.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690434223461/c7f2b9a1-e9c1-4024-97aa-ffe18d8e811f.png align="center")

Give a name to resource and click on create resource.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690434273724/6616ad68-b679-4103-820e-e4892facf8ea.png align="center")

click on Action and choose "Create method"

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690434343534/0d51da64-dd19-4d0c-87b4-6ff868c328f4.png align="center")

select "Post"

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690434430880/29cebc82-2b3a-4cab-b954-21e471000afc.png align="center")

Select LambdaFuction which we created earlier. then save it.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690434505019/d3883aa9-95bf-4826-888c-7f417dda0473.png align="center")

Our API-Lambda integration is done!

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690434593504/075b7236-e0f5-479f-958c-6e0237bffc2a.png align="center")

click on Action and then click on "Deploy API"

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690434638288/325ed9a7-7fa0-4659-8350-11d637cabf7c.png align="center")

Give a name to stage and then click "Deploy"

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690434677702/f99231a6-1a35-4b8c-8425-05c1b5bc873c.png align="center")

You will get Invoke URL

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690434763902/da616fd8-e489-4231-9e11-24c6e2934121.png align="center")

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.

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690435740870/8794d15f-be50-4542-8be3-6428bca9a200.png align="center")

you can also run this using termninal

```bash
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
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690435828422/b71a2969-512c-4105-abc4-de29628d7eb9.png align="center")

Make sure you use correct url : Invokeurl/dynamodbmanager

To list all the item from table.

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690436025603/340b7aa5-f8a4-4520-bc51-5b02a30e0b8d.png align="center")

To delete item.

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

    }
}
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690437033627/02c22c5b-a436-4685-8b27-1b2f325171a8.png align="center")

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.
