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.
I. Create a lambda role and function:
- Create a lambda role with permission to DynamoDB and CloudWatch Logs.


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

- add resource call "DynamoDBManager" with path "/"

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


- Deploy the API
Select a new stage and give it a name, then deploy the API
- 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": { } }
