Django DevOps Part 2: Creating EC2 instance and DNS configuration

Sebastian Gomes
8 min readAug 24, 2024

--

Welcome to the second part of our Django DevOps series. In this tutorial, I will show you how to create a virtual machine in AWS using their EC2 service and how to point a domain to that virtual machine. I will also guide you through the process of buying a domain from Godaddy and configuring DNS settings. You can also choose any other services to purchase a domain, the basic idea is the same.

What have we done so far

In the last tutorial of this series, I showed you how to dockerize a web application using Django, Gunicorn, Nginx, and PostgreSQL. Here is the repository of what we have done there: Part 1 repository.

So let's get started!

1. Setting up our Virtual Machine in EC2

A Virtual Machine (VM) is a software-based emulation of a physical computer. It runs an operating system and applications just like a physical computer would, but it is created and managed by virtualization software. This allows multiple VMs to run on a single physical machine, sharing its resources (CPU, memory, storage, etc.) while operating independently of each other.

We will be using Amazon’s EC2 Service (Elastic Compute Cloud) to create and manage virtual machines (or “instances”) in the cloud. However, you can also use your own virtual machines from a different service (like DigitalOcean or Heroku).

Step 1: Create a free tier EC2 instance

  • First, create an account in AWS.
  • Go to the AWS Management Console and sign in.
  • In the AWS Management Console search bar, type “EC2” and select it from the dropdown. This will take you to the EC2 Dashboard.
  • On the EC2 Dashboard, click the “Launch Instance” button.
  • Instance Name: Give your instance a name.
  • Select an AMI: Under “Choose an Amazon Machine Image (AMI),” select “Ubuntu Server, Architecture: 64-bit (x86)”.
  • Scroll down to “t2.micro” under the “Instance Type” section. The t2.micro is part of the AWS Free Tier, providing 750 hours per month of usage for the first 12 months. Even if you are charged, it will be very minimal.
  • Now we will go to the next section and set up our private key.

Step 2: Select or Create a Key Pair

Now we will create a secure key to access our instance from our computer using SSH. Click on ‘create new pair’ if you don’t have any existing keys.

  • Key Pair Name: Give your key pair a name, e.g., django_devops_vm_key. Keep everything else as default.
  • Download Key Pair: Click on ‘create key pair’ and download it (.pem file). Store it in a secure place.

Step 3: Configure Security Group

Security Group acts as a virtual firewall for our EC2 service. We can control the incoming traffic to our instance in inbound rules and outgoing traffic using outbound rules. The incoming user requests to our app are examples of incoming traffic, whereas when we are installing a package using pip we need to make outbound requests from our instance.

What protocols do we need for inbound rule

We will allow three protocols for our incoming traffic. SSH, HTTP, and HTTPS.

Without SSH we can not remotely log into our EC2 instance. We have to access with SSH, pull our git repository and run, install docker and run the docker commands.

When our dockerized app is running general users will connect with the Nginx service from their service with HTTP and HTTPS (secure connection). We will have to do some extra work for HTTPS later but for now, these three incoming channels need to stay open.

  1. Create a New Security Group: Check the ‘Create security group’ option if you don’t have any rules set up already.
  • Enter a name, for example, ‘django_instance_securiry_group’ for the security group and then give a description.

2. Add Rule for SSH: Check ‘Allow SSH traffic …’

3. Add Rule for HTTP: Check ‘Allow HTTP traffic …’

4. Add Rule for HTTPS: Check ‘Allow HTTPS traffic …’

5. For simplicity we are allowing all IPS to use ‘Anywhere’ but this also increases our security risk.

What does these protocols mean?

These three correspond to three types of network protocols. They run on different ports. Later, we will use these ports in our NGINX config file to route different types of requests in different directions.

  • Type: SSH, Protocol: TCP, Port Range: 22
  • Type: HTTP, Protocol: TCP, Port Range: 80
  • Type: HTTPS, Protocol: TCP, Port Range: 443

Important!

4. Outbound Rules: By default, all outbound traffic is allowed the default usually works for most use cases.

5. Let’s configure our storage.

Step 4: Add Storage

  • Storage Configuration: By default, you get an 8 GB General Purpose (GP3) SSD. Leave everything else as defaults.
  • Click “Next: Add Tags”: Proceed to tagging.

