在這篇文章, 我將會指引怎麼用 zappa 把一個 django app 發佈到 AWS Lamdbahtml
django 是一個很是流行的 Python web 框架. 只需用不多的代碼, 就可以高效地構建一個很是優秀的網站python
AWS Lambda 是一項計算服務,可以使您無需預配置或管理服務器便可運行代碼git
若是不瞭解什麼是 AWS IAM, 建議先讀 IAM 的介紹github
zappa 會幫助你建立 lambda 程序相關的角色, 在這個階段, 爲了避免要那麼操心, 先給你本地的 IAM 設置成 "Administrators" 組. 生產環境不要這麼作web
把 IAM 的密鑰放置在 ~/.aws 配置文件django
###~/.aws/credentials
[default]
aws_access_key_id=[...]
aws_secret_access_key=[...]
複製代碼
mkdir mysite
cd mysite
touch requirement.txt
複製代碼
把 django 和 zappa 寫入 requirement.txt, 以下json
cat requirement.txt
django
zappa
複製代碼
建立虛擬環境, 由於我本地有多個 python 版本, 因此用 pyenv 和 pyenv-virtualenv 管理個人虛擬環境api
pyenv virtualenv mysite-env
pyenv activate mysite-env
pip install -r requirement.txt
複製代碼
用如下經常使用的方法也是能夠的bash
virtualenv --no-site-packages venv
source venv/bin/activate
pip install -r requirement.txt
複製代碼
初始化 django服務器
django-admin startproject mysite .
複製代碼
運行 django server, 看看程序的狀態
python manage.py runserver
複製代碼
Zappa 是一個構建和發佈 python 程序到 AWS Lambda 的工具, 能用於 WSGI web apps, 例如 django, Flask.
zappa init
複製代碼
像如下輸出, 全部選項能夠直接按回車選默認
███████╗ █████╗ ██████╗ ██████╗ █████╗
╚══███╔╝██╔══██╗██╔══██╗██╔══██╗██╔══██╗
███╔╝ ███████║██████╔╝██████╔╝███████║
███╔╝ ██╔══██║██╔═══╝ ██╔═══╝ ██╔══██║
███████╗██║ ██║██║ ██║ ██║ ██║
╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝ ╚═╝
Welcome to Zappa!
Zappa is a system for 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]:
複製代碼
zappa deploy dev
複製代碼
zappa update dev
複製代碼
若是出現如下錯誤, 檢查一下 pip 版本, 若是是10.x.x, 要降級回 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' ============== 複製代碼
https://gc3sszkyo3.execute-api.us-east-1.amazonaws.com/dev
並無那麼高興, 對吧, 這只是個 demo回到 lambda 上能夠看到, zappa 自動生成如下的組件
1, 地址的末尾 /dev/ 要去掉的 2, 域名要換成本身的
參考這裏, https://edgarroman.github.io/zappa-django-guide/walk_core/#why-is-the-url-path-appended-with-dev
https://edgarroman.github.io/zappa-django-guide/walk_domain/