AWS Projects-EC2 Part 1: Creating an EC2 instance to display a Basic web page
Tags: aws, ec2, tutorial, cloud
Published: 2022-10-24 12:56:18

## **Links to other labs**

1. [IAM](https://dev.to/msaghu/aws-projects-iam-creating-users-and-groups-14cg)
2. [EC2 - Part 1](https://dev.to/msaghu/aws-projects-ec2-creating-an-ec2-3ejb)

---

## **What is AWS EC2**
AWS Elastic Compute Cloud(AWS EC2) is a web service that provides resizable compute capacity in the cloud.

With the web service interface of Amazon EC2, you can obtain and configure capacity with minimal friction. It provides you with complete control of your computing resources. You can run application servers, blogs, batch processing, and more.

---
Objectives for this exercise

1. Launch our EC2 instance with termination protection.

2. Monitoring an EC2 instance using CloudWatch.

3. Updating your security group and accessing the web server.

---

## **Let's get into it**

**1. Launch our EC2 instance with termination protection.**

- Termination protection prevents a user from accidentally terminating an EC2 instance.

- In the AWS Management Console on the Services menu, choose EC2.

- In the left navigation pane, choose EC2 Dashboard to ensure that you are on the dashboard page.

- Choose instances, and then select Launch instance.


![EC2 dashboard](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0v6w65uc34lh8szmgfae.png)

- In the Name and tags section, for Name, I entered Web-Server

- Choose the Add additional tags link.
From the Resource types dropdown list, ensure that both Instances and Volumes are selected.

- Locate the Application and OS Images (Amazon Machine Image) section. It is just below the Name and tags section.
Select the Amazon Linux 2 AMI . 

- As we try to remain in the free tier range, we will keep the default instance type, t2.micro.

- In the Key pair (login) section, from the Key pair name - required dropdown list, choose Proceed without a key pair.

- In the Network settings section, choose Edit.

- From the VPC - required dropdown list, choose the default VPC.

- In the Network settings section, for Security group name - required, enter Web Server security group.

- Since we don't want to access this VPC via SSH, we will delete the existing SSH rule, next to Security group rule 1, choose Remove.

- In the Configure storage pane, keep the default storage configuration.
 
- Expand the Advanced details pane then choose enable from the Termination protection drop down list.

- To successfully start the web server, we paste the follwing code into the User data section 

```
#!/bin/bash
yum -y install httpd
systemctl enable httpd
systemctl start httpd
echo '<html><h1>Hello From Your Web Server!</h1></html>' > /var/www/html/index.html
```

- We then click Launch instance.
![A successful EC2 instance launch](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cdl0svut233rjfveagkp.png)

- View all instances to confirm that its passed all checks.
![Image 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/77mkxpymw8rds5ohmbmw.png)

**2. Monitoring an EC2 instance using CloudWatch.**

- Choose the Status checks tab, then choose Monitoring tab.

![Successful upload ](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l51w4r86z87xbl2u0bw1.png)

- To see if all packages were successfully installed, go to the top of the page, choose the Actions  dropdown menu. Select Monitor and troubleshoot Get system log. We see that HTTP package was installed from the script above;


![Monitor and troubleshoot ](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f8fsiqzw3dercx4jcyjb.png)

- Return to the Amazon EC2 dashboard, by choosing Cancel.

- With your Web-Server selected, go back to the Actions  dropdown menu, and select Monitor and troubleshoot , Get instance screenshot.

![Get instance screenshot](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/km0smymzxnedijdwlmrx.png)

**3. Updating your security group and accessing the web server.**
Since we didn't choose any inbound rules, we will not be able to connect to our instance. We can check this by:

- Select the check box next to the Amazon EC2 Web-Server that you created, and then choose the Details tab. 

- Copy the Public IPv4 address of your instance to your clipboard and paste in a new tab, you will get an error. Now let's change this:

![Image browser](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/25f7uoq4ewxgumxj79kf.png)

- Leave the browser tab open, and return to the EC2 Management Console tab.

- From the left navigation pane, choose Security Groups.

- Next to Web Server security group, select the  check box.

- Choose the Inbound rules tab, where we will see that the security group currently has no rules.


![Security group](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/emcbt2mvmwpi5zeadlff.png)

- Choose Edit inbound rules, and then choose Add rule and configure the following options:
Type: Choose HTTP.
Source: Choose Anywhere-IPv4.

- Save the rules, then go back to the open tab and refresh. The page will now display your HTML document:
![Successfully deployed](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jtmunpw8cocwevtmonw1.png)