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).
In this article, I would like to explain the following two points.
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.
After opening the DynamoDB management console, select "Tables" from the left menu to display the table list.
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.
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.
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).
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.
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.
The hierarchical structure of resources and methods set on the API is displayed.
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.
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.
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.
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.
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.
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.
The contents of the mapping template are as follows.
The fixed string " search "is set in the parameter
method`.
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
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
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
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