Tech Junkie Blog - Real World Tutorials, Happy Coding!: Iaas With AWS: Install Apache Web Server With User Data

Monday, September 28, 2020

Iaas With AWS: Install Apache Web Server With User Data

 User Data in an instance allows you run commands while your instance boots up.  In the previous posts we just plain vanilla instances so far.  But in this post we are going to install the Apache Httpd service when we create our instance using User Data.

Here are the steps to create an instance with User Data:

In this blog we are going to start our journey into AWS infrastructure with the creation of an EC2 instance which is probably the most common task you'll ever do. 

 Here are the steps to create an EC2 instance on AWS:

1. Log into AWS and on the "Find Services" search box type EC2 and press enter 

2. You will be taken to EC2 screen, then click on "Instances"

2. Click on "Launch Instance"


3. On "Step 1: Choose an Amazon Machine Image (AMI)" screen select "Amazon Linux 2 AMI (HVM), SSD Volume Type"


4. On the "2. Choose Instance Type" screen select the one that says "Free tier eligible"


5. Then select "Next: Configuration Instance Details" at the bottom of the screen
6. On the "3. Configure Instance" select the subnets that are closest to you
7. Scroll down to the bottom of the page expand "Advance Details",  and find the field that says "User Data" and type in the following commands into the field to first update the OS then install, start and enable the httpd service

#cloud-boothook
#!/bin/bash
#Use this for your user data (script without newlines)
# install httpd (Linux 2 version)

yum update -y 
yum install -y httpd.x86_64 
systemctl start httpd.service 
systemctl enable httpd.service 
echo "Hello world from $(hostname -f)" > /var/www/html/index.html

We just created an index.html file to write out the hostname for testing later on



8. Then select "Next: Add Storage" at the bottom of the screen
9. Accept the default and press "Next: Add Tags"

10. Select "Add Tag", then select "Next: Configure Security Group"

11. On this screen you are going to set the configuration for SSH and HTTP traffic, select "Create new security group" then give it a name and description.  There will be and "SSH" rule already assigned accept it, now we are going to add the HTTP rule by clicking on the "Add Rule" button. Select "HTTP" from the dropdown list. In the IP range field type 0.0.0.0/0


11.  Select "Review and Launch" at the bottom of the screen.
12. Make sure the t2.micro image is selected if you want to stay in the free tier, select "Edit instance type" to select it, if it's not. Then click "Launch"

13.  On the "Launch Screen" click on the "Download Key Pair" to download the private key file(.pem). Make sure you remember where you download this file because you will need it to connect the instance later on.  Give your key pair a name and select "Launch Instances"

14. On the "Launch Status" screen click on the instance link to see the status of instance deployment, it will take a couple of minutes to complete the deployment but eventually you will see the the "Instance State" is "running" and the "Status Checks" is good


You've just deployed an EC2 instance in the cloud.  Now open a new tab on your browser and type in the http://  Public DNS or Public IP and you will see the hostname because the User Data has been ran

You can also add User Data to an instance that is already created by selecting Actions: Instance Settings: View/Change User Data


Finally you could access your User-Data with this endpoint 

http://169.254.169.254/latest/user-data

 

No comments:

Post a Comment

Search This Blog