Making a EC2 Instance including Apache with AWS CLI

Mitch Cassidy
3 min readMar 26, 2022

--

In this post I will be going over how I created a EC2 instance with an Apache web server through the AWS CLI.

First you will need to make sure you have somethings. I needed to have the AWS CLI installed and configured on my local PC. I also needed to have Vim installed and working.

1|Create

The first step I did was creating a new keypair in AWS using the CLI.

aws ec2 create-key-pair --key-name KeyPairName --query 'KeyMaterial' --output text | out-file -encoding ascii -filepath KeyPairName.pem

And as you may have guessed the bold text is where I entered the name I chose for my key pair. You can either check your keypair in the CLI or on your AWS account on the EC2 page under network and security. Make sure you save you key pair to your computer somewhere.

Next I created a Vim file to install an Apache web server for my EC2 instance. To do this I created a Vim file named apache_install.sh

Then I edited the Vim file as shown..

#!/bin/bash
sudo yum -y update
sudo yum -y install httpd
sudo systemctl start httpd
sudo systemctl enable httpd

I then saved the file and made sure I had the file path for it saved for later. If you are at the file it is easy to find the file path by right clicking the file and going to “Properties”.

Next I created the security groups for this instance to open the ports and allow public access to it. First I found the VPC.

aws ec2 describe-subnets

I used this to find the VPC, I saved the VPC text then continued with the security group.

aws ec2 create-security-group --group-name securitygroupname --description "Description for security group" --vpc-id vpctext

Here again the bold text is where I put my desired name for the security group, the description for the security group and then the VPC I saved from the last step.

Once this was entered, in the response on the CLI, I saved the security group ID which looked something like sg-xxxxxxx

Next I did the permissions for our security group.

aws ec2 authorize-security-group-ingress --group-id sg-xxxxxxx --protocol tcp --port 22 --cidr 0.0.0.0/0aws ec2 authorize-security-group-ingress --group-id sg-xxxxxxx --protocol tcp --port 80 --cidr 0.0.0.0/0

These command opened the ports 22 and 80 for public access to the instance we will create. The bold here is where I entered my specific security group’s ID.

2|Launch

Before I launched the instance I need to find the AMI I will use.

aws ec2 describe-images --owners amazon --filters "Name=name,Values=amzn2-ami-hvm-2.0.????????.?-x86_64-gp2" "Name=state,Values=available" --query "reverse(sort_by(Images, &Name))[:1].ImageId" --region us-east-1 --output text

This command found the AMI for the free tier eligible Amazon Linux II AMI t2.micro that I wanted to use. After my input I copied the AMI for when I launched the instance.

This is finally where I launched the instance,

aws ec2 run-instances --image-id ami-xxxxxxx --count 1 --instance-type t2.micro --key-name KeyPairName --security-group-ids sg-xxxxxxx --subnet-id subnet-xxxxxxx --user-data file://"FILEPATH"

And of course for the bold text I entered the corresponding values from the things I have saved throughout this project, the keypair name I created without the file extension, the security group ID, AMI, and subnet ID. Then the last bold text is where I entered the file path to my Vim file for the apache webserver.

This concluded my launching of the instance. Next I just ran some checks to make sure it was up and running properly. I simply copied the Public IPv4 address and then entered it into my browser while making sure I put “http://” before the IP address. I was greeted by the following screen which concluded the project.

--

--