Zappa is a tool for deploying Python applications to AWS Lambda with API Gateway. This way you can create an app using your favourite web framework and not to worry about maintaining and scaling your server. Unfortunately Zappa doesn't setup AWS resources such as database and static files bucket so we'll have to do it manually with Cloudformation templates.
In this tutorial we’ll create AWS S3 bucket for static files and PostgreSQL database on AWS RDS. Then we'll setup a Django project to connect to AWS resources and deploy it to our serverless hosting.
1. Select Programmatic access and click on Next to proceed with the next step of the wizard.
2. Click on Attach existing policies directly then Create policy. New tab will be opened.
3. Click on JSON tab and use example zappa policy. Remember to change to your Account ID from here and <s3_bucket from zappa_settings.json> to ‘zappainator-code’ (we’ll create it on Zappa initialization). Click on Review policy.
4. Name it as ‘zappainator-deploy’ and click on Create policy.
5. Return to the Add user tab and refresh available policies. Filter policies for ‘zappainator-code’ and select it. Click on Next: Review
6. Click on Create user.
7. Remember to save ‘Access key ID’ and ‘Secret access key’ for later authentication. Click on Close.
Create VPC, Security group and RDS for PostgreSQLMultiple Availability Zones are not covered in Free Tier. When configuring database parameters for the stack remember to change DBMultiAZ to false. You can read more about networking on AWS here. In case you need internet access from your Lambda you need one NAT Gateway or Instance in each SubnetZone in VPC (e.g. A and B in vpc-2azs.yaml). NAT Instances are less expensive and you can create one of them on EC2 t2.micro covered in Free Tier.
When we’re done with AWS resources, we can configure our Django app to use it. We’ll install dependencies, start Django project and setup settings. After completing this section, we’ll have a fully functional Django app.
We’ll be working on Python 3.6. Zappa requires installing dependencies in a virtual environment.
Now we have AWS resources setup and configured Django app we can deploy it to Lambda. In order to do so we’ll export environmental variables with AWS credentials and call Zappa deploy command. Zappa-django-utils will prepare database and static files. At the end of this section we’ll have a Django admin panel hosted on AWS Lambda.
Export environmental variables
You can export environmental variables on your project directory using direnv. Required variables:
(zappainator) $ zappa deploy dev (zappainator) $ zappa manage dev "collectstatic --noinput" (zappainator) $ zappa manage dev create_pg_db (zappainator) $ zappa manage dev migrate (zappainator) $ zappa manage dev create_admin_user admin email@example.com admin
You can check out if everything is ok by going to url returned by Zappa deploy command and logging into admin.
In this guide we managed to create a serverless Django app with access to RDS and S3 bucket. AWS resources were created using cloudformation. Application was deployed to AWS Lambda and API Gateway using Zappa.