If you're building a Django application and want to get it online quickly without dealing with tons of server setup, AWS Elastic Beanstalk might be exactly what you need. It takes care of the heavy lifting like provisioning servers, managing environments, scaling, and even monitoring — all so you can focus on writing code. In this article, we’ll walk you through how to deploy a Django app on AWS Elastic Beanstalk step by step. Whether you're a solo developer or building for a small team, this guide will help you confidently take your app live.
What Is AWS Elastic Beanstalk and Why Use It for Django?
Think of AWS Elastic Beanstalk as a “smart wrapper” around AWS infrastructure. It automates tasks like launching EC2 instances, configuring load balancers, setting up databases, and deploying code. For Django developers, this means you don’t need to manually configure every server or service — Elastic Beanstalk can handle it for you.
Here's why it’s great for Django:
- Quick deployment: Upload your code, and AWS handles the rest.
- Built-in scalability: It can automatically scale your app up or down based on traffic.
- Free tier eligible: You can start deploying without spending much (or anything at all initially).
- Integrated with other AWS services: Like RDS for databases, S3 for file storage, and CloudWatch for monitoring.
So if you want a production-ready environment with minimal hassle, Elastic Beanstalk is a solid choice.
Step 1: Prepare Your Django Application for Deployment
Before sending your app to the cloud, you’ll need to make a few changes.
1.1 Set Up a Virtual Environment
If you haven’t already, create a virtual environment to manage dependencies.
python -m venv venv
source venv/bin/activate
1.2 Install Required Packages
Install gunicorn
(a WSGI HTTP server) and psycopg2
(if you’re using PostgreSQL).
pip install gunicorn psycopg2-binary
Then freeze your dependencies:
pip freeze > requirements.txt
1.3 Configure settings.py
for Production
A few changes are required in settings.py
:
-
Allowed hosts:
ALLOWED_HOSTS = ['.elasticbeanstalk.com']
-
Static files: Add these at the bottom of
settings.py
.STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, 'static')
Run:
python manage.py collectstatic
This command gathers all your static files in one place so Elastic Beanstalk can serve them properly.
Step 2: Create the Project Structure for Beanstalk
Elastic Beanstalk expects a specific structure to deploy your app.
2.1 Create a File Called application.py
Elastic Beanstalk looks for a file named application
. Update your wsgi.py
:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "your_project.settings")
application = get_wsgi_application()
Make sure it’s in your root directory or symlinked properly.
2.2 Add a .ebextensions
Directory
Inside your project root, create a folder called .ebextensions
. This will contain configuration files for Elastic Beanstalk.
Here’s a sample django.config
file:
option_settings:
aws:elasticbeanstalk:container:python:
WSGIPath: your_project/wsgi.py
aws:elasticbeanstalk:application:environment:
DJANGO_SETTINGS_MODULE: "your_project.settings"
PYTHONPATH: "/var/app/current:$PYTHONPATH"
You can also use this directory to add database environment variables, logging configurations, and more.
Step 3: Initialize and Deploy Using the EB CLI
AWS provides a command-line tool to work with Elastic Beanstalk.
3.1 Install the EB CLI
pip install awsebcli
Then configure it:
eb init
This will ask for:
- Region (like
us-east-1
) - Application name
- Platform (choose Python 3.x)
- Code commit options (optional)
If you're using Git, make sure your code is committed before deploying.
3.2 Create and Launch the Environment
Run:
eb create django-env
This command sets up a new environment with EC2, security groups, and other required AWS resources.
3.3 Deploy the App
Once your environment is ready:
eb deploy
This command zips your code, uploads it, and triggers deployment.
To check the live app:
eb open
Step 4: Connect to a Database (RDS)
Most real apps need a database. While Elastic Beanstalk can use SQLite for testing, you’ll want something more powerful like PostgreSQL or MySQL for production.
4.1 Add RDS During Environment Creation
You can attach an RDS database while setting up your environment with:
eb create django-env --database.engine postgres --database.username admin --database.password password123
Or use the AWS Management Console to add an RDS instance manually.
4.2 Update settings.py
with Database Config
Use environment variables to configure the database:
import os
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': os.environ.get('RDS_DB_NAME'),
'USER': os.environ.get('RDS_USERNAME'),
'PASSWORD': os.environ.get('RDS_PASSWORD'),
'HOST': os.environ.get('RDS_HOSTNAME'),
'PORT': os.environ.get('RDS_PORT'),
}
}
Make sure you’ve run:
python manage.py migrate
Step 5: Debugging and Logs
Things don’t always go smoothly the first time. If your app throws errors, you can check logs:
eb logs
You can also SSH into the EC2 instance:
eb ssh
This is helpful if you want to inspect files, check settings, or troubleshoot manually.
Security Tips and Best Practices
Deploying your app is one thing. Keeping it secure and running well is another.
- Use environment variables: Never hardcode secrets in your code.
- Enable HTTPS: Use AWS Certificate Manager to add SSL to your app.
- Monitor with CloudWatch: Set alarms for CPU usage, memory, or error rates.
- Use IAM roles: Restrict what your app can access in AWS.
- Backup your database: RDS supports automatic backups — use them.
Conclusion: Ready to Go Live
Deploying Django on AWS Elastic Beanstalk might seem intimidating at first, but once you understand the workflow, it becomes a smooth and powerful way to launch web apps. With Elastic Beanstalk, you get speed, scalability, and integration with the entire AWS ecosystem — without managing servers yourself.
As you grow, you can start optimizing performance, adding background tasks, integrating S3 for media, or even setting up CI/CD pipelines. For startups, hobby projects, or even internal tools, this setup gives you a reliable foundation that can scale with your needs.
One final tip: don’t be afraid to experiment. Elastic Beanstalk environments can be torn down and spun up again with ease — giving you a safe playground to learn, test, and improve.
Post a Comment