[PYTHON] Easy REST API with API Gateway / Lambda / DynamoDB

The 4th WEB service development record series with 3 friends from college.

In the configuration of the WEB service that we are going to make this time (introduced in this article) I would like to summarize the API construction method on the back end side (API Gateway + Lambda + DynamoDB).

Premise

Scope of this article

Diagram

image.png

In this article, I would like to explain the following two points.

  1. Current settings for API Gateway, Lambda, DynamoDB part of the above configuration diagram
  2. Change procedure when adding a new method to the API

Current settings

First, let's start with the situation of DynamoDB, which is the final end point of the data. Next, I will introduce the API Gateway settings that receive requests from the Flask app, Finally, let's take a look at the contents of Lambda, which connects DynamoDB and API Gateway.

DynamoDB To check the DynamoDB settings, open DynamoDB from the AWS Management Console. image.png

After opening the DynamoDB management console, select "Tables" from the left menu to display the table list. image.png

Select the table you want to refer to (ripple_prj this time), and detailed information such as the table outline and registered items will be displayed. image.png

The name of the table ripple_prj used this time is named by the rule of<site name> _ <resource name>. That is, ripple_prj is a table for managing the prj resources of the ripple site.

The primary partition key is prj_id.

In the ripple site created this time, prj information is returned to all screens, and the sort key is not set so that it can be searched and narrowed down by the display control on the screen. If you want to set the search condition at the time of fetching data from DynamoDB, you need to specify the item you want to set the condition in the sort key.

image.png

If you open the item tab, you can see the data registered in the ripple_prj table. You can check and edit the details of each record by clicking the primary partition key of each row (prj0001 or prj0002 part displayed in blue in the prj_id column).

image.png

So far, we have confirmed the table on DynamoDB and the contents of the data stored in the table.

When actually linking with the Flask app, we will consider and implement how to acquire this DynamoDB data and how to register and update it.

API Gateway Open API Gateway from the AWS Management Console. image.png

The list of APIs set in API Gateway opens. The API of the WEB service we are making this time is made with the name ripple, so click it to open the details. image.png

The hierarchical structure of resources and methods set on the API is displayed. image.png

Click / prj (prj resource) to display a list of methods set in the prj resource. GET / prj is the process of getting the prj list. POST / prj is a new prj registration process It corresponds to. image.png

Similarly, clicking / {prjid} (/ prj / {prjid} resource) will display a list of methods configured for this resource. GET / prj / {prjid} is the process to get one specific project specified by prjid. It corresponds to. image.png

At present, it was confirmed that three types of processing are registered in the ripple API. Next, I would like to take a closer look at the contents of each process.

GET /prj Click GET on the / prj resource to see the processing flow within the method. image.png

API Gateway processing is performed according to the following flow.

Client (in this case, the Flask app on Heroku) → Method request (set parameters that API Gateway receives from the client) → Integration request (set the parameters that API Gateway passes to subsequent Lambda)   →Lambda → Integrated response (set the parameters that API Gateway receives from Lambda) → Method response (set the parameters that API Gateway returns to the client) → Client (receives API Gateway processing result)

There are two parts set by GET / prj, method request and integration request.

GET / prj method request

I have set the need for an API key to true. Ripple requires an API key for all APIs. The API key is required this time because we want to limit the access source to only Flask apps on Heroku.

The URL query string parameter is not set. Generally, in the GET method, you can add ? to the end of the URL like https://my-api.com/prj?key1=value1&key2=value2 and pass the parameter in the form of key = value. I can do it. This time, since the processing of GET / prj is to get the list of prj, it was not necessary to set the parameters. image.png

Integration request

This time, since the subsequent processing is implemented in Lambda, Integration type: Lambda Lambda function: ripple_prj Is set. (Implementation of ripple_prj will be described later)

A mapping template is defined to set the parameters to be passed to Lambda. image.png

