Deploy Django to AWS (Part 1)
Following this tutorial.
If the database is not running run:
You should see a database named
cptmusicblog in the output.
control + D stops the postgres on Mac.
iotd/iotd/settings.py to your local database settings:
Run some django commands to set up the database and create an admin user. The credentials you choose will allow you to log in to the admin section of the site.
http://localhost:8000/admin/ and log in with credentials. If you go to the root (
http://localhost:8000/) you will see a django error. Now go add an image in the admin section under “Featured images”. Once you upload you’ll see your image on the homepage
Install this to your virtualenv:
Then sign in to AWS:
Okay then set up an elastic beanstalk environment:
I created a new ssh key for this named aws-eb. Once that command is done there is now a
.elasticbeanstalk directory in your project.
$ eb console and your browser will open up
This is us right now:
This is where we want to be:
This is going to happen first:
Don’t do this:
We want to have a development and production environment.
$ eb create
So that looks good. We want to host our static files in an S3 bucket.
Elastic beanstalk created an environment and tried to deploy our app…
In the console (
eb console) you can view the environment:
Now create a database in the AWS console:
- Click the “Configuration” link.
- Scroll all the way to the bottom of the page, and then under the “Data Tier” section, click the link “create a new RDS database”.
- On the RDS setup page change the “DB Engine” to “postgres”.
- Add a “Master Username” and “Master Password”.
- Save the changes
I saw this for a while:
.elasticbeanstalk goes in the root of the directory, so we have to move it, so from the
iotd directory (where it is and not supposed to be).
Move the file:
This pulls down a config file from AWS. After we edit the config the app will redeploy. We have to edit this part of the file:
Change the last line so that it reads:
$ eb deploy after you save and exit the changes.
You can now go to the website (http://cptmusicblog-dev.elasticbeanstalk.com/ in my case) and see a nice friendly django error:
This is good! We got the same thing when running locally before we uploaded the doge image. Go to
<your_eb_url>/admin (so in my case
http://cptmusicblog-dev.elasticbeanstalk.com/admin) and you will see the django admin login panel. Hooray!!
.elasticbeanstalk/02_python.config we specify settings for AWS:
Notice the second step creates a superuser. The directory we cloned has a script in
So when you go to
/admin log in with
username: admin, password: admin. Then you can upload an image and you will see doge again!
So we are almost good. The problem now is that the images are hosted on the EC2 instance itself in the static files directory. We don’t want that. Every time we run
eb deploy we will create a brand new instance with a clean static files directory. We don’t want to wipe our data every single time we deploy.
Instead we will to host our data in an S3 bucket. The instances will talk to the bucket and render the data. That way we can deploy all day long and not erase the data we’ve uploded.
Try it! Run
eb deploy and the image will disappear:
The text will be stored in the database we set up but the static_files will be wiped.
This is us if we host our images on EC2 instances instead of S3 buckets:
I will continue on how to set up and configure S3 bucket for our app in part 2:
If you don’t need file upload and only a server and a database you are good to go. Otherwise see you “after the jump”.