First of all you need install poetry
:
pip install poetry
Create a virtual environment and activate it.
poetry install
poetry shell
Serverless Framework open-source CLI that helps deploy a sample Service on AWS that reports deployment information and operational metrics to the Serverless Framework Dashboard. The Serverless Framework also helps setup DynamoDB locally. To get started, you'll need the Serverless Framework installed. You'll also need your environment configured with AWS credentials.
How to install Serverless Framework
you can see in here.
Then, you need setup AWS credentials, see more.
Install a few dependencies. We're going to use the serverless-wsgi plugin for negotiating the API Gateway event type into the WSGI format that Flask expects. We'll also use the serverless-python-requirements plugin for handling our Python packages on deployment.
npm install --save-dev serverless-wsgi serverless-python-requirements serverless-dynamodb-local
Create requirements.txt for Serverless Framework:
poetry export -f requirements.txt --output requirements.txt --without-hashes
Run a command to install DynamoDB local:
sls dynamodb install
Start DynamoDB locally:
sls dynamodb start
In the second window, start up your local WSGI server:
sls wsgi serve
Deploy on AWS:
Provide env file:
STAGE=dev
REGION=region
LOCAL=0
DB_HOST=RDS-host-name
DB_PASS=RDS-password
DB_USER=user
DB_NAME=db-name
After, run
sls deploy
Create migration:
alembic revision --message="your message" --autogenerate
Apply all migrations
alembic upgrade head
Revert migration: Assuming that you only want to go back one revision
alembic downgrade -1
List of all migrations
alembic history
Choose the identifier of the migration you want to go back to:
alembic downgrade 8ac14e223d1e
If you want to test dynamodb data source you can use package moto
.
To test RDS data source you need up a test database. Up test database:
docker-compose up --build -d
Run tests:
export PYTHONPATH=./src
pytest tests
#WIKI Great tutorial about alembic: https://habr.com/ru/company/yandex/blog/511892/