EC2 Web Server with Apache using AWS CLI
Published: 2022-10-10 03:07:06



Amazon Web Services provides services and infrastructure to build reliable, fault-tolerant, and highly available systems in the cloud. Services that provide basic infrastructure, such as EC2 and EBS, provide specific features, such as availability zones, auto-scaling groups, and application load balancers that a fault- tolerant and highly available system must take advantage of and use correctly.

As I start working on building a basic infrastructure, first I will be showing you how to create an EC2 Web Server with Apache using AWS CLI. In the coming days, you will be hearing more from me on building complex 2-tier/3-tier infrastructures for highly available systems.



You can find all the projects related to EC2, in my [GitHub Repository](https://github.com/awsmine/EC2_projects).

## What is AWS CLI?:

**_AWS CLI_** or Command Line Interface (AWS CLI) is an open-source tool. It helps you to interact, manage many AWS services and to automate them through scripts. With minimal configuration, the AWS CLI helps to run the commands from the command prompt in your terminal program which are equivalent to that provided by the GUI-based AWS Management Console.

**_Let's get started!_**

## Objectives:

**1.--->** AWS CLI Installed/Configured

**2.--->** Items needed to create an AWS EC2 instance

- AMI (Amazon Machine Image)
- Instance type
- Key Pair
- Security Group
- Add Rules to the Security Group
- Verify the rules created for security group


**3.--->** Create Apache script and create an EC2 Instance to launch a Web Server

**4.--->** Verify your installed EC2 instance and Apache Web Server

## Pre-Requisites:
- AWS Account with admin permissions, not a root account
- IAM user with admin permissions
- Cloud9 IDE - AWS CLI Installed


## Resources Used:

[AWS CLI Installed/Configured](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html)
[AWS EC2 Instance](https://docs.aws.amazon.com/cli/latest/userguide/cli-services-ec2.html)

## Steps for implementation to this project:

###Check if AWS CLI Installed/Configured

As per Amazon "AWS Cloud9 EC2 environments come preinstalled with the AWS CLI, which is authenticated with the permissions of the logged-in AWS user automatically. This enables you to run interactive CLI commands against AWS services from the built-in terminal window in Cloud9 without any additional configuration."

On Cloud9, check to see if AWS CLI is Installed/Configured

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2yso6czywf0uwmymecoo.png)


###Create an AWS EC2 instance

####- Find an AMI using AWS CLI
An Amazon Machine Image (AMI) is an image provided by AWS. It is 
a template for the root volume of the instance (for example, an 
operating system, an application server, and applications).

Select an AMI from which to launch the instance, either 
<u>through the AWS EC2 console</u>/Instances/Launch an EC2 Instance/Under - Application and OS Images (Amazon Machine Image) - Amazon Linux AWS

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h80j5lv4gah0udoe1a8c.png)



**OR**
<u>Run this command</u>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ygcs389vvus3mdu2unih.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0dm587aadjzpsngew26j.png)



####- Instance types

There are many instance types which are grouped together based on different compute, memory and storage capabilities. I have chosen **t2.micro** which is in free-tier.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9zf810z4vad7e008xf3v.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gv96aq9799rjgkukykrv.png)



####- Key Pair

- Run this command 
To create the key pair, which will output the pair to a .pem file in your current directory

**`aws ec2 create-key-pair --key-name MyKeyPair --query 'KeyMaterial' --output text > MyKeyPair.pem`**


- Run this command 
To check key pair created 

**`aws ec2 describe-key-pairs --key-name MyKeyPair`**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/io1gd5y436iumiyo6ryj.png)




####- Security Group


Create a security group for Amazon EC2 instance, which acts as a firewall for in-bound and out-bound network traffic.

**`aws ec2 create-security-group --group-name MyWebSG --description "Allows SSH and HTTP connections for the Web Server"`**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fjvb28ufwe2ehyxx75ar.png)



Copy the security group id, which you will need when creating an EC2 Instance

```
sg-000d2c961be941fa0
```


####- Add rules to the Security Group

Now add rule to allow network traffic on TCP port 22 for SSH connections and on TCP port 80 for HTTP connections for connecting to your EC2 instance.

A mandatory parameter of this command is the **CIDR notation**. Include either your public IP address of your computer (just type value you get from "what is my ip" command in the Google search bar) or leave it open with 0.0.0.0/0 for outside world.

- network traffic on TCP port 22 for SSH connections

**`aws ec2 authorize-security-group-ingress --group-id sg-000d2c961be941fa0 --protocol tcp --port 22 --cidr 0.0.0.0/0`**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4fikrc0c2hjgoibl5e4i.png)


- network traffic on TCP port 80 for HTTP connections

**`aws ec2 authorize-security-group-ingress --group-id sg-000d2c961be941fa0 --protocol tcp --port 80 --cidr 0.0.0.0/0`**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i04018zpjlfstbchf7aj.png)


####- Verify the rules created for security group

**`aws ec2 describe-security-groups --group-ids sg-000d2c961be941fa0`**



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/njbltuwe11sfpxrfy3c7.png)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5cigpuans1wi1sk2rc0k.png)


###Create Apache script and create an EC2 Instance to launch a Web Server



**- Create Apache Script** — apachescript.sh in your Cloud9 folder and type in these commands, save and exit.

```
#!/bin/bash
# use this for your user data (script without new lines)
# install httpd (Linux 2 version)
sudo yum update -y
sudo yum install httpd.x86_64 -y
systemctl start httpd.service
systemctl enable httpd.service

```

**- Create an EC2 Instance** to launch a Web Server

Run this command to create an EC2 Instance

**`aws ec2 run-instances --image-id ami-026b57f3c383c2eec --count 1 --instance-type t2.micro --key-name MyKeyPair --security-group-ids sg-000d2c961be941fa0 --user-data file://apachescript.sh`**


See, the status of your EC2 instance shows “Pending”


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2ab11tp6dwe2j5z4mk1y.png)


###Verify your installed EC2 instance and Apache Web Server

Now move on to your Final Step

- Now, the status of your EC2 instance shows “running”

**`aws ec2 describe-instances`**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s8e0v766vm5tvh1ejn8q.png)


- Now, the status of your EC2 instance shows “running” from AWS
  Management Console

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hq8vvx1sbo0pwp8x45e1.png)


- Login as ec2-user with this command for successful login to via
  SSH:

**`chmod 400 MyKeyPair.pem`**
**`ssh -i MyKeyPair.pem ec2-user@44.201.169.29`**



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/57y973qkdg1rowr9tz2h.png)



- Check your network connections via HTTP

**`sudo systemctl status httpd`**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qy2xy0qbsotmteszs0ap.png)


- Now copy and paste your Public IPaddress on to the browser to 
  see your successful connection.

**`http://44.201.169.29`**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jpw543xbbsacwpo6rvhl.png)

## Cleanup:

- copy the instance-id
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/isxnja16rinfggybr1mc.png)

- terminate the instance 

**`aws ec2 stop-instances --instance-ids i-0e8ba01e4bc58da57`**

first, the status is "stopping"

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3xj7c6w96l2hjy867il8.png)



**`aws ec2 describe-instance-status --instance-id i-0e8ba01e4bc58da57`**

- Now status is "stopped"

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fwbe4j4fw80gdoxcbb1m.png)



**`aws ec2 terminate-instances --instance-ids i-0e8ba01e4bc58da57`**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o12q58u2qr2lpu3jklmj.png)

## What we have done so far:

Created successfully an EC2 Web Server with Apache using AWS CLI. Verified its connection thru SSH and HTTP.