In this article, I’ll show you how to publish a Django app to AWS Lamdba using Zappa

django

Django is a very popular Python Web framework. You can efficiently build a great website with very little code

AWS Lambada

AWS Lambda is a computing service that lets you run code without pre-configuring or managing a server

AWS IAM

What IAM

If you do not know what AWS IAM is, it is recommended to read the introduction of IAM first

Configuring local IAM

Zappa will help you create the roles associated with the lambda program. At this stage, to ease your worries, set your local IAM to the “Administrators” group first. Don’t do this in production

Place the IAM key in the ~/. Aws configuration file

###~/.aws/credentials [default] aws_access_key_id=[...]  aws_secret_access_key=[...]Copy the code

To construct a Django app

The initial directory and requirement. TXT

mkdir mysite
cd mysite
touch requirement.txt
Copy the code

Add Django and Zappa to the requirement. TXT as follows

cat requirement.txt
django
zappa
Copy the code

Create a virtual environment. Since I have multiple versions of Python native, I use PyEnv and Pyenv-VirtualEnv to manage my virtual environment

pyenv virtualenv mysite-env
pyenv activate mysite-env
pip install -r requirement.txt
Copy the code

It is also possible to use the following common methods

virtualenv --no-site-packages venv
source venv/bin/activate
pip install -r requirement.txt
Copy the code

Initialize the django

django-admin startproject mysite .
Copy the code

Run Django Server to see the status of your application

python manage.py runserver
Copy the code

Zappa

Zappa is a tool for building and publishing Python programs into AWS Lambda for WSGI Web apps such as Django and Flask.

Initialize the Zappa environment

zappa init
Copy the code

As shown in the following output, all options can be selected by default directly by pressing Enter

█ █ █ █ █ █ █ ╗ █ █ █ █ █ ╗ █ █ █ █ █ █ ╗ █ █ █ █ █ █ ╗ █ █ █ █ █ ╗ ╚ ═ ═ █ █ █ ╔ ╝ █ █ ╔ ═ ═ █ █ ╗ █ █ ╔ ═ ═ █ █ ╗ █ █ ╔ ═ ═ █ █ ╗ █ █ ╔ ═ ═ █ █ ╗ █ █ █ ╔ ╝ █ █ █ █ █ █ █ ║ █ █ █ █ █ █ ╔ ╝ █ █ █ █ █ █ ╔ ╝ █ █ █ █ █ █ █ ║ █ █ █ ╔ ╝ █ █ ╔ ═ ═ █ █ ║ █ █ ╔ ═ ═ ═ ╝ █ █ ╔ ═ ═ ═ ╝ █ █ ╔ ═ ═ █ █ ║ █ █ █ █ █ █ █ ╗ █ █ ║ █ █ ║ █ █ ║ █ █ ║ █ █ ║ █ █ ║ ╚ ═ ═ ═ ═ ═ ═ ╝ ╚ ═ ╝ ╚ ═ ╝ ╚ ═ ╝ ╚ ═ ╝ ╚ ═ ╝ ╚ ═ ╝ Welcome to Zappa! Zappa is a systemfor running server-less Python web applications on AWS Lambda and AWS API Gateway.
This `init` command will help you create and configure your new Zappa deployment.
Let's get started! Your Zappa configuration can support multiple production stages, like 'dev', 'staging', and 'production'.
What do you want to call this environment (default 'dev'): AWS Lambda and API Gateway are only available in certain regions. Let's check to make sure you have a profile set up in one that will work.
Okay, using profile default!

Your Zappa deployments will need to be uploaded to a private S3 bucket.
If you don't have a bucket yet, we'll create one for you too.
What do you want call your bucket? (default 'zappa-gc39ra9lq'):

It looks like this is a Django application!
What is the module path to your projects's Django settings?
We discovered: mysite.settings
Where are your project's settings? (default 'mysite.settings'):

You can optionally deploy to all available regions in order to provide fast global service.
If you are using Zappa for the first time, you probably don't want to do this!
Would you like to deploy this application globally? (default 'n') [y/n/(p)rimary]: Okay, here's your zappa_settings.json:

{
    "dev": {
        "aws_region": "us-east-1"."django_settings": "mysite.settings"."profile_name": "default"."project_name": "mysite"."runtime": "Python2.7."."s3_bucket": "zappa-gc39ra9lq"
    }
}

Does this look okay? (default 'y') [y/n]:
Copy the code

The command is issued for the first time

zappa deploy dev
Copy the code

Issue command after code modification

zappa update dev
Copy the code

If any of the following errors occur, check the PIP version and, if 10.x.x, downgrade back to 9.0.3

h no! An error occurred! : (= = = = = = = = = = = = = = lib/python2.7 / site - packages/zappa/core. Py", line 751, in get_installed_packages pip.get_installed_distributions() AttributeError: 'module' object has no attribute 'get_installed_distributions' ==============Copy the code

Uploaded successfully

The request page

https://gc3sszkyo3.execute-api.us-east-1.amazonaws.com/dev

Components used

Going back to lambda, you can see that Zappa automatically generates the following components

IAM

API Gateway

CloudWatch Event

Lambda functions

todo

1, the end of the address /dev/2 to be removed, the domain name to be replaced with its own

Get rid of/dev /

Reference here, https://edgarroman.github.io/zappa-django-guide/walk_core/#why-is-the-url-path-appended-with-dev

Use your own domain name

https://edgarroman.github.io/zappa-django-guide/walk_domain/