Skip to content

Instantly share code, notes, and snippets.

@spaceplesiosaur
Last active May 31, 2020 05:42
Show Gist options
  • Save spaceplesiosaur/cc81ee7cfe8dea3f541475de43d1371e to your computer and use it in GitHub Desktop.
Save spaceplesiosaur/cc81ee7cfe8dea3f541475de43d1371e to your computer and use it in GitHub Desktop.

Deploy a Django app to AWS

With the help of https://medium.com/saarthi-ai/ec2apachedjango-838e3f6014ab

Spin up an EC2 instance

  • Log into your AWS management console and find EC2 under services. Click on it, then click the orange "Launch Instance" button.

  • Choose an AMI. Generally, I've used the free tier Linux 2018.03 AMI, but I'm choosing the free tier Ubuntu server for now because of the guide I'm using. An AMI is an Amazon Machine Image. It's basically the kind of computer/server that you want it to copy.

  • Choose an instance type. This is the simulated hardware that the machine is running. You'll see them list all of the specs in columns, and describe them for each machine. There's only one free tier, so I go with that.

  • Configure instance details. In this case, I am adding that I want protection against accidental termination, but most defaults stay the same. I do need to create a new file system though. This basically attaches a hard drive to your virtual machines.

  • Create a new file system - just keep hitting next, the defaults are fine.

  • Skip through storage and tags, defaults should be good on storage, tags aren't needed.

  • Configure security group - keep SSH (which allows you to log into it remotely), add rule and choose HTTPS which allows you to use it as a web server over HTTPS.

  • Review and Launch

Create your key pair

  • Download key pair.

marauders-map-key-pair

  • Click on the link to your new instance. Above it, click connect. You will see the info you need to connect. Choose a standalone SSH client as your method.

  • In terminal, in home directory, run the ssh command. Here is an example: ssh -i downloads/marauders-map-key-pair.pem ec2-user@ec2-54-86-136-12.compute-1.amazonaws.com to run your server's terminal. If you need to, run chmod 400 downloads/marauders-map-key-pair.pem next to make it safer.

You can run the ssh command anytime you need to log into your EC2 instance. 'ssh' stands for secure shell, it's a tool for securely connecting to a remote machine. The '-i' means "identity." It's telling ssh to connect using the file that follows the i, which is a key pair certificate. MAKE SURE YOU GET THE PATH RIGHT< THE EXAMPLE COMMAND WILL BE WRONG. Next, you're telling it what you're connecting to and who you're connecting as, which is formatted line an email. username@postname. You'll get this from the example in the "connect" lightbox.

  • The first time you ssh you will be prompted to verify that you are connecting with the right server. Not sure how to verify, though, so just put yes.

Set up your EC2 Server

While in the EC2 environment in your terminal, type sudo apt-get update. apt-get is basically like brew but for Ubuntu. Ubuntu packages are already on the server I've spun up, so by running update I'm basically refreshing all of them.

sudo (super user do) means "do the next thing as 'root', not as the user you're logged in as. It's something to use carefully. Ubuntu uses is a lot for package installation.

Now you can install your dependencies! For this project, we need Python, Apache, and Wisg, which is how Python scripts talk to Apache.

sudo apt-get install python3-pip apache2 libapache2-mod-wsgi-py3

Next, install your Python virtual env: sudo pip3 install virtualenv

"A virtual environment is a tool that helps to keep dependencies required by different projects separate, by creating      isolated python virtual environments for them, and it doesn’t affect the dependencies installed on the OS level."

Make a django directory and cd into it. mkdir django

Once you're in it, name your virutal env. virtualenv nameenv. I'm using marauderenv for this.

And clone your Git Hub repo: git clone https://github.com/spaceplesiosaur/marauders_map_api This will allow you to fetch the latest changes directly from github, so you don't have to push up to both github and AWS like you do with Heroku. AWS allows you to pull from your source of truth.

Activate your virtual env: source marauderenv/bin/activate. If you do an ls in your server, you'll see your env, and in it is a bin file that has the activate script.

cd into your project - it should have been put into your directory when you cloned your git repo. This one is "marauders_map_api"

The above guide suggests editing your files with VIM. Don't do it. It's a great way to fuck up your deployments. Make the changes in your actual files, push to GitHub, then pull down.

Edit settings.py

Open settings.py. I don't need to set up the static root STATIC_ROOT = os.path.join(BASE_DIR, "static/") command at the end of it like in the guide - it's for things that are unchanging, like your JS, CSS, etc. This particular program has a bit of that, you do see something rendered when you open the API, but we added something called whitenoise to handle that.

You will need to put the public DNS into the allowed hosts list though (as a string). Get the public DNS from your server's page on AWS, it is listed at the bottom.

Commit and push your changes to GH.

Install the requirements from requirements.txt

This is why you have the requirements.txt file - now you can get those requirements that you have locally and put them onto the server that will be running your program - that way people who use your program (which they will be doing on your EC2 server), will be able to use your program with its requirements too!

pip install -r requirements.txt

Then install django, make your migrations, migrate!

pip install django python manage.py makemigrations python manage.py migrate

If you have static files, run python manage.py collectstatic. It puts all of your static files into one place, used to always be an s3 bucket. Nowadays put it on AWS and whitenoise can handle it on the fly.

Set up your database

Shell: The program that runs that gives you your bash that allows you to type a command and get a result.

Environment: A big list of key value pairs (environment variables) that every single program that runs on your computer can see.

Processes: A specific running program on a computer

12 factor: An approach to writing programs that tells the program to pull what it needs from the environment as opposed to changing the source code in each context (your computer, EC2, Frank's computer, etc) where you might run it.

We will use 12 factor, which is the process of putting settings in your environment variable. Everything in your settings that needs to be different in production can be but in an environment variable and read from there.

Everytime you run a process, your shell (bash, zsh, etc. It's the program that runs on your terminal that interprets commands) has a process list that's basically a bunch of key value pairs that are strings. You can find your own stuff and stick it in there. It's a good place for secrets. (This is basically the config vars in Heroku settings).

In EC2, you are just given a computer...so you have to do a bit of shell configuration yourself. After you configure your shell, you need to put a script in your program that tells it what to change based on the environment. In Node, you've seen the PROCESS.env stuff.

Django needs to be told 3 things via the environment at minimum

  • debug
  • secret key
  • database

Put the 3 variables in your settings

Run your test server!

ec2-54-146-68-164.compute-1.amazonaws.com:8000

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment