Amazon Elastic Compute Cloud (Amazon EC2)

Hilal Gevrek
10 min readJun 6, 2023

--

In this article, it will cover the details of the AWS EC2, which must be known to be successful in a cloud-related career, and it will be explain how to host a Website to EC2 instance (virtual machine).

What is Amazon EC2?

Amazon Elastic Compute Cloud (EC2) is a web service provided by Amazon Web Services (AWS) that allows users to rent virtual servers on which they can run their own computer applications. Amazon EC2 provides scalable computing capacity in the AWS Cloud, and is designed to make web-scale cloud computing easier for developers.

With EC2, users can launch and manage virtual machines (known as instances) on demand, configure security and network settings, and scale up or down their capacity as needed. Also, EC2 allows users to choose the geographic location of their instances, which can help optimize performance by reducing network latency, and provide a high level of redundancy by placing instances in multiple locations.

Why you should use Amazon EC2?

  • You don’t require any hardware units
  • Easily scalable (up or down)
  • You only pay for what you use
  • You have complete control
  • Highly secure
  • You can access your assets from anywhere in the world

Overall, Amazon EC2 is a flexible, scalable, reliable, secure, and cost-effective platform for running your applications in the cloud. It provides a wide range of features and benefits that can help you build, deploy, and manage your applications with ease.

Amazon EC2 pricing

As part of the AWS Free Tier, you can get started with EC2 for free. This includes 750 hours of Linux and Windows t2.micro instances (t3.micro for the regions in which t2.micro is unavailable), each month for one year. To stay within the Free Tier, use only EC2 Micro instances.

For more information, see AWS Free Tier

Attention!!! Keep in mind that you will be charged for any AWS services you use, so it’s important to familiarize yourself with AWS pricing and to set up billing alerts to avoid any unexpected charges.

Please visit Create a billing to monitor your estimated AWS charges web page for detailed information.

How to host an HTML website on an Amazon EC2 instance?

In this task, it will be explain:

  • Creating an AWS account,
  • Choosing a region for a server, and why is that important,
  • Launching an EC2 instance,
  • Connecting to the EC2 using EC2 instance connect and SSH.
  • Getting the html files which will be hosted, and are already located in the remote private repository in the GitHub account to the EC2 instance.
  • Starting the apache service.

1. Create a free AWS account

If you already have an account, you can continue with sign in option.

Sign up for AWS or sign in to an existing AWS account

In Amazon Web Services (AWS), there are two different privileged accounts. One is defined as Root User (Account owner) and the other is defined as an IAM (Identity Access Management) User.

The root user has full administrative privileges over all AWS resources in the account, while IAM allows you to create and manage users and roles, and grant them specific permissions to access AWS resources. It is recommended to create IAM users and assign them appropriate permissions instead of using the root user, to improve security and reduce the risk of unauthorized access to your resources.

It is recommended to continue with IAM user.

2. Choose the region for the EC2 instance

In AWS, an EC2 region refers to a geographic location where AWS has one or more data centers. AWS currently has multiple regions around the world, including North America, South America, Europe, Asia Pacific, and the Middle East. Each region has its own set of AWS services available, and the cost of using these services can vary by region.

When creating an EC2 instance, you can choose the region in which you want to launch the instance. It’s important to choose the region closest to your customers or end-users, or the region that best meets the requirements of your application, to optimize performance and minimize latency.

In this practice, we are going to choose US East (N. Virginia) region as following.

3. Launch an EC2 instance

In this section, it will create an EC2 instance, create and customize a security group to allow access, and create a key pair in order to ensure our access is secure.

Type ‘EC2’ in search area. If you want, you can click the star next to it and pin it to the top of the screen. Click on ‘Launch Instance’ button.

3.1. Name and Tags

In this step you can add name and tags to help you to administer your AWS resources.

You can define as ‘MyWebServer’ as following.

3.2. Application and OS Images (Amazon Machine Image)

Amazon Machine Image (AMI) is a template for creating a new instance (or virtual machine) for your instance based on user requirements. The AMI will contain information about the software, operating system, volume, and access permissions.

Choose an instance type. Notice that these AMIs are marked free tier eligible, if you don’t want to be charged.

You can choose a free-tier eligible Linux option (Amazon Linux AMI, SSD Volume Type). This is the operating system (OS) for your instance.

3.3. Instance Type

Each instance type has different CPU, memory, storage, and network capabilities, and different pricing options.

Under Instance type, from the Instance type list, you can select the hardware configuration for your instance. Choose the option marked as free tier eligible.

You can choose the t2.micro instance type, which is selected by default. The t2.micro instance type is eligible for the free tier. In regions where t2.micro is unavailable, you can use a t3.micro instance under the free tier.

3.4. Key pair (login)

Create a new key pair, if you already haven’t an existing key pair. The key pair, consisting of a public key and a private key, is a set of security credentials that you use to prove your identity when connecting to an EC2 instance. EC2 stores the public key on your instance, and you store the private key. For Linux instances, the private key allows you to securely SSH into your instance.

Once your instance is launched, you can access it using the public IP address or public DNS name provided by AWS. You can also connect to your instance using SSH or Remote Desktop Protocol (RDP) depending on the operating system you chose for your instance.

3.5. Network settings

Network settings determine which ports and protocols are allowed to communicate with your instance.

You should open your server to port 22, and port 80.

