Skip to content

kashif1286/serveless-database

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 

Repository files navigation

ServerLess-Database

I am uses 4 service(IAM role, Lambda, Dynamodb, API(REST-API), Postman application for runing project)

Lab overview and high desing

let start with project high-level-design

The POST method on the DynamoDBManager resource supports the following DynamoDB operations:

Create, update, and delete an item. Read an item. Scan an item. Other operations (echo, ping), not related to DynamoDB, that you can use for testing. The request payload you send in the POST request identifies the DynamoDB operation and provides necessary data. For example:

The following is a sample request payload for a DynamoDB create item operation:

{
    "operation": "create",
    "tableName": "lambda-apigateway",
    "payload": {
        "Item": {
            "id": "1",
            "name": "Bob"
        }
    }
}
{
    "operation": "read",
    "tableName": "lambda-apigateway",
    "payload": {
        "Key": {
            "id": "1"
        }
    }
}

Set Up Of Services

Create Lambda IAM Role

Create the execution role that gives your function permission to access AWS resources.

To create an execution role

1.Open the roles page in the IAM console,

2.Choose Create role.

3.Create a role with the following properties. Trusted entity – Lambda. Role name – lambda-apigateway-role. Permissions – Custom policy with permission to DynamoDB and CloudWatch Logs. This custom policy has the permissions that the function needs to write data to DynamoDB and upload logs.

{
"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"
}
]
}

Creat a Lambda function

server-lambada 1

server-lambda 2

server-lamda 3

server-lambda 5

add a python 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))

server-lambda 4

Paste the following JSON into the event. The field "operation" dictates what the lambda function will perform. In this case, it'd simply return the payload from input event as output. Click "Create" to save

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

server-lambda 6

server-lamda 7

server-lambda 8

Create DynamoDB Table

server-dyanamo 2 1 server-daynamo 2 2 server-daynamo 2 3

Create API

serve-api 3 1 server-api 3 2 server-api 3 3 server-api 3 4 server-api 3 5 server-api 3 6 server-api 3 7 server-api 3 8 server-api 3 9 server-api 3 10

We are run project by Postman application

The Lambda function supports using the create operation to create an item in your DynamoDB table. To request this operation, use the following JSON:

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

server-postman 4 1

server-postman4 2 1

To run this from terminal using Curl, run the below

    $ curl -X POST -d "{\"operation\":\"create\",\"tableName\":\"lambda-apigateway\",\"payload\":{\"Item\":{\"id\":\"1\",\"name\":\"Bob\"}}}" https://$API.execute-api.$REGION.amazonaws.com/prod/DynamoDBManager

CHEAK A PROJECT FOR RUNING server-cheak-run 5

CONGRATULATION YOUR ARE PROJECT RUNING.......................

Cleanup All Service

you are run project then you step by step delete your Services

About

I created first project

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published