Morgan Clark
5 min readMar 30, 2022

--

How to use the AWS CLI to launch an ec2 instance with a user data script which will install and start an Apache Web server (HTTP)

The AWS Management console provides a solid option for creating an EC2 instance and provisioning an Apache web server (HTTP), however, using the AWS CLI (Command Line Interface) requires the user to be familiar with the various commands to accomplish the same task and can be very challenging to a novice developer, such as myself. However, that is exactly what this article covers, How to Launch an EC2 instance with an Apache Web server via the AWS CLI.

Prerequisites:
An IAM user with AWS EC2 permissions and a key pair {If key pair is available proceed to step 2, otherwise start with step 1.}
AWS CLI installed and configured

Objectives:
Create a security group and add rules to enable SSH to instances in the same security group.
Create a t2.nano EC2 instance
Create a script to update all packages, install, enable and start Apache
Verify that Apache is running by using the public ip of the instance in the web browser

Step 1: Create a new key pair if one is not currently available, otherwise proceed to step 2

aws ec2 create-key-pair — key-name 1newKeyPair — query ‘KeyMaterial’ — output text > 1newKeyPair.pem

Step 2: Obtain the VpcID assigned to your profile

Command execution result: vpc-0d56189e5c80efb0a

Step 2: Create a security group and add rules to allow inbound and outbound traffic.
aws ec2 create-security-group — group-name <Enter a group name> — description “ description” — vpc-id “ Option entry”

aws ec2 create-security-group — group-name <Enter a group name> — description “Enter a description” — vpc-id vpc-0d56189e5c80efb0a

Command execution result: sg-08041c265029551b6

Next, configure the rules for the security group to allow access for SSH (port 22) and HTTP (port 80). I used 0.0.0.0/0 for the CIDR to allow any ip addresses to access the service provided by the EC2 instance.

aws ec2 authorize-security-group-ingress — group-id sg-08041c265029551b6 — protocol tcp — port 22 — cidr 0.0.0.0/0

Command execution result

aws ec2 authorize-security-group-ingress — group-id sg-08041c265029551b6 — protocol tcp — port 80 — cidr 0.0.0.0/0

Command execution result

Step 3: Create and name the script that will update packages, install, start, and enable Apache to run on the instance.

vim script4apachewebsvr.sh (script will be used in the user_data field when the ec2 instance has been launched.) #!/bin/bash
yum update -y
yum install httpd -y
systemctl enable httpd
systemctl start httpd

Press i on the keyboard to enter <INSERT mode>, then enter the commands above into the VIM editor

Press the ESC key { exists active command}, then the “:” <colon key> and enter : wq! to save and exit the file

{Make a note of where the new script has placed on your computer, you ill need the path for the ec2 launch step}

Step 4: Create and launch an EC2 instance:
Run this command to locate an AMI (Amazon Machine Image)

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” — output text

Command execution result {ami-03e0b06f01d45a4eb}

To launch a new instance, enter the code below, replacing the AMI id, instance type t2.nano, key pair name ( — enter only the name and not the extension type .pem) for the user, and security group id, and the newly-created script.

aws ec2 run-instances — image-id ami-????????? — count 1 — instance-type ????? — key-name ???????? — security-group-ids sg-???????????? — user-data ????????? {path to file on your computer}

aws ec2 run-instances — image-id ami-03e0b06f01d45a4eb — count 1 — instance-type t2.nano — key-name 1newKeyPair — security-group-ids sg-08041c265029551b6 — user-data file://script4apachewebsvr.sh

Step 5: Verify that it has Apache correctly configured by entering the public ip of the instance into the browser search bar:

To identify the public ip of the instance, run the command below: (*Note: replace the AWS region, and instance id — this can be copied from the output of the previous command.)

aws ec2 describe-instances — instance-ids — query “Reservations[*].Instances[*].PublicIpAddress” — output text

Command execution result {3.83.213.240}

Next, copy the public ip and paste it into the search bar of the browser, click ENTER — if configured correctly, the Apache test page should appear.

Apache HTTP web server create successfully!!!

$tep 6 *** Be $ure to $TOP or TERMINATE the instance to avoid incurring fee$ from AW$. ***
Locate and copy the instance-ID within AWS console

Now it’s time to save some $$$
$TOP the instance by entering the following command:

aws ec2 stop-instances — instance-ids <replace with INSTANCE-ID that you copied in the AWS console>

Command execution result

TERMINATE the instance by entering the following command: aws ec2 terminate-instances — instance-ids <INSTANCE-ID>

aws ec2 terminate-instances — instance-ids 030668876764bcb1d aws ec2 stop-instances — instance-ids i-030668876764bcb1d

Command execution result

Congratulations, you have successfully used the AWS CLI to launch an ec2 instance, provision an Apache HTTP (web server), then clean up the session by stopping the instance and finally terminating the instance and thereby $aving $ome money.

See you soon for my next article!

--

--

Morgan Clark

Sr. Telecom Engineer, pursuing new skills and career opportunities in DevOps.