Port 22 is used for Secure Shell (SSH) communication and allows remote administration access to the virtual machine. By opening port 22, you allow incoming SSH connections to your EC2 instance. SSH is a secure protocol used for securely accessing and managing remote servers. Opening port 22 allows you to connect to your EC2 instance using SSH from your local computer or any other authorized device.

Port 80 is the port number assigned to commonly used internet communication protocol, Hypertext Transfer Protocol (HTTP). It is the default network port used to send and receive unencrypted web pages. This is essential if you want to host a website or web application accessible over the internet. Opening port 80 enables users to access your website through their web browsers.

Select ‘Create a new security group’. Give a name to your security group, and add a description which might be a good reminder for you. You can write ‘Allow access on port 80 and 22’. VPC option can be as default. Inbound security groups roles should be as follows.

Network settings
Inbound security group rules

3.6. Configure Storage

You can choose from various types of storage, including Amazon Elastic Block Store (EBS) and instance store volumes.

You can leave it as default option.

3.7. Advanced Details

There are some detailed settings for the instance.

You can skip it.

3.8. Summary

Configure the number of instances to launch, check the summary of the instance.

3.9. Launch Instance

Click ‘Launch instance’.

If you refresh your page, you can see your new instance listed on the EC2 dashboard on ‘running’ status.

4. Connect to the EC2 instance

It is possible to connect to your instance using EC2 Instance connect or SSH.

4.1. Connect using EC2 Instance Connect

Pick the instance that you want to connect, and click ‘Connect’.

When you want to connect with this way, you have to change the SSH setting -‘Custom’, 0.0.0.0/0- as following.

Click ‘Connect’.

If you have reached the screen below, you have command-line access to your EC2 instance.

AWS CLI

4.2. Connect using SSH from local computer

In a terminal window, use the SSH command to connect to the instance. You specify the path, and file name of the private key (.pem), the user name for your instance, and the public IPv4 address for your instance. When you click the Instance ID, you will see your Public IPv4 address of your instance.

Open your terminal -you can use GitBash as terminal- where your pem file is located. Type the command as following with your file name and ipv4.

ssh -i <file_name>.pem ec2-user@<your_ipv4>

If you have reached the screen below, you have remote command-line access to your EC2 instance.

AWS CLI

5. Get the HTML file from GitHub to the EC2 instance

5.1. Assign authorization to the root

sudo su

When you run sudo su, you are prompted to enter your password to confirm that you have permission to switch to the root user. Once you enter your password and press ‘Enter’, the su command is executed with superuser privileges, allowing you to become the root user or another user with elevated privileges. You can now execute commands and access files and directories that are normally restricted to regular users.

5.2. Update the packages

You should update all the packages available on the system with the following command.

yum update -y

5.3. Install ‘httpd’ package to your EC2 instance

The ‘httpd’ package, also known as Apache HTTP Server, is important for the EC2 instance because it provides the software infrastructure necessary to host the website and serve web content.

You can install httpd package with the following command.

yum install -y httpd

5.4. Install Git on your EC2 instance

According to the scenario, the html file to be hosted will be retrieved from the private repository on GitHub. Therefore, you need Git on your EC2 instance. You can install Git on your EC2 instance with the following command.

sudo yum install git -y

5.5. Set the ssh settings

You have to access to your GitHub account for get the html files which will be hosted. Therefore, ssh setting should be done.

If the repository you are trying to access is private, you will need to set SSH setting. If it is a public repository, you can clone it directly.

You can access the html file used in this scenario from my GitHub account.

Generate a new set of keys with the following command.

ssh-keygen -t rsa -C "<your-email@gmail.com>"

You don’t have to define a file or a password. You can skip as empty.

Add your SSH key to ssh-agent.

ssh-agent is a program that starts when you log in and stores your private keys. For it to work properly, it needs to be running and have a copy of your private key.

eval "$(ssh-agent -s)"

Your public key has been saved in ‘/root/.ssh/id_rsa.pub’.

Then, add your private key to ssh-agent with the following command.

ssh-add ~/.ssh/id_rsa

You need to copy your public SSH key.

cat ~/.ssh/id_rsa.pub

Now, you need to define your SSH key to the GitHub account.

Change your directory.


cd /var/www/html

To connect to GitHub server using SSH protocol, you must enter the following command

ssh git@github.com

Clone the repository with following command.

git clone <repo URL>

Notice above that you got the SSH link as the repo URL.

Check with ‘ls’ if it got the repository.

Move the html file to the ‘/var/www/html’ location with following command.

mv page.html /var/www/html

If your html files are zipped: you can install unzip with ‘sudo yum install unzip’ command, and unzip with ‘unzip <folder_name>.zip’ command.

6. Start the apache service

The systemctl command manages both system and service configurations, enabling administrators to manage the OS and control the status of services.

You allows the web server to be automatically launched and available for serving web pages without manual intervention with the following command.

systemctl enable httpd

After run the following command, the web server will begin listening for incoming connections and will start serving web pages and content.

systemctl start httpd

Click the ‘Public IPv4 address’. Modify the address as ‘http’ from ‘https’. If you are successful, you will see the following page.

page.html

7. Terminate your EC2 instance

Click ‘Instances’ , locate the specific EC2 instance you want to terminate in the instance list. Now, you can terminate from the ‘Actions’ dropdown menu, choosing ‘Instance State’ and then click on ‘Terminate’.

I hope I helped. See you in my next post 🙂

--

--