Step 5: Configure Instance Details

  • Unless you have specific network configurations, leave everything in ‘Advance Details’ as defaults.
  • Leave the number of Instances as 1 on the right sidebar (summary).

Step 6: Review and Launch

  1. Check all the settings to ensure they are correct and click ‘Launch Instance’ when ready.

Step 6: Viewing our Instance

  • Once launched, click “Instances” and you will see our running instance.
  • Note down the public IP address of this instance. We will use it later for connecting our Domain with this IP.
  • Now click on the instance ID, go to the connect link, and copy the command. Then copy the command from there. It should look like the following format:
  • ssh -i key_name.pem ubuntu@public_ip_of_our_instance

Step 7: Accessing our Instance

  • On your local machine, open a terminal (for Windows you can use git bash). Using the CD command go to the directory where you have put your public key.
  • SSH into our VM using the command you just copied.
  • I kept my pem file in the c/django_devops directory
I am using git bash on a Windows computer
  • When you are in the VM you should see ‘ubuntu’ VM’s public IP beside your cursor.
Sucess! We are in

2. Setting up our Domain

Let’s understand Domain Name Resolution

Domain Name Resolution is the process by which a human-readable domain name (like www.example.com) is translated into an IP address (like 192.0.2.1) that computers can use to identify each other on a network. This process involves several steps and I will not dive into it too deeply. However, I am showing you how you can do it with Godaddy. Here is an oversimplified diagram of this process:

A simplified workflow of DNS resolution

Remember we noted down the IP address of our VM (For me it is: 34.229.227.204). Our goal is to bind this IP with a domain name.

Setting up domain resolution in GoDaddy involves configuring DNS settings so that your domain name points to the correct IP addresses or servers for your website, email, or other services. Here’s a step-by-step guide to doing this:

Step 1: Buy a Domain

If you choose not to purchase a domain for this series, you won’t be able to complete the SSL setup in part 3. However, I will still show you how to deploy your Dockerized project on EC2 using your virtual machine’s public IP address. That said, I strongly recommend purchasing a domain for this series.

  1. Go to the GoDaddy website and sign up/sign in.
  2. Search for a domain and buy one. You can skip all the extra charges (Ownership protection, Microsoft email services, and so on).

Step 2: Access the Domain Management Section

  1. Navigate to your account dashboard.
  2. From the dashboard, find the “My Products” section under your username in the top right nav menu.
  3. Click on ‘manage’.

Step 3: Manage DNS Settings

  1. After selecting the domain, you will be directed to the Domain Dashboard page. Click on ‘Domain’ from the left sidebar and we will arrive at the Domain Settings page.
  2. Now click on ‘Manage DNS’.

Step 4: Add or Edit DNS Records

On the DNS Management page, you can add or edit various types of DNS records. We will edit two of these records:

  • Type: A, Name: @
  • Type: CNAME, Name: www

1. ‘A Record’ (IPv4 Address)

This points your domain to a specific IP address (typically your web server’s IP). This is where we will put the public IP address of our EC2 instance.

  1. click the pencil icon next to it.
  2. Type: This should be ‘A’
  3. Host: Enter “@” to point to the domain.
  4. Value: Enter the IP address of our EC2 instance (For me it is 34.229.227.204).
  5. TTL: Set the Time to Live (default is usually fine).
  6. Click “Save.”

2. ‘CNAME www’ Record

This Points a subdomain to another domain name. For our case, we will just use ‘www’ as our first subdomain. This will point www.example.com to example.com.

Our steps will be the same as before

  1. Click “Add” and select “CNAME” from the “Type” dropdown.
  2. Host: Enter the subdomain “www”
  3. Value: Enter the domain name you purchase (for me it's sebastiangomes.com).
  4. TTL: Set the TTL (default is usually fine).
  5. Click “Save.”

We will not configure the rest of the settings to keep this tutorial simple.

Step 5: Verify DNS Changes

  1. DNS changes can take anywhere from a few minutes to 48 hours to propagate across the internet.
  2. You can use tools like What’s My DNS to check the propagation status of your DNS changes globally.

What’s Next?

In Part 3, we will change our codebase and deploy our dockerized Django project in this AWS EC2 instance with a dockerized Certbot service for SSL.

--

--

Sebastian Gomes

I am a full-stack software developer. My interests are SPA frameworks, Django, Node.js, DevOps and, Microservices.