Creating an AWS EC2 Instance: A Step-by-Step Guide

Adetutu Oluwasanmi
9 min readDec 1, 2023

--

OUTLINE

  • Introduction
  • Prerequisites
  • Components of an AWS EC2 Instance
  • How to create an AWS EC2 Instance
  • Conclusion

Introduction

Amazon Elastic Compute Cloud (Amazon EC2) is a web service provided by Amazon Web Services (AWS) that allows users to run virtual servers, commonly known as “instances,” in the cloud. Amazon EC2 provides its users with services for managing and provisioning virtual machines.

Instead of having a physical server, Amazon EC2 provides a virtual server that enables its users to scale up or scale down their resources depending on their application requirements.

Amazon EC2 can easily be integrated with other AWS services, such as Amazon S3 for storage, Amazon RDS for databases, Amazon VPC for networking, and others. It has a pricing structure that allows its users to pay for what they are using and how much they are using it.

This step-by-step guide will walk you through the process of creating an AWS EC2 Instance from scratch. Let’s dive in.

Prerequisites

  • AWS account
  • Access to AWS management console
  • Basic understanding of the components of Amazon EC2
  • Basic command line

Components of an AWS EC2 Instance

Instance/ Instance type

Instances are also known as virtual servers and they are used to run applications or host websites. Amazon EC2 has different instances and each instance type has different combinations of CPU, memory, storage, and networking capacity.

Security Group

A security group is a firewall for instances to control inbound and outbound traffic. There are two kinds of security rules; inbound and outbound rules. The inbound rule is the traffic that is coming towards your instance. Outbound is the traffic that is generated from your instance and going out. You can specify the rules to allow or deny traffic coming to and from your instance.

Key Pair

To log in to an EC2 instance, you will need a key pair. We have the public and the private key; the public key is associated with the EC2 instance while the private key is secured on the local machine. In Linux machines, key pairs are used to SSH into the instances.

Tags

Tags are like labels that you can attach to your instance or virtual server. Each tag has a key and a value to organize and manage AWS resources. You can use tags in AWS EC2 by creating simple key-value pairs, such as naming an EC2 instance, specifying the project, identifying the owner, or indicating the environment. You can assign as many tags as needed to provide useful information and organisation for your resources.

Amazon Machine Image (AMI)

In launching an EC2 instance, you need an AMI which are ready-made virtual machine like Vagrant. AMIs contain the necessary information to launch or create an instance. You can choose from a wide range of publicly available AMIs or create your own.

Elastic Block Store (EBS)

Elastic Block Store provides storage volumes that can be attached to EC2 instances to store data, applications, or databases. EBS can also be used to capture changes that have been made to the data on the volume and this is called Snapshots.

Elastic Load Balancing (ELB)

ELB is an AWS service that distributes incoming traffic across multiple EC2 instances, ensuring a more balanced and efficient workload distribution for your website or application.

Auto Scaling

Amazon EC2 Auto Scaling is a service provided by AWS that adjusts the number of EC2 instances in a group based on the current demand for the application such that when the demand is high it increases the number of instances, and when the demand is low, it reduces the number of instances.

How to Create an AWS EC2 Instance

1. Login to your AWS Account

  • Navigate to the AWS Management Console at https://aws.amazon.com/ and Sign in with your AWS account credentials.
  • Once you are logged in, go to the region section to choose the AWS region where you want to launch your EC2 instance and select the “US East (N. Virginia) Us-east-1” region.
  • Navigate to the “Services” dropdown and under the “Compute” section, select “EC2”.

2. Create a Key Pair

  • Navigate to the “Key Pairs” section under the network and security settings, and select it.
  • Proceed to click on “Create key pair” located at the top right corner of the dashboard.

Note: The following steps involve setting up a website from tooplate.com. We would be using this website https://www.tooplate.com/view/2083-steak-house

  • Assign a key pair name adhering to proper naming conventions. For this example, we’ll use the name “Steak-dev”
  • Keep the key pair type as RSA and opt for .pem as the private key format.
  • Click on “Create key pair.” This action will initiate the download of the private key to your computer.
  • The key pair has been successfully created.

