Skip to content

Deployment via Amazon Elastic Beanstalk

Stanislav Valasek edited this page Nov 21, 2020 · 10 revisions

Input sources

How to deploy Django to AWS using Elastic Beanstalk

Install AWS CLI

pip install awsebcli

ASW Deployment

mkdir .ebextensions

Create .ebextensions\django.config with content

container_commands:
    01_sh_executable:
        command: find .platform/hooks/ -type f -iname "*.sh" -exec chmod +x {} \;
option_settings:
    aws:elasticbeanstalk:application:environment:
        DJANGO_SETTINGS_MODULE: ebdjango.settings
    aws:elasticbeanstalk:environment:proxy:staticfiles:
        /static: static
        /static_files: static_files
    aws:elasticbeanstalk:container:python:
        WSGIPath: ebdjango.wsgi:application

Run the commands

eb init -p python-3.7 kicoma
eb create kicoma-env
eb status

copy CNAME value CNAME: kicoma-env.eba-j2q4pbqw.eu-central-1.elasticbeanstalk.com

to ALLOWED_HOSTS in the settings file

Create .platform/hooks/predeploy/01_migrations.sh with a content

container_commands:
    01_sh_executable:
        command: find .platform/hooks/ -type f -iname "*.sh" -exec chmod +x {} \;
option_settings:
    aws:elasticbeanstalk:application:environment:
        DJANGO_SETTINGS_MODULE: ebdjango.settings
    aws:elasticbeanstalk:environment:proxy:staticfiles:
        /static: static
        /static_files: static_files
    aws:elasticbeanstalk:container:python:
        WSGIPath: ebdjango.wsgi:application

Add a DB

Update settings file

import os

if 'RDS_HOSTNAME' in os.environ:
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': os.environ['RDS_DB_NAME'],
            'USER': os.environ['RDS_USERNAME'],
            'PASSWORD': os.environ['RDS_PASSWORD'],
            'HOST': os.environ['RDS_HOSTNAME'],
            'PORT': os.environ['RDS_PORT'],
        }
    }

Deploy

eb deploy

Clone this wiki locally