如何把djangoapp發佈到awslambda

NO IMAGE

在這篇文章, 我將會指引怎麼用 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 版本, 所以用 pyenvpyenv-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

如何把djangoapp發佈到awslambda

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'
==============

上傳成功

如何把djangoapp發佈到awslambda

請求首頁

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

如何把djangoapp發佈到awslambda

並沒有那麼高興, 對吧, 這只是個 demo

用到的組件

回到 lambda 上可以看到, zappa 自動生成以下的組件

IAM

如何把djangoapp發佈到awslambda

API Gateway

如何把djangoapp發佈到awslambda

CloudWatch Event

如何把djangoapp發佈到awslambda

lambda 函數

如何把djangoapp發佈到awslambda

todo

1, 地址的末尾 /dev/ 要去掉的
2, 域名要換成自己的

去掉 /dev/

參考這裡, https://edgarroman.github.io/zappa-django-guide/walk_core/#why-is-the-url-path-appended-with-dev

如何把djangoapp發佈到awslambda

使用自己的域名

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

如何把djangoapp發佈到awslambda

相關文章

SpringBoot2.X(四):SpringBoot自定義WebMVC配置

SpringBoot2.X(三):使用SpringMVC+MyBatis+Thymeleaf開發web應用

SpringBoot2.X(二):集成MyBatis數據層開發

SpringBoot2.X(一):入門篇