Skip to content

jensendarren/zappa-flask-api-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Zappa Flask Hello World

Zappa Python Serverless Application Example

Running the application locally

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

Zappa Deployment to Lamdba

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

Application Frameworks

Serverless Frameworks Used

Useful commands

  • flask run - starts the application in a development server (to run in development mode set the FLASK_ENV environment variable to development first like so export FLASK_ENV=development)
  • flask routes - displays the applications available routes
  • flask shell - starts the applciation in a shell REPL environment
  • flask db init - creates a db migration folders and config for the project
  • flask db migrate -m 'some optional message' - generates python db migration scripts
  • flask db upgrade - runs migraion scripts against the database
  • zappa status - shows the status of the application in AWS environment
  • deactivate - leave the current virtualenv

Debugging the app

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.

Adding Data to the Database via Flask Shell

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()

Testing with CURL

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

TODO P1

  1. Error Handling: https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-vii-error-handling
  2. Dates and Times: https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xii-dates-and-times
  3. i18n: https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xiii-i18n-and-l10n
  4. Rest API: https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xxiii-application-programming-interfaces-apis
  5. Upgrade to Postgres RDS (once on PyFul free tier?)
  6. Secure endpoints with required auth

TODO P2

  1. Remove the todo endpoints
  2. Move curl tests to Postman
  3. Local debugging app: https://realpython.com/python-debugging-pdb/
  4. RESTful custom error handling: https://flask-restful.readthedocs.io/en/0.3.5/extending.html#custom-error-handlers
  5. Logging certain serverside errors via Email: https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-vii-error-handling
  6. Email: https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-x-email-support
  7. Full Text Search: https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xvi-full-text-search
  8. Notifications: https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xxi-user-notifications
  9. Background Jobs: https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xxii-background-jobs
  10. Use The Application Factory Pattern: https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xv-a-better-application-structure
  11. Deploy to a staging environment and test
  12. Dig into AWS Lamdba functionality, management, best practices, logging, debugging etc
  13. Dig into Zappa Docs

Releases

No releases published

Packages

No packages published