3. Create a security group

  • Go to the “Security group” within the network and security section, then click on it. Proceed to click on “Create security group” located at the top right corner of the dashboard.
  • Create a security group with the following naming convention: “Steak-web-dev-sg,” where “Steak” indicates the type of service (web service), “dev” indicates the development environment, and “-sg” denotes it as a security group. You can set the description name to match the security group name.
  • Go to the inbound rules and click on “Add rule”.
  • For the first inbound rule, set the type to SSH, with the default port range of 22. Specify the source as “My IP”. This rule allows SSH access only from the designated IP.
  • For the second inbound rule, set the type to Custom TCP, the port range to 80, and the source as “My IP.” This rule allows incoming web traffic on port 80 only on the specified IP address.

Note: Do not edit outbound rules. Skip it.

  • Scroll down and Click on “Create a security group”.
  • The security group has been successfully created.

4. Launch an Instance

  • Navigate to the “Instance” on the left panel of the console and click on it, then click on “Launch Instance.”
  • Name your EC2 instance and apply tags. When creating the first tag, select “Instances”, “Volumes”, and “Network interfaces” as the resource types.

Note: You can add two or more tags to an EC2 instance such as “Name”, “Environment”, “Project”, etc. It makes it easier to identify the purpose or role of that instance and easily searchable.

  • Choose the Amazon Machine Image (AMI) as the Ubuntu Server.
  • Opt for the t2.micro instance type to stay within the free tier limits.
  • Select the previously created key pair; you can use one key for all instances or assign unique keys to each.
  • Adjust the network settings by choosing the existing security group and maintaining the volume size at 8GB.
  • Set the number of instances to 1 and proceed to launch the instance.
  • After launching, follow the link to open a new tab.

5. Connect to Your EC2 Instance

  • In this tab, select the running instance, and then click on the “Connect” option.
  • Click on “SSH client” and take note of the username “Ubuntu” and the public IP address “34.207.176.65”.
  • Open Git Bash and enter the following command in the terminal:

ssh -i Downloads/Steak-dev.pem ubuntu@34.207.176.65

  • ssh: This is the command for connecting securely to remote servers.
  • -i Downloads/Steak-dev.pem: The -i flag provides the identity file, which is the private key file contained in the Downloads/Steak-dev.pem directory that will be used for authentication.

Note: Remember that the private key was downloaded to your computer as a .PEM file in step 2 after creating the key pair.

  • ubuntu@34.207.176.65: Indicates the username (ubuntu) and the server's IP address (34.207.176.65) you want to connect to.
  • When prompted, type “yes” to confirm the connection.

To configure the website;

  • Log in as the root user by typing “sudo -i”

In the terminal, execute the following commands below in a sequential order;

Update your system;

sudo apt update

Install Apache2 along with wget and unzip tools;

sudo apt install apache2 wget unzip -y

Download the website template;

wget https://www.tooplate.com/zip-templates/2083_steak_house.zip

Unzip it;

unzip 2083_steak_house.zip

Copy the contents to the Apache web server directory;

sudo cp -r 2083_steak_house/* /var/www/html/

Restart Apache:

sudo systemctl restart apache2

Verify that the files have been copied;

ls /var/www/html

Check the status of the Apache service;

systemctl status apache2

  • Open your web browser, type the IP address, and click enter: 34.207.176.65. Then the website would be up and running.
Steak House Website

Please note that if you encounter any connection issues or face challenges with the website configuration, revisit your security group settings (as outlined in step 3) to ensure that both inbound and outbound rules are accurately configured.

6. Terminate the instance

  • Click on the specific instance. Navigate to the “Instance State” and choose “Terminate Instance” from the drop-down menu.
  • Confirm your intention to terminate the instance.
  • After termination, return to the EC2 Dashboard to verify that the EC2 instance is no longer running.

Conclusion

Amazon EC2 provides a scalable and flexible way to run virtual servers in the cloud. Elastic Compute Cloud (EC2) is an important AWS web service for hosting websites, running applications, processing data, and scaling computing resources in the cloud.

EC2 provides users with the ability to adjust resources based on the application needs and it can be seamlessly integrated with various AWS services like S3, RDS, and VPC to enhance its functionality.

In this guide, we’ve successfully created and launched a website on the AWS EC2 instance.

--

--