Django and AWS: Configure statics in Elastic Beanstalk

Requirements

  • Having a Django 2.2 project doployed into a AEB application. You can read more about this here.
  • Having the EB-CLI installed in your local environment. Instructions here.

Configure the static folder in the django settings

  • Define the static url in the project settings.py
  • Very important, define the django.contrib.staticfiles in you INSTALLED_APPS list.
INSTALLED_APPS = [
...
...
'django.contrib.staticfiles',...
...
]STATIC_URL = ‘/static/’
  • Create the file `.ebextensions/django.config` to configure the eb-extension that will execute the scripts to migrate the model changes and collect the static files into the EC2 instance in the deployment process.

packages:
yum:
git: []
postgresql95-devel: []
libjpeg-turbo-devel: []
container_commands:
01_migrate:
command: “source /opt/python/run/venv/bin/activate && python manage.py migrate — noinput”
leader_only: true
02_collectstatic:
command: “source /opt/python/run/venv/bin/activate && python manage.py collectstatic — noinput”
leader_only: true
  • Deploy your application with the EB-CLI by executing the following instruction from the root folder
eb deploy

Configure the static files in Elastic Beanstalk

...├── manage.py
├── runtime.txt
├── appname
│ ├── settings.py
│ ├── templates
│ ├── urls.py
│ ├── views.py
│ └── wsgi.py
├── staticfiles
...
...├── manage.py
...
├── appname
│ ├── settings
│ │ ├─--components
│ │ ├──- static.py
│ │ ├──- common.py
...
...
│ │ └── __init__.py
│ ├── staticfiles
│ └── __init__.py
...
│ ├── urls.py
│ ├── views.py
│ └── wsgi.py

...

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Juan Miguel Rúa Gutiérrez

Juan Miguel Rúa Gutiérrez

#Tech #SoftwareArchitect #DevOps #AWS #Terraform #SoftwareDevelopment #Python #Nodejs #Entrepreneurship #juanmiguelrua.com