ãã®è¨äºã¯ Django Advent Calendar 2017 - Qiita ã®17æ¥ç®ã®è¨äºã§ãã
ããæ°å¹´ãµã¼ãã¼ã¬ã¹ã¢ã¼ããã¯ãã£ãçãä¸ãã£ã¦ãããã®ã®ãDjangoã¨çµã¿åãããäºä¾ãã°ã°ã£ã¦ããã¾ããªãã®ã§æ¸ãã¦ã¿ã¾ãããã°ã°ã£ã¦ãåºã¦ããªãã¨ãããã¨ã¯éè¦ãç¡ãã¨ãããã¨ããããã¾ããããããã¿ãªããFlaskã¨ãã£ã軽éãªwebãã¬ã¼ã ã¯ã¼ã¯ã¨Dynamoã£ã¦çµã¿åããã§ä½¿ã£ã¦ããã§ãããã
ç§ã¯Lambda+Djangoã¨ããçµã¿åããã§æ¬çªæå ¥ããçµé¨ã¯ããã¾ãããæ¬è¨äºã¯ãéç¨ã«ä½¿ããããï¼ã¨ãã観ç¹ã§æ¤è¨¼ãã¾ããã
æ¬è¨äºã®ãµã³ãã«ã³ã¼ãã¯ä»¥ä¸ã«ç½®ãã¦ããã¾ããå種ãã¼ã¸ã§ã³ã¯githubå ã®requirements.txtãåç §ãã¦ãã ããã
GitHub - koty/dj-lambda-sample: A sample of Django app on AWS Lambda using severless framework.
Djangoããã¸ã§ã¯ãã®ä½æ
é©å½ãªååã§Djangoããã¸ã§ã¯ããä½ãã¾ããããã¸ã§ã¯ãæ§æã¯åè¿°ã®githubãªãã¸ããªãã確èªãã ãããæ¨æºçãªãã®ã ã¨æãã¾ãã
Serverless Frameworkã®å°å ¥
Lambdaãéç¨ããã«ã¯ããããªãã¨ãã£ã¦ããã¾ãããå種ãªã½ã¼ã¹ãzipã«ã¾ã¨ãã¦Lambdaã«deployãã¦ããã¾ããã¾ãAPI Gatewayãªã©ã®å¨è¾ºãªã½ã¼ã¹ã®è¨å®ãã§ãã¾ããpythonã¢ããªãä½ãã®ã«npmã¢ã¸ã¥ã¼ã«ãå°å ¥ããã®ã¯ã©ããªãã ã¨ãæãã¾ããã©ãã
serverless ã³ãã³ãã使ãããã« -g ã¤ãã§installãã¾ãã
npm install -g serverless
VPCãIAM roleçã®è¨å®ãå¿ è¦ã§ãããserverless frameworkèªä½ã¯æ å ±ãå¤ãããã¾ãã®ã§ãããã§ã¯ãã以ä¸è§¦ãã¾ããã
serverless-wsgi ã®å°å ¥
Djangoã使ãã«ã¯wsgiã¨ããè¦æ ¼ã«æ²¿ãå¿ è¦ãããã¾ããæ®æ®µEC2çã§åããå ´åã¯uwsgiãgunicornã使ãã°wsgiã«æ²¿ããã¨ãã§ãã¾ãããLambdaã®å ´åã¯ä»¥ä¸ã使ãã¾ãã
npm install serverless-wsgi --save-dev
serverless.ymlã«ä»¥ä¸ã®ããã«è¿½è¨ãã¾ãã
functions: api: events: # ãã®è¨è¿°ã«ãããã«ã¼ãã£ã³ã°ãwsgiå´ï¼Djangoï¼ã«ç§»è²ã§ããã {proxy+} ã®æå³ã¯ä¸æããdocumentã«ããæ¸ãã¦ããã - http: ANY / - http: ANY {proxy+} plugins: - serverless-wsgi custom: wsgi: # appã«ã¯wsgi.py ã¸ã®ãã¹ãè¨è¿°ã serverless-python-requirementsã«ããã±ã¼ã¸ã³ã°ãä»»ãããã packRequirements: false ãè¨è¿° app: dj-lambda-sample.wsgi.application packRequirements: false
serverless-python-requirements ã®å°å ¥
EC2äºæã®dockerã³ã³ããä¸ã§pip installãdeployããã±ã¼ã¸ãä½ã£ã¦ããã¾ããlxmlçã®pip installæã«ã³ã³ãã¤ã«ãããããã±ã¼ã¸ã使ã£ã¦ãã¦ãEC2ã§åé¡ãªãåãããããã±ã¼ã¸ã§ãã¾ãã
npm install serverless-python-requirements --save-dev
serverless.ymlã«ã¯ä»¥ä¸ã®ããã«è¨å®ãã¾ãã
plugins: - serverless-python-requirements custom: pythonRequirements: dockerizePip: true
Djangoã®è¨å®
DB ã®è¨å®
使ãRDBMSã¯PostgreSQLã§ããMySQLã¯ãã»ã©ã®çç±ããªãéãã¯ä½¿ããªãæ¹ãè¯ãããã§ãã
äºåã«RDSãç«ã¦ã¦ããã¾ãï¼EC2ä¸ã«ç«ã¦ã¦ãå½ç¶OKï¼ãæ¤è¨¼ãæ¸ãã ãè½ã¨ãã¦ããã®ãå¿ããã«ãã
serverless.env.ymlã以ä¸ã®ããã«ä½ãã¾ãã
SECURITY_GROUP_ID: 'sg-xxxxxxx' SUBNET_ID1: 'subnet-xxxxxxx' SUBNET_ID2: 'subnet-xxxxxxx' DATABASE_NAME: 'xxxxx' DATABASE_USER: 'xxxxx' DATABASE_PASSWORD: 'xxxxxx' DATABASE_HOST: 'xxxxxx.xxxxxxxx.ap-northeast-1.rds.amazonaws.com' DATABASE_PORT: 5432
ç°å¢å¤æ°ãä½ãããã«ãserverless.ymlã«ä»¥ä¸ã®ããã«è¨å®ãã¾ããå¤é¨ãã¡ã¤ã«ããèªã¿è¾¼ãããã§ãã
provider: environment: DATABASE_NAME: ${file(./serverless.env.yml):DATABASE_NAME} DATABASE_USER: ${file(./serverless.env.yml):DATABASE_USER} DATABASE_PASSWORD: ${file(./serverless.env.yml):DATABASE_PASSWORD} DATABASE_HOST: ${file(./serverless.env.yml):DATABASE_HOST} DATABASE_PORT: ${file(./serverless.env.yml):DATABASE_PORT}
Djangoã®settings.pyã«ä»¥ä¸ã®ããã«è¨è¿°ãã¾ãã
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': os.getenv('DATABASE_NAME'), 'USER': os.getenv('DATABASE_USER'), 'PASSWORD': os.getenv('DATABASE_PASSWORD'), 'HOST': os.getenv('DATABASE_HOST'), 'PORT': os.getenv('DATABASE_PORT'), }, }
æå¾ã«EC2çãã ./manage.py migrate
ãã¾ããEC2ãç¨æããã®ãé¢åã ã£ãã®ã§ã
class MigrateView(APIView): def get(self, request): from django.core import management management.call_command('migrate') return Response({})
ã¨ããviewã§migrateãã¾ãããããç¾å®çã«ã¯VPCå ã«ç«ã¦ãEC2ããmigrateããã£ã¦ããã§ãããã
ãã®ã
ãã®ã¸ãã§åå°½ãã¦ãã¾ãããLambdaã§ã¯éçãã¡ã¤ã«ã¯ãã¹ãã§ããªãã®ã§ãS3ã使ãã¾ããdjango-storageã使ã£ã¦ãS3ã«åãã£ã¦collectstaticãã¾ãã
ã¾ã¨ã
ç°¡åãªAPIã§ããã°ãã®æ§æã§éç¨ã§ãããã§ããLambdaã®ã¹ã±ã¼ã©ããªãã£ã¯é åã§ãããããå¦çãå ¥ã£ã¦ããã¨EC2ãã»ãããªãããã§ãããããç¹°ãè¿ãã¾ããã使ãRDBMSã¯MySQLã¯ãããæ¹ãè¯ãã§ããè·å ´ã§ç§ã®æãåã®äººããã©ããããã¦ã¾ããã
æ®èª²é¡
- ãããã¤æã«æ¥ç¶ãåãããï¼
- Lambdaã®ããã±ã¼ã¸å®¹éå¶éã¯ã©ãããï¼site-packagesã®å®¹éãå¢ããã¨ã©ããªãï¼
ãã£ã¨coolãªæ¹æ³ããããï¼ã¨ããæ¹ã¯ãã²æãã¦ãã ãããã
Two Scoops of Django 1.11: Best Practices for the Django Web Framework (English Edition)
- ä½è : Daniel Roy Greenfeld,Audrey Roy Greenfeld
- åºç社/ã¡ã¼ã«ã¼: Two Scoops Press
- çºå£²æ¥: 2017/10/11
- ã¡ãã£ã¢: Kindleç
- ãã®ååãå«ãããã°ãè¦ã
Vue.js 2 Cookbook: Build modern, interactive web applications with Vue.js
- ä½è : Andrea Passaglia
- åºç社/ã¡ã¼ã«ã¼: Packt Publishing
- çºå£²æ¥: 2017/04/28
- ã¡ãã£ã¢: Kindleç
- ãã®ååãå«ãããã°ãè¦ã