Deploying AWS CLI to Launch an EC2 Instance with an Apache Web Server

Alexandria Matthews
5 min readMar 20, 2023

--

Hi everyone!

This week we continue delving into Amazon Web Services and EC2 instances. This publication entails the steps needed to launch an EC2 instance with an Apache Web Server using the AWS Command Line Interface (AWS CLI).

To get started, the first thing you will need to do is ensure that the AWS CLI is installed on your device. For installing or updating the latest version of the AWS Command Line Interface for your particular operating system, please follow the instructions found here:

https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html.

After the installation is complete, you will need to log into your AWS account to create an AWS Identity & Access Manager (IAM) user and log in with this account, instead of a root user. This is a good practice to have to protect sensitive information, as the root user account has FULL access to billing and resources.*

Let’s Get Started

1. We need to configure the AWS CLI to be compatible with AWS:

In your command terminal type the command “aws configure”

I am using the Powershell terminal for Windows

You should then see prompts to input your AWS access key ID, secret access key, default region name, and default output format:

To retrieve your access key ID and secret access key, you can follow the instructions here:

https://docs.aws.amazon.com/powershell/latest/userguide/pstools-appendix-sign-up.html

2. Creating a Key Pair

Now it’s time to create a key pair using the AWS CLI. In your terminal enter the following command:

aws ec2 create-key-pair — key-name <KeyPairName>

*Where you see, <KeyPairName> be sure you enter the name you would like for your keypair!*

Now this is where I actually began to run into issues. For some reason, whenever I originally created my IAM user, there was only a Access Key ID that was generated. I kept entering my Access Key ID twice. I had to go back into my IAM user account and generate the Secret Access Key.

When initially trying to create the keypair, I kept receiving a message stating “AWS was not able to validate the provided access credentials.”

Key Pair Error Message

3. Locating your Virtual Private Cloud (VPC)

The VPC is a cost effective private cloud within the public cloud that allows businesses/users to use only the resources they need. You will need to use the following command to locate your VPC ID:

aws ec2 describe-vpcs

The VPC ID is highlighted in pink

4. Creating a Security Group

After locating your VPC ID, we will need to create a Security Group. The security group acts a firewall that monitors incoming and outgoing traffic.

Here is where you will copy and paste your VPC ID in the following command and name your Security Group:

aws ec2 create-security-group — group-name <SecurityGroupName> — description “My security group” — vpc-id <vpcid>

Security Group Creation

When you receive your output, be sure to save the sg-xxxxxxx number that you see. We will need it a few steps down the line to run our instance!

5. Allowing Traffic by Opening Ports:

For this walkthrough, I have decided to allow SSH and HTTP traffic by opening ports 80 and 22 by using the two commands:

· aws ec2 authorize-security-group-ingress — group-name LUITWK6 — protocol tcp — port 80 — cidr 0.0.0.0/0

· aws ec2 authorize-security-group-ingress — group-name LUITWK6 — protocol tcp — port 22 — cidr 0.0.0.0/0

Allowing Traffic for Port 80
Allowing Traffic for Port 22

6. Installing an Apache Web Server

Now let’s run a script that enables us to update all packages on our system, installs and starts the Apache web server.

First use :

vim apache_script.sh

Then:

#!/bin/bash
yum update -y
yum install -y httpd.x86_64
systemctl start httpd.service
systemctl enable httpd.service

7. Launching and Running an EC2 Instance

Before we attempt to launch our EC2 instance, we will need to visit our AWS Console and locate our Amazon Machine Image ID(AMI).

The AMI ID can be found under the EC2 Services in the AWS Console.

Click “Launch Instance” and scroll down to view the available operating systems:

*The AMI ID for the free tier Amazon Linux operating system I have chosen is highlighted in green below.*

To run our EC2 t.2 micro free-tier instance we can use the following command:

aws ec2 run-instances — image-id ami-xxxxxxxx — count 1 — instance-type t2.micro — key-name KeyPairName — security-group-ids sg-001e03efe6d5ecc5f — user-data file://apache_script.sh

Be sure to include and update your AMI ID, keypair name, AND security group ID!

Here is an example of what my command looks like:

aws ec2 run-instances — image-id ami-0c2b0d3fb02824d92 — count 1 — instance-type t2.micro — key-name LUITWK6keypair — security-group-ids sg-001e03efe6d5ecc5f — user-data file://apache_script.sh

To verify that our instance is now successfully running, you can visit the AWS Console and go to the EC2 dashboard to view it:

Running Instance

8. Verifying and Pinging IP Address:

Let’s verify the IP address in our web browser.

I entered my IP address https://18.207.235.139 and the Apache Web Test Page displays!

We have successfully used the AWS CLI to launch an EC2 instance with an Apache web server!

This by far was the most difficult project I’ve been tasked with to date. I worked on it relentlessly for 4 days straight, but I am so glad I finally saw the light at the end of the tunnel.

Thank you for taking the time to read my article and next week I should have a new one for you!

--

--

Alexandria Matthews

DevOps Engineer | Level Up in Tech| Linux Certified| AWS | Here is where I will showcase my progress along my DevOps journey!