DEV Community

Arina Nasri
Arina Nasri

Posted on

How to deploy an AWS EC2 Instance

While building your applications as a software developer, an important step in the process is to deploy your app for the world to see. If you have never done it before, it can seem very scary and daunting so this blog is meant to serve as a How To tutorial as well as educate on what specific terminology means when you go to deploy. There are many different deployment services, but for this blog, I will be focusing on deploying an EC2 instance on AWS.

What is Amazon EC2?
Amazon EC2 is a web service provided by AWS where users can rent virtual computers to run their own applications. When launching an EC2 instance (which is basically just a server), the instance is within an Availability Zone in the region. The instance is secured with a security group which is just a virtual firewall that controls traffic on your deployment. Then, a private key is stored on your local computer and a public key is stored on your instance. This key pair is used to verify the identity of the user.
Image description

How Do I Do All Of This?
Navigate to this link!4422!3!651751059243!e!!g!!amazon%20web%20server!19852662164!145019249497 and click Sign In to the Console on the top right

Once logged in, you will be greeted with the home page. On the top right corner you will see a drop down bar with specific locations, like so
Image description
Here, you will be clicking on a region where the instance will be deployed. This is when the availability zones come into play because AWS has these zones (essentially data centers) located around the world. You can pick a region based on where your users will be located or a region that has the compliance requirements you seek.

Now, you can use the search bar to search EC2 and once you click that, you will be greeted with this page
Image description
The resources tab is where you can see all the different features EC2 provides. Instances is the virtual server you will create, dedicated hosts is a physical server for your own use, load balancers is used to distribute your application/network traffic across multiple targets, security groups are the virtual firewalls for your instance, volumes provide storage for data that ie needed even if an instance is terminated, auto scaling groups adjust the number of EC2 instances in response to changes in demand, elastic IPs are static IPv4 addresses that can be associated with EC2 instances, key pairs are the public and private keys used to secure your instance, placement groups control the placement of instances in the AWS Cloud to meet requirements, and snapshots are backups of EBS volumes. For our purpose, we will be clicking the orange Launch Instance button.

Now we are launching an instance.

  • First you must input a name for your instance.
  • Then, you pick which AMI you'd like to use. AMI is a template that contains the software configuration required to launch an instance. There are a lot of them, but I personally use Ubuntu.
  • Then you pick the specific tier you'd like to use. This is where you would find an AMI that fits the needs of your specific application and for my purposes, the given free tier is fine for me.
  • Then you must pick an instance type that meets your computing, memory, networking, or storage needs. If you're creating a large application that will require a lot of data to be held in storage and/or lots of traffic, you may need to pick an instance type with larger memory and a higher vCPU. Again, for my purpose, a t2.micro is fine.
  • Then, you pick a key pair. If you hadn't made one prior to launching the instance, thats fine and you can just click create new key pair.
  • Then we deal with network settings. You should create a new security group. There are 3 options: Allow SSH traffic from, Allow HTTPS traffic from the internet, and allow HTTP traffic from the internet. Allow SSH traffic from helps connect to your instance. SSH is a protocol used for secure remote access to a server. It allows for two computers to communicate and share data securely over an unsecured network. This is where you transfer your files through SSH to a remote server. If you click Allow SSH traffic from you can choose between allowing it from anywhere, custom, or my IP. It is best to only allow SSH traffic from IP addresses that need access to the deployment.
  • Finally, you can scroll to the bottom and click Launch Instance.

Now you're able to navigate to your instances through the resources view.
Image description
You'll need to click Instances and then click on your instance's Instance ID link. Once you do that, this will give you all the information about your instance. While in here, take note of the Public IPv4 address and the Public IPv4 DNS address. These will be important later.
Image description
For now, you're going to want to click on security, then the security groups link. This is where you can edit the inbound rules of your instance. These are the settings that control the incoming traffic to your instance. They specify the protocol, port range, and the source of the traffic. One rule should already be there from when you initially created the instance, and you can add a rule to allow traffic from the port you used in your application.

STEP 6 (if you use google authentication, if not just skip)
You are going to need to navigate to Google Cloud and go to APIs and Services and create a new credential for OAuth 2.0 Client IDs. Here, you will link your Public IPv4 DNS link with the specific port you used and the endpoint like so
Image description

Now we can finally work in the terminal. For reference, I use a Macbook Air and specifically work with iTerm and VIM. Also, since my instance is Ubuntu that will be reflected in the terminal.
Remember that .pem file we downloaded that holds our key? We are going to move that to a more secure space in our local computer. For me, I put it in my .ssh file.

  • Move your key: mv path-to-key ~/.ssh (for example, if your key is called Healthier and its currently in your downloads folder, you're going to type mv Downloads/Healthier.pem ~/.ssh)
  • Cd into .ssh and run ls -a to check and see if the key is there
  • Cd out of .ssh and now we are going to need to set the permission of the file to be read-only for the owner. To do this, you are going to do chmod 400 ~/.ssh/key. This means that the owner has read access to the private key file, and no one else can access it
  • Finally we can ssh into our instance Image description
  • The command to run is ssh -i ~/.ssh/keyname.pem ubuntu@ec2address since we know our key is in the .ssh file and ubuntu is the root user for our instance because that is what we originally chose. Your ec2 address can be found in the Public IPv4 address in your instance
  • Once you are ssh'd in, you can run any install/file setup steps necessary for your project and then navigate to your instance's link

Deploying your application is a great way to show your colleagues/friends the application you have been working on. There are many different deployment options but Amazon's EC2 service is the easiest for me to navigate. Happy deploying!


Top comments (0)