在这篇文章, 我将会指引怎么用 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
回到 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/