在这篇文章, 我将会指引怎么用 zappa 把一个 django app 发布到 AWS Lamdba
django
django 是一个非常流行的 Python web 框架. 只需用很少的代码, 就能够高效地构建一个非常优秀的网站
AWS Lambada
AWS Lambda 是一项计算服务,可使您无需预配置或管理服务器即可运行代码
AWS IAM
IAM 是什么
如果不了解什么是 AWS IAM, 建议先读 IAM 的介绍
配置本地 IAM
zappa 会帮助你创建 lambda 程序相关的角色, 在这个阶段, 为了不要那么操心, 先给你本地的 IAM 设置成 "Administrators" 组. 生产环境不要这么做
把 IAM 的密钥放置在 ~/.aws 配置文件
###~/.aws/credentials
[default]
aws_access_key_id=[...]
aws_secret_access_key=[...]
构造 Django app
初始目录和 requirement.txt
mkdir mysite
cd mysite
touch requirement.txt
把 django 和 zappa 写入 requirement.txt, 如下
cat requirement.txt
django
zappa
创建虚拟环境, 因为我本地有多个 python 版本, 所以用 pyenv 和 pyenv-virtualenv 管理我的虚拟环境
pyenv virtualenv mysite-env
pyenv activate mysite-env
pip install -r requirement.txt
用以下常用的方法也是可以的
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
Zappa 是一个构建和发布 python 程序到 AWS Lambda 的工具, 能用于 WSGI web apps, 例如 django, Flask.
初始化 zappa 环境
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 自动生成以下的组件
IAM
API Gateway
CloudWatch Event
lambda 函数
todo
1, 地址的末尾 /dev/ 要去掉的
2, 域名要换成自己的
去掉 /dev/
参考这里, 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/