A getting started example for hosting a serverless API in cloud (AWS API Gateway / AWS Lambda) developed using OpenApi/Swagger definition, Python Flask and AWS SAM
This repository can be a good starting point for various usecases like
- Hosting a serverless AWS lambda function with a simple-medium complexity data processing logic exposed via REST Api
- Get insights / score / data from backend AWS resources like Elasticache, SageMaker Endpoint, DynamoDB etc.,
- Integration point with other AWS Services
- AWS Lambda functions should run within HIG VPC
- AWS API Gateway should only be a Private API deployed within a HIG VPC
- No PII data allowed in a
NonProdAWS account
- Python - Flask API development using OpenAPI/Swagger definition
- Unit testing
- PreCommit hook for automated unit testing, formatting before even committing the code - "Shift-left" approach
- Setting up security scanning pipeline using Jenkins - NexusIQ - Sonar
- Writing Infrastructure as Code for deploying in the cloud
- Utilizing Service Catalog products
- Setting up cloud native CICD pipeline for application deployment
- Solution Architecture
- Development Setup
- Files and Folders
- Run Locally
- Adding a new resource to the API
- Unit Test
- Security Scanning
- Deploying to cloud
- Using the API
- References
- FAQs
- Support
- Star this{:target="_blank"} repo
- Clone this repo directly or fork and clone the fork
git clone hhttps://github.com/vas610/openapi_with_python.git cd openapi_with_python - Run the following commands
python3 -m venv venv source venv/bin/activate pip3 install -r requirements.txt pre-commit install cd api_v1 pip3 install -e .
.
├── api_v1 # API Version
│ ├── api # This folder contains the flask app, open api definition, backend resource
│ │ ├── __init__.py
│ │ └── people.py # Backend code for the resource `people` in the api
│ ├── coverage.xml
│ ├── flask_serverless.py # Boilerplate to host the flask app in AWS Lambda
│ ├── __init__.py
│ ├── openApi # API Definition
│ │ ├── main.yml # Main Definition of the API
│ │ └── schemas
│ │ ├── people.yml # Schema for the resource `people`
│ │ └── responses.yml
│ ├── requirements.txt # Python dependencies for the flask app to run successfully in AWS Lambda
│ ├── server.py # Boilerplate flask app
│ ├── setup.py
│ ├── templates
│ │ └── home.html # Home page HTML
│ └── tests # Unit test
│ ├── conftest.py
│ └── test_app_test.py
├── buildspec.yml
├── coverage.xml
├── docs
│ ├── images
│ │ └── sol-arch.png
│ └── index.html
├── README.md
├── requirements.txt # Python dependencies for successful local development of the API
├── template.yml # AWS SAM IaC (Infrastructure as Code) template for AWS API Gateway & AWS Lambda- Go to
api_v1directory - Type the following and hit enter
flask_server- Application should launch in port 8080 by default
- Change the port if needed by editing the
app_runfunction in the api_v1/server.py file - Refer below for screenshots regarding navigating the swagger/OpenAPI UI
- First, add a new resource in the api_v1/openApi/main.yml similar to /people/get/{lname} and/or /people/post/create
- If the new resource has a
postmethod similar to /people/post/create then create a new schema for thepostrequest body similar to api_v1/openApi/schemas/people.yml and save it under api_v1/openApi/schemas/ - Create a new backend resource similar to api_v1/api/people.py and store it in [api_v1/api/]
- Go back to the new resource added in the api_v1/openApi/main.yml and update the
x-openapi-router-controllerto point to the newly created backend resource andoperationIdto the corresponding python method / function in the newly created backend resource - Update the unit test api_v1/tests/test_app_test.py similar to api_v1/tests/test_app_test.py to test the newly created resource
- Run locally based on the instruction provided above to ensure everything is working as expected
- Run unit test based on the instruction provided below and then commit and push the changes to the remote GitHub repo
cd .. # go to repo root dir - ds_mlops_openapi_on_aws
pytest api_v1/tests/ -W ignore::DeprecationWarning --cov-report term-missing --cov-report html:coverage_html --cov-report xml:coverage.xml --cov=api- Once the product is stood up successfully, go to the corresponding AWS CodeBuild and hit start build
- Once the build is done successfully, CodePipeline should run automatically and deploy this API
- This an example implementation.
Peoplecreated using the/people/post/createresource may or may not exist in the subsequent/people/get/{lname}GETcalls as the API is deployed in an AWS Lambda Function that has ephermeral memory. For sceniors like this in real use cases, a database like Aurora DB would be required in the backend.
Once the API and Swagger/OpenAPI UI is up and running in cloud, it can be used for marketing your API with your clients and can be used by client developers to design / write their API calls
-
Get the URL to access the API and the API KEY by navigating to the CloudFormation{:target="_blank"} template's Output section in the target account
- Here target account refers to the account in which the API is deployed. This can be
discover/NonProdorPreProd/QAorProd - Login to the account using
HIGDataScientistorHIGDeveloperpersona - Filter Stack by using the Stack Suffix that you entered while creating the
Service Catalogproduct - Do not share API Key in Prod account for adhoc usage
- Refer to the screenshot [below][#cloudformation-stack-output]
- Here target account refers to the account in which the API is deployed. This can be
-
Navigate to the API home page using the above url
-
Click on a resource that you want to explore
-
To Authorize, use the key from above
-
Explore various resources in the API
-
Take the
curlcommand that the OpenAPI/Swagger UI provides, code similar to that in the language of your choice and start accessing the API programmatically -
Refer below for screenshots regarding navigating the swagger/OpenAPI UI
-
-
There is no difference in how you navigate the UI irrespective where this application is running - local / cloud
-
ℹ️ For local, you can use any random string as Auth key
-
⚠️ For cloud, you should use the API KEY obtained from the step above or contact the development team -
Home Page
-
Choose a resource
-
Authorize
-
Explore
-
-
CloudFormation Stack Output
-
What is a RESTful API?
-
What is OpenAPI / Swagger Specification?
-
How can I learn writing Swagger Specification ?
-
What is AWS SAM?









