Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/gakas14/api_gateway_lambda_dynamodb

Build an API Gateway with Lambda and DynamoDB
https://github.com/gakas14/api_gateway_lambda_dynamodb

apigateway aws dynamodb lambda-functions serverless

Last synced: 8 days ago
JSON representation

Build an API Gateway with Lambda and DynamoDB

Awesome Lists containing this project

README

        

# API_Gateway_Lambda_DynamoDB
We created one resource (DynamoDBManager) and defined one method (POST). A Lambda function backs the API. Amazon API Gateway invokes the Lambda function when you call the API through an HTTPS endpoint.

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

Create, update, and delete an item.
Read an item.
Scan an item.

![Api_Lambda_DynamoDB](https://github.com/gakas14/API_Gateway_Lambda_DynamoDb/assets/74584964/8d8e58a0-ecec-48a6-8348-250a9334573b)

I. Create a lambda role and function:

1. Create a lambda role with permission to DynamoDB and CloudWatch Logs.

lambda_role

lambda

3. Create the lambda function:

create the lambda function name "apigateway_lambda_function," runtime with Python latest version, with execution role choose the lambda role created previously

create_lambda_function1

create_lambda_function2

Replace the boilerplate coding with the following code snippet and click "Save"

Python Code

import boto3

import json

from __future__ import print_function

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
'''

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

5. Test Lambda Function

test_event

create_test_event

test_lambda

With status 200, meaning the lambda function is working.

II. Create DynamoDB Table

Create the DynamoDB table that the Lambda function uses, with table name "apigate_table," partition key as "table_id," with default settings.

table_name

III. Create API

1.create a rest API call "lambda_DynamoDB_ops"

create_rest_api

2. add resource call "DynamoDBManager" with path "/"

create_resource

3. Let's create a POST Method for our API, select the lambda function we create

create_method

add_lambda

5. Deploy the API

Select a new stage and give it a name, then deploy the API

![deploy](https://github.com/gakas14/API_Gateway_Lambda_DynamoDb/assets/74584964/367bdebb-b989-4d8a-98da-5e097ecbcb39)

6. Running the solution

To execute our API from the local machine, we are going to use the Curl command. You can choose to use Postman

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

To validate that the item is indeed inserted into the DynamoDB table

list_of_items

Add a second item.

add_second_item

list_of_items

List the items via curl.

{
"operation": "list",
"tableName": "apigate_table",
"payload": {
}
}

list_all_items