Deploying on AWS Elastic Bean Stalk

Django + AWS Elastic Beanstalk = Deployment Magic! โจ Your Fun Guide
Hey Devs! ๐ So you've built an awesome Django app. High five! ๐ But now comes the slightly intimidating part: getting it live on the web for everyone to see. Enter AWS Elastic Beanstalk (EB), a service that promises to make deployment smoother. Sounds good, right?
But let's be real, sometimes the setup feels like wrestling a digital octopus. ๐ Fear not! This guide will walk you through setting up the essential tools (specifically the EB Command Line Interface on Windows) and structuring your Django project so Elastic Beanstalk knows exactly what to do.
Ready to turn those deployment nightmares into sweet dreams? Let's dive in!
Step 1: Gotta Catch 'Em All... Python First! ๐
Elastic Beanstalk's command-line tool (the EB CLI) is built with Python, so we need Python installed on our system first.
- Head to the Source: Go to the official Python website:
https://www.python.org/downloads/windows/ - Grab the Latest: Download the latest stable version for Windows (e.g., Python 3.12.x).
- Run the Installer: Double-click that downloaded file!
- ๐จ SUPER IMPORTANT STEP! ๐จ: During installation, make sure you CHECK THE BOX that says "Add Python to PATH". Seriously, don't skip this. It makes life so much easier.
- Click "Install Now" and let the magic happen.
Verification Time! โ
Let's make sure Python is ready to roll. Open your Command Prompt (Hit Win + R, type cmd, hit Enter).
Now, type these commands one by one, hitting Enter after each:
python --version
pip --version
If you see version numbers pop up for both, give yourself a pat on the back! Python is installed and ready.
Step 2: Installing the Mighty EB CLI โ๏ธ๐ง
With Python and pip (Python's package installer) ready, installing the EB CLI is a breeze.
In that same Command Prompt, run this command:
pip install awsebcli --upgrade --user
This tells pip to install the awsebcli package, upgrade it if it's already there, and install it in your user directory.
Where Did It Go? ๐ค
This command usually places the eb.exe file in a path similar to this (replace HP with your actual Windows username and Python312 with your Python version):
C:\Users\HP\AppData\Roaming\Python\Python312\Scripts
You can double-check by running:
dir %USERPROFILE%\AppData\Roaming\Python\Python312\Scripts
You should see eb.exe listed in there.
Step 3: Tell Windows Where to Find EB (The PATH Update!) ๐บ๏ธ
Okay, EB CLI is installed, but your computer is like, "What's 'eb'?" We need to tell Windows exactly where that eb.exe file lives by adding its folder to the system's PATH environment variable.
- Search for "Environment Variables" in your Windows search bar and select "Edit the system environment variables".
- Click the "Environment Variables..." button.
- Under "System variables" (or "User variables for..." if you prefer), find the
Pathvariable, select it, and click "Edit...". - Click "New".
- Paste the exact path where
eb.exewas installed. Using our example: (Again, adjustMakefileC:\Users\HP\AppData\Roaming\Python\Python312\ScriptsHPandPython312to match your system!) - Click "OK" on all the open windows to save the changes.
Step 4: The Grand Finale Test! ๐
Moment of truth! Close your current Command Prompt and open a brand new one. This is crucial so it picks up the PATH changes.
Now, type:
eb --version
If you see something like EB CLI 3.x.x (Python 3.x.x), YOU DID IT! ๐ The EB CLI is installed and ready for action!
Step 5: Prepping Your Django Project for Launch ๐ (File Structure is Key!)
Now that the tools are set up, we need to organize our Django project so Elastic Beanstalk understands it. Think of it like packing your suitcase correctly for a trip!
Here's the essential structure you'll need in the root of your Django project (where your manage.py file lives):
your-django-project/
โ
โโโ .ebextensions/ # <--- AWS EB specific configs go here!
โ โโโ 01_django.config
โ โโโ django.config
โ โโโ environment.config # (Optional but recommended for secrets!)
โ
โโโ .elasticbeanstalk/ # <--- EB CLI creates this automatically (leave it alone!)
โ
โโโ your_actual_django_app/ # <--- Your app code, settings.py, urls.py etc.
โ
โโโ your_project_name/ # <--- Your project's wsgi.py lives here
โ โโโ wsgi.py
โ
โโโ .ebignore # <--- Tells EB what NOT to upload
โ
โโโ requirements.txt # <--- List of Python packages needed
โ
โโโ manage.py # <--- Django's trusty manager
โ
โโโ Procfile # <--- Tells EB how to run your web server
Let's break down those special files:
-
.ebextensions/01_django.config: This file tells EB where to find your Django project's settings and WSGI entry point.
(Replace your_project_name with the actual name of the folder containing your wsgi.py)
YAML# .ebextensions/01_django.config option_settings: aws:elasticbeanstalk:container:python: WSGIPath: your_project_name/wsgi.py # <-- Point this to YOUR wsgi.py file! aws:elasticbeanstalk:application:environment: DJANGO_SETTINGS_MODULE: your_project_name.settings # <-- Point this to YOUR settings! -
.ebextensions/django.config: This helps configure how EB serves your static files.YAML# .ebextensions/django.config option_settings: aws:elasticbeanstalk:environment:proxy:staticfiles: /static: staticfiles # Tells EB to map /static URL to the 'staticfiles' directory collected by DjangoNote: Ensure
STATIC_ROOT = BASE_DIR / 'staticfiles'(or similar) is set in yoursettings.pyand you've runpython manage.py collectstaticlocally to test. -
.ebextensions/environment.config(Optional but HIGHLY Recommended): This is where you can securely define environment variables like yourSECRET_KEY, database passwords, API keys, etc., without putting them directly in your code. EB will inject these into your environment.YAML# .ebextensions/environment.config option_settings: aws:elasticbeanstalk:application:environment: SECRET_KEY: "YOUR_SUPER_SECRET_KEY_HERE" # Replace with a real generated key! DB_PASSWORD: "YOUR_DATABASE_PASSWORD" # Add other sensitive variables hereNEVER commit actual secret values to Git! Use placeholder values in the config you commit, and set the real values directly in the Elastic Beanstalk environment configuration via the AWS Console for maximum security.
-
Procfile: This simple file tells Elastic Beanstalk what command to run to start your web server. We use gunicorn, a popular Python WSGI server.
(Replace your_project_name with the folder containing wsgi.py)
Plaintext# Procfile web: gunicorn your_project_name.wsgi --bind 0.0.0.0:8000 -
requirements.txt: You know this one! It lists all the Python packages your project needs. Make sure it includes AT LEAST:Plaintext# requirements.txt Django>=3.2 # Use your project's Django version gunicorn>=20.0 # Essential for the Procfile command # ... include ALL other dependencies (psycopg2-binary, requests, etc.)Generate it easily with:
pip freeze > requirements.txt(but clean it up!). -
.ebignore: This works like.gitignore. It tells the EB CLI which files and folders not to bundle up and send to AWS. Saves time and space!Plaintext# .ebignore venv/ .env *.pyc __pycache__/ .git/ .gitignore .DS_Store local_settings.py # Anything specific to your local machine *.sqlite3 # Don't upload your local dev database!
Crucial Final Checks:
- File Placement: Double-check all these files (
.ebextensionsfolder,Procfile,requirements.txt,.ebignore) are in the ROOT directory of your project, alongsidemanage.py. .configExtension: All files inside.ebextensionsmust end with.config.- Static Files: Make sure
STATIC_URLandSTATIC_ROOTare correctly set in yoursettings.py. Runpython manage.py collectstaticbefore deploying to gather your static files into theSTATIC_ROOTdirectory (which we told EB about indjango.config). - Allowed Hosts: In your
settings.py, updateALLOWED_HOSTSto include Elastic Beanstalk domains:PythonALLOWED_HOSTS = ['YOUR_DOMAIN.com', '.elasticbeanstalk.com'] - Database: Your
settings.pyshould be configured to read database credentials from environment variables (which you'll set via.ebextensions/environment.configor the EB console), so it works both locally and in production.
Step 6: Houston, We Are Ready for Liftoff! ๐ (Deploying!)
Okay, deep breaths! Your tools are ready, your project is structured correctly. Now for the magic commands.
Navigate to your project's root directory in your Command Prompt.
-
Create the Application Container: This tells EB about your app in general. You only do this once per application.
Basheb create app your-app-name(Replace
your-app-namewith a unique name for your application). -
Create and Deploy the Environment: This is the command that actually builds the AWS resources (servers, databases, load balancers, etc.) and deploys your current code to them.
Basheb create your-environment-name --platform "Python 3.12" --instance_type t2.micro(Replace
your-environment-namewith a unique name for this specific deployment environment, likemy-django-prod. Choose the Python platform version matching your project.t2.microis a small, often free-tier eligible instance type to start with).
The eb create command will zip up your code (respecting .ebignore), upload it to EB, and start provisioning the resources. This can take a few minutes, so grab a coffee! โ You'll see updates directly in your terminal.
Once it's done, EB will give you a URL where you can access your live Django application! ๐
And there you have it! You've conquered the EB CLI setup on Windows and learned how to structure your Django project for successful deployment. It might seem like a lot at first, but breaking it down step-by-step makes it totally manageable.
Happy Deploying! Let me know how it goes!