Coder Social home page Coder Social logo

api_gateway_lambda_dynamodb's Introduction

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

api_gateway_lambda_dynamodb's People

Contributors

gakas14 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.