0% found this document useful (0 votes)
18 views5 pages

EC2 Instance Bootstrapping

The document describes how to bootstrap EC2 instances using user data scripts to automatically configure servers. It demonstrates manually configuring a web server, then using a script to automate the process, finding issues when MySQL didn't install correctly. A fixed script is then used to successfully configure another server.

Uploaded by

Himanshu Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views5 pages

EC2 Instance Bootstrapping

The document describes how to bootstrap EC2 instances using user data scripts to automatically configure servers. It demonstrates manually configuring a web server, then using a script to automate the process, finding issues when MySQL didn't install correctly. A fixed script is then used to successfully configure another server.

Uploaded by

Himanshu Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

EC2 Instance Bootstrapping

Introduction
Amazon Elastic Compute Cloud (Amazon EC2) is a web service that provides secure, resizable compute
capacity in the cloud. In this lab, we configure a web server to corporate standards, noting how much
effort it can take to walk through these steps manually each time. We then learn how to bootstrap an
instance using a user data script, allowing us to automatically build servers. By the end of this lab, the
user will understand what an EC2 bootstrap script is and how to use one to automate one of the most
tedious parts of server building.
Solution
Log in to the AWS Management Console using the credentials provided on the lab instructions page.
Make sure you're in the N. Virginia (us-east-1) Region throughout the lab.
Manually Install Software on webserver-01
Set Up apache2
1. From the AWS Management Console, navigate to EC2.
Note: At the top of the sidebar menu, ensure New EC2 Experience is toggled off for this lab.
2. In Resources at the top, click Instances (running).
3. Select the webserver-01 instance and click Connect.
4. Select EC2 Instance Connect and click Connect to open a terminal window. (You can also
use your own local terminal if you prefer.)
5. Log in to the server using the credentials provided on the lab page for Cloud Server of
webserver-01:
6. ssh cloud_user@<PUBLIC_IP_ADDRESS>
7. Update and install the packages, using the same password as before, when prompted:
8. sudo apt-get update && sudo apt-get upgrade -y
Note: It may take a few minutes to complete.
9. Install the apache2 web server:
10. sudo apt-get install apache2 -y
11. Once installed, return to the AWS Management Console to confirm the apache2 install was
successful.
Set Up the AWL CLI Tool
1. Navigate back to the EC2 dashboard > Instances.
2. Click Instances (running).
3. Select the webserver-01. In the Details section at the bottom of the page, copy the Public
IPv4 address and paste the IP address in a new tab to access the Apache web page.
Note: If using the open address link, you may receive an error that the site can't be reached. This
is because the link defaults to HTTPS instead of HTTP. In the address URL, change HTTPS to HTTP to
load the Apache2 default welcome page.
4. Return to the terminal window and install the unzip tool:
5. sudo apt-get install unzip -y
6. Download the AWS CLI tool:
7. curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
8. Unzip the file:
9. unzip awscliv2.zip
10. Install the AWS CLI tool:
11. sudo ./aws/install
12. Verify AWS CLI version 2 has been installed:
13. aws --version
14. To edit the web page's index.html file, you'll need to grant user access to the file:
15. sudo chmod 777 /var/www/html/index.html
16. To get instance metadata about the server's Availability Zone, enter the following command
Observe the Availability Zone is listed at the front of the username in the result:
17. curl http://169.254.169.254/latest/meta-data/placement/availability-zone
Note: If you don't see any output, wait a moment and retry the command.
18. Add the Availability Zone, instance ID, public IP, and local IP instance metadata to
the index.html file (paste all of this into the terminal):
19. echo '<html><h1>Bootstrap Demo</h1><h3>Availability Zone: ' > /var/www/html/index.html
20. curl http://169.254.169.254/latest/meta-data/placement/availability-zone >>
/var/www/html/index.html
21. echo '</h3> <h3>Instance Id: ' >> /var/www/html/index.html
22. curl http://169.254.169.254/latest/meta-data/instance-id >> /var/www/html/index.html
23. echo '</h3> <h3>Public IP: ' >> /var/www/html/index.html
24. curl http://169.254.169.254/latest/meta-data/public-ipv4 >> /var/www/html/index.html
25. echo '</h3> <h3>Local IP: ' >> /var/www/html/index.html
26. curl http://169.254.169.254/latest/meta-data/local-ipv4 >> /var/www/html/index.html
27. echo '</h3></html> ' >> /var/www/html/index.html
28. Navigate back to the Apache web page, and refresh it to view the results of the changes you
made.
29. Return to the terminal and install mysql:
30. sudo apt-get install mysql-server -y
Note: It may take a few minutes for MySQL to install.
Use a Bootstrap Script to Build webserver-02 and Debug Issues
Set Up the Script
1. Return to the AWS Management Console and navigate to EC2.
2. On the EC2 dashboard, click Launch instances.
3. In the Launch an instance section, under Name and tags type webserver-02.
4. Scroll down to the Application and OS Images (Amazon Machine Image) to select
the Ubuntu logo, click the dropdown menu to select Ubuntu Server 20.04 LTS (HVM), SSD
Volume Type.
5. Scroll down to the Instance type, and click the dropdown menu to select t3.micro.
6. Under Key pair (login), click the dropdown and select Proceed without a key pair (Not
recommended) Default value.
7. Under Network settings, click Edit and enter the following information:
o Auto-assign public IP: Select Enable from the dropdown menu.
o Firewall (security groups): Choose Select existing security group.
o Common security groups: Select EC2SecurityGroup from the dropdown menu.
8. Under Advanced details, click the dropdown arrow to expand.
9. Scroll down to User data and paste in the following bootstrap script:
10. #!/bin/bash
11. sudo apt-get update -y
12. sudo apt-get install apache2 unzip -y
13. sudo systemctl enable apache2
14. curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
15. unzip awscliv2.zip
16. sudo ./aws/install
17. echo '<html><h1>Bootstrap Demo</h1><h3>Availability Zone: ' > /var/www/html/index.html
18. curl http://169.254.169.254/latest/meta-data/placement/availability-zone >>
/var/www/html/index.html
19. echo '</h3> <h3>Instance Id: ' >> /var/www/html/index.html
20. curl http://169.254.169.254/latest/meta-data/instance-id >> /var/www/html/index.html
21. echo '</h3> <h3>Public IP: ' >> /var/www/html/index.html
22. curl http://169.254.169.254/latest/meta-data/public-ipv4 >> /var/www/html/index.html
23. echo '</h3> <h3>Local IP: ' >> /var/www/html/index.html
24. curl http://169.254.169.254/latest/meta-data/local-ipv4 >> /var/www/html/index.html
25. echo '</h3></html> ' >> /var/www/html/index.html
26. sudo apt-get install mysql-server
27. sudo systemctl enable mysql
28. Click Launch instance.
Connect to and View webserver-02
1. Once the instance launch has been successfully initiated, click View all instances.
2. Click the refresh button, if webserver-02 is not displayed.
Note: It may take a few minutes for webserver-02to complete its configuration.
3. Once the webserver-02 instance has passed status checks, select this instance, and
click Connect.
4. Select EC2 Instance Connect and click Connect.
5. In the terminal, check if Apache was installed correctly. The output should display Active: active
(running):
6. sudo systemctl status apache2
7. Verify apache2 is running. The output should display a few apache2 processes:
8. ps aux | grep apache
9. Verify mysql is running:
10. sudo systemctl status mysql
11. Try using mysqld:
12. sudo systemctl status mysqld
13. Check for any running mysql processes:
14. ps aux | grep mysql
15. Try to start the mysql service:
16. sudo systemctl start mysql
Note: These commands should all return an error that the mysql service was not found.
17. Use curl to retrieve the user-data:
18. curl http://169.254.169.254/latest/user-data
19. At the bottom of the script, notice the following code:
20. sudo apt-get install mysql-server
Observe the code is missing the -y flag needed for mysql to automatically install without a user
prompt.
21. Install mysql-server manually:
22. sudo apt-get install mysql-server -y
23. Enable the mysql service:
24. sudo systemctl enable mysql
Use a Fixed Bootstrap Script to Build webserver-03
Set Up the Script
1. Navigate back to the EC2 dashboard, and click Launch instances.
2. In the Launch an instance section, under Name and tags type webserver-03.
3. Scroll down to the Application and OS Images (Amazon Machine Image) to select
the Ubuntu logo, click the dropdown menu to select Ubuntu Server 20.04 LTS (HVM), SSD
Volume Type.
4. Scroll down to the Instance type, and click the dropdown menu to select t3.micro.
5. Under Key pair (login), click the dropdown and select Proceed without a key pair (Not
recommended) Default value.
6. Under Network settings, click Edit and enter the following information:
o Auto-assign public IP: Select Enable from the dropdown menu.
o Firewall (security groups): Choose Select existing security group.
o Common security groups: Select EC2SecurityGroup from the dropdown menu.
7. Under Advanced details, click the dropdown arrow to expand.
8. Scroll down to User data and paste in the following bootstrap script:
9. #!/bin/bash
10. sudo apt-get update -y
11. sudo apt-get install apache2 unzip -y
12. sudo systemctl enable apache2
13. curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
14. unzip awscliv2.zip
15. sudo ./aws/install
16. echo '<html><h1>Bootstrap Demo</h1><h3>Availability Zone: ' > /var/www/html/index.html
17. curl http://169.254.169.254/latest/meta-data/placement/availability-zone >>
/var/www/html/index.html
18. echo '</h3> <h3>Instance Id: ' >> /var/www/html/index.html
19. curl http://169.254.169.254/latest/meta-data/instance-id >> /var/www/html/index.html
20. echo '</h3> <h3>Public IP: ' >> /var/www/html/index.html
21. curl http://169.254.169.254/latest/meta-data/public-ipv4 >> /var/www/html/index.html
22. echo '</h3> <h3>Local IP: ' >> /var/www/html/index.html
23. curl http://169.254.169.254/latest/meta-data/local-ipv4 >> /var/www/html/index.html
24. echo '</h3></html> ' >> /var/www/html/index.html
25. sudo apt-get install mysql-server -y
26. sudo systemctl enable mysql
This time, the -y flag for mysql has been added.
27. Click Launch instance.
Connect to and View webserver-03
1. Once the instance launch is initiated, click View all instances.
2. Click the refresh button, if webserver-03 is not displayed.
Note: It may take a few minutes for webserver-03to complete launching.
3. While you are waiting for webserver-03 to complete its setup, check if the index.html file
for webserver-02 has been configured correctly.
4. Select webserver-02 from the Instances list, and copy the Public IPv4 address.
5. Paste the IP address in a new browser tab to access the Apache web page. Observe the
information that's returned.
6. Navigate back to the EC2 dashboard, and click the refresh button to check the status
of webserver-03.
7. Once the webserver-03 instance has passed its status checks, select this instance and
click Connect.
8. Select EC2 Instance Connect and click Connect to connect to the webserver-03 instance in
a new terminal window.
9. In the terminal, check if Apache was installed correctly. The output should display Active: active
(running):
10. sudo systemctl status apache2
11. Verify apache2 is running. The output should display a few apache2 processes:
12. ps aux | grep apache
13. Verify mysql was installed:
14. systemctl status mysql
This time, it is running.
15. Confirm mysql processes:
16. ps aux | grep mysql
17. Verify AWS CLI tool was installed:
18. aws --version
19. Navigate back to the EC2 dashboard and copy the Public IP address for webserver-03.
20. Paste the IP address in a new tab to confirm the Apache web page for webserver-03.

You might also like