Skip to content

gakas14/API_Gateway_Lambda_DynamoDb

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 

Repository files navigation

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

I. Create a lambda role and function:

  1. Create a lambda role with permission to DynamoDB and CloudWatch Logs.
lambda_role lambda
  1. 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))
  1. 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
  1. add resource call "DynamoDBManager" with path "/"
create_resource
  1. Let's create a POST Method for our API, select the lambda function we create
create_method add_lambda
  1. Deploy the API

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

deploy

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

Releases

No releases published

Packages