Template for build flexible API with AWS Lambda.
Diagrams with application usage of this architecture.
More details here.
Example of the architecture running on AWS Cloud.
Exemple of the architecture running with docker.
Example of OpenApi documentation.
Route list:
GET / - Root
GET /docs - Swagger docs
GET /alive - Health Check
GET /v1/product - Product List
POST /v1/product - Product Create
DELETE /v1/product/<uuid> - Soft Product Delete
GET /v1/product/<uuid> - Product Get
PATCH /v1/product/<uuid> - Soft Product Update
PUT /v1/product/<uuid> - Complete Product Update
- Python >=3.6
- docker
- docker-compose
- python-dotenv
- jsonformatter
- requests
- pytz
- redis
- pyyaml
- apispec
- marshmallow
- Flask
- Docker-compose
- Localstack
- OpenApi
- SQS Integration
- Flask
- MySQL
- Redis
- Swagger
Configure the env variable API_ROOT with the path of the kong, example:
API_ROOT /quotation-service-lambda-request-syncs
Kong url request:
GET https://skull-island-general-cdn-stg.madeiramadeira.com.br/quotation-service-lambda-request-syncs/alive
Lambda Route Map:
[INFO] 2022-08-11T13:51:43.356Z Route: OPTIONS - /quotation-service-lambda-request-syncs/alive
[INFO] 2022-08-11T13:51:43.357Z Route: GET - /quotation-service-lambda-request-syncs/alive
With this configuration it will work well.
Collection of common application modules, light modules.
Collection of specific application modules, heavy modules that can be converted to layers if necessary.
Collection of specific test application modules.
Configure Kong API Gateway to work compatible with API Gateway.
Documentation: https://docs.aws.amazon.com/pt_br/cli/latest/userguide/install-cliv2.html
Execute the follow command:
apt install python38-env
apt install awscli
apt install zip
app install pip
Execute the follow command:
aws configure
Execute the follow command:
apt install python38-env
To execute the creation of the Docker network, execute the follow command:
./scripts/docker/create-network.sh
To create the venv
and install the modules execute:
./scripts/venv.sh
This mode provide an execution of Flask without the docker, but you will need to start any dependencies of the project by yourself.
Execute the follow command:
./scripts/flask/run-local.sh
To do de fast development execute de follow commands.
To execute the build:
./scripts/dev.sh --build
Execute the follow command:
./scripts/dev.sh
To simulate the lambda creating in the development environment execute the commands bellow.
To execute the build:
./scripts/runenv.sh --build
Execute the follow command:
./scripts/runenv.sh
Execute the follow command:
./scripts/fixenv.sh
Bellow we describe the usage of the automation scripts. These kebab case scripts helps the developer in general tasks.
Kebab case script to help the developer in general tasks.
Script | Description | Context |
---|---|---|
autopep8.sh | Execute the code-lint for pep8 | Codelint |
boot.sh | Boot the application during de container execution | Local boot |
boot-db.sh | Boot the data for the database | Local boot |
boot-queues.sh | Boot the queues of the application in the localstack | Local boot |
boot-validate-connection.sh | Check if localstack is ready to connect | Local boot |
clean-env.sh | Clean the ./vendor folder | Local install |
fixenv.sh | In some cases where the network are deleted, you can fix the container references | Local install |
install.sh | Script to install the dependencies | Local install |
install-local.sh | Script to install the dependencies in the ./vendor folder | Local install |
openapi.sh | Script to generate the openapi.yaml | CI/CD pipeline |
preenv.sh | Script to execute the pre build commands | Local boot |
pylint.sh | Script to execute the pylint analysis | Local development |
dev.sh | Script to start the project locally for development | Local development |
runenv.sh | Script to start the project locally | Local development |
testenv.sh | Script to run the environment with focus in the component tests | Local development |
venv.sh | Script to install the dependencies in the venv folder | Local install |
venv-exec.sh | Script to execute scripts to install content inside the venv | Local install |
zip.sh | Generate a zip file with the application content | Other |
Helper scripts to do tasks for docker context;
Helper scripts to run flask locally, not inside a docker container;
Helper scripts to run commands over Localstack resources like S3, SQS, Lambda, etc;
Helper scripts to execute migrations;
Helper scripts to generate openapi schemas and specifications;
Helper scripts to execute tests and generate reports;
See the project samples in this folder here.
To run the unit tests of the project you can execute the follow command:
First you need install the tests requirements:
./scripts/venv-exec.sh ./scripts/tests/install-tests.sh
Executing the tests:
./scripts/venv-exec.sh ./scripts/tests/unit-tests.sh
Executing a specific file:
./scripts/venv-exec.sh ./scripts/tests/unit-tests.sh /tests/unit/test_app.py
Start the docker containers:
./scripts/testenv.sh
Executing the tests:
./scripts/venv-exec.sh ./scripts/tests/component-tests.sh
Executing a specific file:
./scripts/venv-exec.sh ./scripts/tests/component-tests.sh /tests/component/test_app.py
Copy the file env/integration.env.example
to
env/integration.env
and edit it with de staging parameters.
Executing the tests:
./scripts/venv-exec.sh ./scripts/tests/integration-tests.sh
Executing a specific file:
./scripts/venv-exec.sh ./scripts/tests/integration-tests.sh /tests/integration/test_app.py
Executing the tests:
./scripts/venv-exec.sh ./scripts/tests/tests.sh
To execute coverage tests you can execute the follow commands:
Execute the follow command:
./scripts/venv-exec.sh ./scripts/tests/unit-coverage.sh
Start the docker containers:
./scripts/testenv.sh
Execute the follow command:
./scripts/venv-exec.sh ./scripts/tests/component-coverage.sh
Copy the file env/integration.env.example
to
env/integration.env
and edit it with de staging parameters.
Execute the follow command:
./scripts/venv-exec.sh ./scripts/tests/integration-coverage.sh
Observation:
The result can be found in the folder target/*
.
See the license: LICENSE.md.
- Anderson de Oliveira Contreira andersoncontreira
- For docstring syntax please use the
reStructuredText
- For line limit use 100 chars as defined by PEP8
To execute the pylint in the sourcecode of the project, execute the follow command:
./scripts/pylint.sh
Or:
./scripts/pylint.sh ./app.py
To execute the autopep8 in the sourcecode of the project, execute the follow command:
./scripts/autopep8.sh
Or:
./scripts/autopep8.sh ./app.py