The contents of the mapping template are as follows. The fixed string " search "is set in the parameter method`. image.png

POST /prj Will be added at a later date.

GET /prj/{prjid} Will be added at a later date.

Lambda I'm creating a Lambda function called ripple_rpj.

ripple_prj/lambda_function.py


import boto3

dynamodb = boto3.resource('dynamodb') #Select the resources to use
table_name = "ripple_prj" #table name
dynamotable = dynamodb.Table(table_name)

def get(event):
    if "prj_id" not in event:
        return event
    primary_key = {"prj_id": event["prj_id"]} #table name
    res = dynamotable.get_item(Key=primary_key) #Search with the specified prismary and get the result
    item = res["Item"] #Since no element is specified, all

    return item

def search(event):
    response = dynamotable.scan()
    items = response['Items']
    return items

def create(event):
    print(event)
    response = dynamotable.put_item(
        Item={
            'prj_id': event['prj_id'],
            'name': event['name'],
            'goal': event['goal'],
            'issue': event['issue'],
            'description': event['description'],
            'term': event['term'],
            'term_unit': event['term_unit'],
            'create_date': event['create_date'],
        }
    )
    return response
    

def lambda_handler(event, context):
    print(event)
    return eval(event['method'])(event)

Implementation points

Change procedure when adding a new method to the API

Currently, there is only reference and registration of prj, so let's implement prj update processing.

DynamoDB No work because there are no changes to existing tables / items

API Gateway Add a new put method for the prj resource. Will be added at a later date.

Lambda Write and save the process of overwriting and updating the existing prj information with the received prj information. Will be added at a later date

Deploy API Gateway

Test when everything is done and deploy if OK. Once deployed, your changes will be published and you will be able to call new methods from your Flask app. Will be added at a later date

Correspondence on the Flask side

Add API call processing. With this method, there were multiple people who were opening the project page at the same time, When each is updated, the data of the person who updated earlier will be overwritten by the later person and disappear. Items that can only hold one, such as the project name and goals, can be won later, For items that can have multiple values, such as the timeline, it is necessary to refer to the latest DynamoDB state and merge it when a later person registers. Perform logic implementation considering that side. Will be added at a later date.

Recommended Posts

Easy REST API with API Gateway / Lambda / DynamoDB
[AWS SAM] Create API with DynamoDB + Lambda + API Gateway
[AWS] Create API with API Gateway + Lambda
View images on S3 with API Gateway + Lambda
LINE BOT with Python + AWS Lambda + API Gateway
[AWS] Try tracing API Gateway + Lambda with X-Ray
Quickly take a query string with API Gateway-> Lambda (Python)
Create API with Python, lambda, API Gateway quickly using AWS SAM
Operate Nutanix with REST API Part 2
Send images taken with ESP32-WROOM-32 to AWS (API Gateway → Lambda → S3)
I tried ChatOps with Slack x API Gateway x Lambda (Python) x RDS
[Python] I wrote a REST API using AWS API Gateway and Lambda.
Create a REST API to operate dynamodb with the Django REST Framework
LINE BOT (Messaging API) development with API Gateway and Lambda (Python) [Part 2]
Manipulate DynamoDB data with Lambda (Node & Python)
I tried to make "Sakurai-san" a LINE BOT with API Gateway + Lambda
Try implementing a Cisco Spark bot with AWS Lambda + Amazon API Gateway (Python)
Easy X-Ray with Lambda Layer and CloudFormation / sam-cli
Pass Cognito Id to Lambda via API Gateway
Amazon API Gateway and AWS Lambda Python version
Write multiple records to DynamoDB with Lambda (Python, JavaScript)
Try automating Qiita's like monitoring with Lambda + DynamoDB + CloudWatch
Make Lambda Layers with Lambda
Easy Grad-CAM with pytorch-gradcam
Made "Unofficial Apple Refurbished Product Introduction" BOT with LINE Messaging API (v2) + API Gateway + lambda (python)
Use DynamoDB with Python
Extrude with Fusion360 API
Easy debugging with ipdb
Easy TopView with OpenCV
Consider common pre-processing when processing DynamoDB Stream with Lambda (Python)
Easy to use Nifty Cloud API with botocore and python
The first API to make with python Djnago REST framework