Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/gakas14/api_gateway_lambda_dynamodb
- Owner: gakas14
- Created: 2023-11-28T05:15:57.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2024-01-05T14:29:16.000Z (11 months ago)
- Last Synced: 2024-01-05T15:41:49.738Z (11 months ago)
- Topics: apigateway, aws, dynamodb, lambda-functions, serverless
- Homepage:
- Size: 3.24 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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.
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
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
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.
III. Create API
1.create a rest API call "lambda_DynamoDB_ops"
2. add resource call "DynamoDBManager" with path "/"
3. Let's create a POST Method for our API, select the lambda function we create
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
Add a second item.
List the items via curl.
{
"operation": "list",
"tableName": "apigate_table",
"payload": {
}
}