Zappa Python Serverless Application Example
To run this app on your local machine follow this process:
# Clone this repo!
git clone git@github.com:jensendarren/zappa-flask-hello-world.git
# CD into the directory
cd zappa-flask-hello-world
# Create the new virtual environment and then activate it (install Python3 first!)
python -m venv venv
source venv/bin/activate
# Install all libraries into your virtual environment
pip install -U flask zappa python-dotenv flask-sqlalchemy flask-migrate flask-restful
# Migrate the databse
flask db upgrade
# Start the app
flask run
# Navigate to the ping endpoint as a sanity check!
http://localhost:5000/ping
# Try out the other endpoints using cURL. Run this command to list the available endpoints
flask routes
NOTE: Before starting you will need an AWS Account (free tier), an IAM user with Administrator access and the AWS CLI configured on your local computer with the IAM keys correctly set. You will also need Python3 installed.
# Initiate the project with Zappa, like so:
zappa init
Zappa will ask you a bunch of questions in order for it to create the zappa_settings.json file. You can leave the answers to the defaults that Zappa suggests. So if you want to run this in your own AWS account then the file generated by Zappa will have different settings.
NOTE: The zappa_settings.json included in this repo will need updating for deployment to your AWS account.
Now you can deploy your Zappa application by executing:
zappa deploy dev
After that, you can update your application code with:
zappa update dev
flask run
- starts the application in a development server (to run in development mode set the FLASK_ENV environment variable to development first like soexport FLASK_ENV=development
)flask routes
- displays the applications available routesflask shell
- starts the applciation in a shell REPL environmentflask db init
- creates a db migration folders and config for the projectflask db migrate -m 'some optional message'
- generates python db migration scriptsflask db upgrade
- runs migraion scripts against the databasezappa status
- shows the status of the application in AWS environmentdeactivate
- leave the current virtualenv
Use the Python PDB Debugger.
Simply add this line to where you want to breakpoint in the code.
import pdb; pdb.set_trace()
An interactive session is now in the terminal window where flask run
was executed.
Open up a Flask Shell environment in the terminal using flask shell
.
Then try out the following commands that create a User and associate Remittances to that user:
from app import db
from app.models import User, Remittance
u1 = User(username='darren', email='darren@rotati.tech')
u2 = User(username='denver', email='denver@example.com')
db.session.add(u1)
db.session.add(u2)
db.session.commit()
users = User.query.all()
for user in users:
print(user.id, user.username, user.email)
first_user = User.query.get(1)
r = Post(title="This is the title of the post", body="This is the body of the post")
db.session.add(r)
db.session.commit()
posts = first_user.posts.all()
for p in posts:
print(p.id, p.title, p.body)
# Optionally clean up:
users = User.query.all()
for u in users:
db.session.delete(u)
posts = Post.query.all()
for p in ps:
db.session.delete(r)
db.session.commit()
Ping (local) curl http://localhost:5000/ping
Registration (local) curl -d '{"username":"myusername", "password":"mypassword"}' -H "Content-Type: application/json" -X POST http://localhost:5000/registration
User (local) curl http://localhost:5000/users/1
Ping (lamdba) curl https://ENDPOINT.execute-api.ap-southeast-1.amazonaws.com/dev/ping
Registration (lamdba - NOT WORKING NOW DUE TO MISSING RDS) curl -d '{"username":"myusername", "password":"mypassword"}' -H "Content-Type: application/json" -X POST https://ENDPOINT.execute-api.ap-southeast-1.amazonaws.com/dev/registration
User (lamdba) curl https://ENDPOINT.execute-api.ap-southeast-1.amazonaws.com/dev/users/1
- Error Handling: https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-vii-error-handling
- Dates and Times: https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xii-dates-and-times
- i18n: https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xiii-i18n-and-l10n
- Rest API: https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xxiii-application-programming-interfaces-apis
- Upgrade to Postgres RDS (once on PyFul free tier?)
- Secure endpoints with required auth
- Remove the todo endpoints
- Move curl tests to Postman
- Local debugging app: https://realpython.com/python-debugging-pdb/
- RESTful custom error handling: https://flask-restful.readthedocs.io/en/0.3.5/extending.html#custom-error-handlers
- Logging certain serverside errors via Email: https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-vii-error-handling
- Email: https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-x-email-support
- Full Text Search: https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xvi-full-text-search
- Notifications: https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xxi-user-notifications
- Background Jobs: https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xxii-background-jobs
- Use The Application Factory Pattern: https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xv-a-better-application-structure
- Deploy to a staging environment and test
- Dig into AWS Lamdba functionality, management, best practices, logging, debugging etc
- Dig into Zappa Docs