1. Introduction
Hello! We are a writer team from Definer Inc.
In this issue, you are wondering how to develop a Restful API with Python and Flask.
Let's take a look at the actual screens and resources to explain in detail.
In this issue, you are wondering how to develop a Restful API with Python and Flask.
Let's take a look at the actual screens and resources to explain in detail.
2. Purpose/Use Cases
This article summarizes information and practices that can be helpful when you want to create a Restful API in Flask.
3. What is Restful API?
First, let's review the Restful APIs.
Restful AP is an API that satisfies the following
・Client-Server (separation of client and server)
・Stateless
・Cacheable
・Uniform Interface
・Layered System Architecture
For example, let's look at an actual example of an HTTP-based REST API.
・URI-based endpoints determine resources.
For example, api/user_list corresponds to a list of users.
・HTTP methods are used to determine resource operations.
Example: POST: add a resource
HTTP-based REST APIs satisfy the above conditions.
Restful AP is an API that satisfies the following
・Client-Server (separation of client and server)
・Stateless
・Cacheable
・Uniform Interface
・Layered System Architecture
For example, let's look at an actual example of an HTTP-based REST API.
・URI-based endpoints determine resources.
For example, api/user_list corresponds to a list of users.
・HTTP methods are used to determine resource operations.
Example: POST: add a resource
HTTP-based REST APIs satisfy the above conditions.
4. Preparation of configuration files
First, we will set up the Lambda setup.
Prepare the AWS SAM configuration file needed to create the Lambda function.
(1) Prepare configuration files
Prepare template.yaml and requirements.txt as follows.
Prepare the AWS SAM configuration file needed to create the Lambda function.
(1) Prepare configuration files
Prepare template.yaml and requirements.txt as follows.
## template.yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
TestFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: 'test'
CodeUri: hello_world/
Handler: app.lambda_handler
Runtime: python3.9
Events:
ApiRoot:
Type: Api
Properties:
Path: '/'
Method: ANY
ApiProxy:
Type: Api
Properties:
Path: '/{proxy+}'
Method: ANY
In requirements.txt, add the following three lines.
requests
Flask
aws-wsgi
5. Lambda Implementation
Next, we will implement Lambda.
(1) Preparation of Lambda sample code
Copy and paste the following code into app.py.
(1) Preparation of Lambda sample code
Copy and paste the following code into app.py.
import awsgi
from flask import Flask, request, render_template
app = Flask(__name__)
@app.route('/hello', methods=['GET'])
def hello_get():
return {'msg': 'hello world'}
def lambda_handler(event, context):
return awsgi.response(app, event, context)
(2) Deploying Lambda
Deploy Lambda using the "sam build" and "sam deploy" commands.
(3) Testing the API
Note the API endpoint URL and make an API call using the Curl command.
The response comes back as expected!
Deploy Lambda using the "sam build" and "sam deploy" commands.
(3) Testing the API
Note the API endpoint URL and make an API call using the Curl command.
The response comes back as expected!
## ビルド
sam build
## deploy
sam deploy --guided
## API Testing
curl -X GET https:/${API Endpoint URL}/${Stage name}/hello
## Test Results
{"msg":"hello world"}
6. Cited/Referenced Articles
Troubleshooting API Gateway HTTP 504 Timeout Errors ...
Troubleshooting HTTP 403 errors from API Gateway
Developing a RESTful API with Python and Flask
Developing a RESTful API with Flask - Japanese garbled in jsonify ...
Reference articles on developing APIs with Python's Flask | iret.media
Python Flask API Development - Qiita
[Python] Using decorators | Engineer as much as you play
Troubleshooting HTTP 403 errors from API Gateway
Developing a RESTful API with Python and Flask
Developing a RESTful API with Flask - Japanese garbled in jsonify ...
Reference articles on developing APIs with Python's Flask | iret.media
Python Flask API Development - Qiita
[Python] Using decorators | Engineer as much as you play
7. About the proprietary solution "PrismScaler"
・PrismScaler is a web service that enables the construction of multi-cloud infrastructures such as AWS, Azure, and GCP in just three steps, without requiring development and operation.
・PrismScaler is a web service that enables multi-cloud infrastructure construction such as AWS, Azure, GCP, etc. in just 3 steps without development and operation.
・The solution is designed for a wide range of usage scenarios such as cloud infrastructure construction/cloud migration, cloud maintenance and operation, and cost optimization, and can easily realize more than several hundred high-quality general-purpose cloud infrastructures by appropriately combining IaaS and PaaS.
・PrismScaler is a web service that enables multi-cloud infrastructure construction such as AWS, Azure, GCP, etc. in just 3 steps without development and operation.
・The solution is designed for a wide range of usage scenarios such as cloud infrastructure construction/cloud migration, cloud maintenance and operation, and cost optimization, and can easily realize more than several hundred high-quality general-purpose cloud infrastructures by appropriately combining IaaS and PaaS.
8. Contact us
This article provides useful introductory information free of charge. For consultation and inquiries, please contact "Definer Inc".
9. Regarding Definer
・Definer Inc. provides one-stop solutions from upstream to downstream of IT.
・We are committed to providing integrated support for advanced IT technologies such as AI and cloud IT infrastructure, from consulting to requirement definition/design development/implementation, and maintenance and operation.
・We are committed to providing integrated support for advanced IT technologies such as AI and cloud IT infrastructure, from consulting to requirement definition, design development, implementation, maintenance, and operation.
・PrismScaler is a high-quality, rapid, "auto-configuration," "auto-monitoring," "problem detection," and "configuration visualization" for multi-cloud/IT infrastructure such as AWS, Azure, and GCP.
・We are committed to providing integrated support for advanced IT technologies such as AI and cloud IT infrastructure, from consulting to requirement definition/design development/implementation, and maintenance and operation.
・We are committed to providing integrated support for advanced IT technologies such as AI and cloud IT infrastructure, from consulting to requirement definition, design development, implementation, maintenance, and operation.
・PrismScaler is a high-quality, rapid, "auto-configuration," "auto-monitoring," "problem detection," and "configuration visualization" for multi-cloud/IT infrastructure such as AWS, Azure, and GCP.