How to Launch an EC2 Instance & Install Apache Server with AWS CLI

Chinelo Osuji
6 min readJul 17, 2023


Using the AWS CLI, we’re going to create a t2.micro EC2 instance and install an Apache HTTP web server. We’re also going to create a simple HTML page and verify that the server is reachable.

Why Should a Company Migrate to AWS?

Let’s say an online retail store wants to move its website to the cloud to improve its online presence and offer customers a more reliable sales platform. By moving its application to an EC2 instance on AWS, the store will be able to handle spikes in traffic and not have to invest in addition hardware, while avoiding up-front costs with pay-as-you-go pricing. The infrastructure provided by AWS is supported with multiple availability zones and automatic failover capabilities, which helps to minimize downtime and ensure access to the store’s products.

First, we must install AWS CLI (Command Line Interface) on our computer. Click here to install.
I’m using Windows, so I selected 64-bit.

After installation, go to your computer’s terminal.

Run aws --version to check the version of the AWS CLI that was installed.

Now we need our Access Key ID and Secret Access Key to access AWS services via the CLI.
We can only view this information when the access key is created.
If this information isn’t saved, we can create a new set of keys.
Login to your AWS account, go to IAM, under Access Management click Users. Select Security credentials then scroll down and click Create access key. For Use case, select Command Line Interface (CLI), check the box at the bottom and click Next twice. We can download the .csv file so that we don’t forget our access keys and click Done.

Go back to the terminal and run aws configure to set up your AWS CLI installation.

Run aws ec2 create-key-pair --key-name ApacheKeyPair --key-type rsa --query "KeyMaterial" --output text > ApacheKeyPair.pem to create a key pair, which is used to authenticate with our EC2 instance.
(Optional) We can run aws ec2 describe-key-pairs --key-name ApacheKeyPair to view the information about the key pair we created.

Run aws ec2 create-security-group --group-name <Name Of Security Group> --description <Description> to create a security group with a name and description of your choosing. The Group ID will be visible.

Run curl to get our public IP address.
In my case, I received an error because when using curl in Windows PowerShell, it tries to parse the HTML content using the Internet Explorer engine and its not available.

So I ran curl -UseBasicParsing to parse the HTML content without using the Internet Explorer engine and I was able to see my public IP address.

Run aws ec2 authorize-security-group-ingress --group-id <Group ID> --protocol tcp --port 22 --cidr to allow inbound traffic for the security group on TCP port 22 (SSH) from your public IP.
And run aws ec2 authorize-security-group-ingress --group-id <Group ID> --protocol tcp --port 80 --cidr to allow inbound traffic on TCP port 80 (HTTP) from any IP.

Now we need the AMI Image ID of an OS we are going to use to create the EC2 instance.
Go to EC2 click Launch Instance and select the OS you are going to use.
I chose CentOS Stream 8 and t2.micro as the Instance type.
Copy the AMI: ami-0cdb8266fcd5d3d63 and paste it in your notepad for later use.

Run vim to create a bash script to install Apache server.
I received an error indicating VIM command is not on my computer.

So I went to to download and install VIM.
I downloaded gvim90.exe for Windows.

I ran the command to create the bash script but I received the same error again.
I realized this is because the vim90.exe file is not in the system's PATH environment variable.
If you are using Windows and this happens to you, navigate to Edit the system environment variables

In the System Properties window click Environment Variables.
In the System variables section select the Path and click Edit.
Click Edit and add the directory containing the vim90.exe file in the field. For me, the path to the directory is C:\Program Files (x86)\Vim\vim90

Now runvim
Inside the text editor, enter the following script below.
To save and exit out of Vim, press Esc key, type :wq and press enter.

#! /bin/bash

# Update system packages and install any available updates
sudo yum update -y

# Install Apache HTTP Server package
sudo yum install -y httpd

# Start the Apache service
sudo systemctl start httpd

# Enable the Apache Service to start on system startup
sudo systemctl enable httpd

# Create an HTML file to display the following message
sudo echo "<h1>Online Store Under Construction.</h1>" > /var/www/html/index.html

Run aws ec2 run-instances --image-id ami-0cdb8266fcd5d3d63 --count 1 --instance-type t2.micro --key-name ApacheKeyPair --security-group-ids sg-0XXXXXXXXXXXXXXX --user-data file:// to launch an EC2 instance using the AMI image ID and t2.micro instance type, key pair and security group we created, and user data as the bash script .
(Optional) We can run aws ec2 describe-instances to confirm the instance exists.
The instance can take a few minutes to be fully up and running.
We can run aws ec2 describe-instance-status --instance-id <Instance ID>to view the instance’s status.

With the instance fully running, we should be able to view the HTML page.
Enter the Public IP Address in your web browser.

And we’re done.
Run aws ec2 stop-instances --instance-id <Instance Id> to stop the instance.
If you don’t need an instance, it’s strongly recommended to stop it so that that you’re not charged for the time it’s running.

(Optional) We can run aws ec2 describe-instance-status --instance-id <Instance ID> to confirm that the instance has stopped.

And that’s it! Thanks for your time. Check back for more!



Chinelo Osuji

DevOps | Cloud | Data Engineer | AWS | Broward College Student