Creating an EC2 Instance on AWS

What is EC2 and AWS?

Amazon Web Services (AWS) is a cloud platform that offers many powerful internet services. Their most popular service is Elastic Compute (EC2), a platform which allows you to create virtual machines in the cloud to run applications on. The hardware behind the VMs (network, storage, CPU, RAM) is all handled by AWS. You can simply create an instance of a virtual machine on EC2 at any time and do anything you want, like host a website.

Amazon Machine Image (AMI)

To make spinning up a VM instance easy, AWS offers pre-built and pre-configured images of popular operating systems with commonly used programs installed. The Amazon Linux AMI is a good place to get started, as it includes Python, Ruby, Java, and more.

Access your instance via SSH

You should pay for AWS service and follow the instruction finishing your own configurations.

Select the instance that you just created. You can name it for future convenience with the little pencil icon under the Name column. Press the Connect button (Right next to Launch Instance).

Pulling your Docker Image on your EC2 Instance

Run the following commands to update the pre-installed packages and install Docker.

$ sudo yum update -y
$ sudo yum install -y docker

Start Docker

$ sudo service docker start

The next command allows us to execute Docker command without using sudo.

$ sudo usermod -a -G docker ec2-user

Since we updated these permissions, we need to logout and log back into the account.

$ exit

Run this command to verify that the above permission has been applied succesfully. Information about your Docker installation should be printed.

$ docker info

A common Docker use case

Let’s pretend this is the production server where we want to host our team project webapp, and that our webapp depends on NodeJS, React, and Firebase. At this point, if we weren’t using Docker, we’d have to manually install those dependencies, configure the project directories with the appropriate files, match Python and dependency versions, etc, which can be a lot of work to keep in sync with your development environment. However, because we’re using Docker, we can simply import our image from Dockerhub to obtain a containerized copy of our development environment and app! We can update this image any time during development by pushing the image to Dockerhub and pulling on EC2, a workflow similar to git and Github.
For now, let’s pull the Python image we created earlier and deploy it into production.

To pull the Python image we created earlier, run the following command and supply your Dockerhub login credentials.

$ docker login

We can now run the Docker command to start our Python app! Docker won’t find the image locally, but since we used docker login to log in to Dockerhub, it will search online for the specified image reference. It should find the image, download it, configure the environment, and run the app.

Use the same username/repository:tag format you used to push the image to Dockerhub.

$ docker run -p 443:80 example_username/repository:tag

Important: The -p 443:80 flag in the command specifies that we are mapping our EC2 instance’s port 443 (the HTTPS port) to the Docker container’s port where Flask is broadcasting. (Recall, in our Dockerfile we expose the container’s port 80 and in the Python app we broadcast our Flask webserver on the container’s port 80.)

This is important because in our instance’s Security Group settings, we have opened our EC2 instance’s HTTPS port (443) to the world. Thus, by mapping Flask to our instance’s port 443 with -p 443:80, we can access our website from anywhere! We just need our instance’s public IP address.

Back in the browser, in the Instances section of the AWS portal, copy the IPv4 Public IP address for your instance.

Leave a Reply