Note_Tech

All technological notes.


Project maintained by simonangel-fong Hosted on GitHub Pages — Theme by mattgraham

Django - Deploy to Elastic Beanstalk

Back


Delpoy Django

Prerequisites


Configure local Django project for EB

  1. General requirements.txt
$ pip freeze > requirements.txt

  1. Create a directory named .ebextensions.
$ mkdir .ebextensions

  1. In the .ebextensions directory, add a configuration file named django.config with the following text:
option_settings:
  aws:elasticbeanstalk:container:python:
    WSGIPath: ebdjango.wsgi:application
  aws:elasticbeanstalk:environment:proxy:staticfiles:
    /static: static

~/proj_container/
|-- .ebextensions
|   `-- django.config
|-- proj_dir
|   |-- __init__.py
|   |-- settings.py
|   |-- urls.py
|   `-- wsgi.py
|-- db.sqlite3
|-- manage.py
`-- requirements.txt

Create an EB environment and Deploy

  1. Initialize EB CLI repository with the eb init command.
~/proj_container$ eb init -p python-3.7 <app_name>

  1. (Optional) Run eb init again to configure a default key pair so that can use SSH to connect to the EC2 instance running your application.
~/proj_container$ eb init
Do you want to set up SSH for your instances?
(y/n): y
Select a keypair.
1) my-keypair
2) [ Create new KeyPair ]

  1. Create an environment
~/proj_container$ eb create <env_name>

  1. Find the domain name of new environment by running eb status.
~/ebdjango$ eb status
Environment details for: <env_name>
  Application name: <app_name>
  ...
  CNAME: eb-django-app-dev.elasticbeanstalk.com
  ...

  1. Open the settings.py file in the ebdjango directory. Locate the ALLOWED_HOSTS setting, and then add your application’s domain name that you found in the previous step to the setting’s value. If you can’t find this setting in the file, add it to a new line.
ALLOWED_HOSTS = [
  'eb-django-app-dev.elasticbeanstalk.com'
]

  1. Deploy application by running eb deploy. When run eb deploy, the EB CLI bundles up the contents of your project directory and deploys it to your environment.
~/proj_container$ eb deploy

  1. When the environment update process completes, open website with eb open.
~/proj_container$ eb open

Clean up EB instance

~/project_container$ eb terminate env-name

TOP