AWS EC2 Instance Bootstrapping Hands-On Lab for Cloud and DevOps Engineers

AWS EC2 Instance Bootstrapping Hands-On Lab for Cloud and DevOps Engineers

AWS Learning Path for Cloud and DevOps Engineers

📝Introduction

This post will cover the creation and configuration of EC2 Bootstrap script helping to understand what it is and how to use one to automate a server building.

📝Log in to the AWS Management Console

Using your credentials, make sure you're using the right Region. In my case, I chose us-east-1.

📝GitHub repository

Go to the GitHub repo to copy the codes that were used in this Lab on this link.

📝Build an EC2 instance using a Bootstrap Script and Debug Issues

1- Go 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 srv-web01.

4- Scroll down to the Application and OS Images (Amazon Machine Image) to select the Ubuntu logo, and click the dropdown menu to select Ubuntu Server 22.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:

  • Auto-assign public IP: Select Enable from the dropdown menu.

  • Firewall (security groups): Select an existing Security Group or create a new one.

  • Under Advanced details, click the dropdown arrow to expand.

  • Scroll down to User data, paste the following Bootstrap script and click Launch Instance.

      #!/bin/bash
      sudo apt-get update -y
      sudo apt-get install apache1 unzip -y
      sudo systemctl enable apache1
      curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
      unzip awscliv2.zip
      sudo ./aws/install
      echo '<html><h1>Bootstrap Demo</h1><h3>Availability Zone: ' > /var/www/html/index.html
      curl http://169.254.169.254/latest/meta-data/placement/availability-zone >> /var/www/html/index.html
      echo '</h3> <h3>Instance Id: ' >> /var/www/html/index.html
      curl http://169.254.169.254/latest/meta-data/instance-id >> /var/www/html/index.html
      echo '</h3> <h3>Public IP: ' >> /var/www/html/index.html
      curl http://169.254.169.254/latest/meta-data/public-ipv4 >> /var/www/html/index.html
      echo '</h3> <h3>Local IP: ' >> /var/www/html/index.html
      curl http://169.254.169.254/latest/meta-data/local-ipv4 >> /var/www/html/index.html
      echo '</h3></html> ' >> /var/www/html/index.html
      sudo apt-get install mysql-server
      sudo systemctl enable mysql
    

Note: The link used on the script is the default link to view all categories of Instance Metadata from within a running instance, so for that, you use the following IPv4 or IPv6 URLs.

8- Once the instance launch has been successfully initiated, click View All Instances.

9- Once the srv-web01 instance has passed status checks, select this instance, and click Connect.

10- Select EC2 Instance Connect and click Connect.

11- In the terminal, check if Apache1 was installed correctly. The output should display Active: active (running):

sudo systemctl status apache2

12- Verify apache is running. The output should display a few apache processes:

ps aux | grep apache

13- Verify mysql is running:

sudo systemctl status mysql

Note: If the above command does not work, try to use the below command:

sudo systemctl status mysqld

14- Check for any running mysql processes:

ps aux | grep mysql

15- Try to start the mysql service:

sudo systemctl start mysql

Note: These commands should all return an error that the mysql service was not found.

16- Use curl to retrieve the user-data:

curl http://169.254.169.254/latest/user-data

17- At the bottom of the script, notice the following code:

sudo apt-get install mysql-server

Note: The code is missing the -y flag needed for mysql to automatically install without a user prompt.

18- Install mysql-server manually:

sudo apt-get install mysql-server -y

19- Enable the mysql service:

sudo systemctl enable mysql

20- Paste the Public IPV4 address in a new browser tab to access the Apache1 web page. Observe the information that's returned.

📝Use a Fixed Bootstrap Script to Build

1- Repeat Steps 1-6 from the section "Build an EC2 instance using a Bootstrap Script and Debug Issues" changing only the name of the instance for srv-web02 and using the below user-data script:

#!/bin/bash
sudo apt-get update -y
sudo apt-get install apache2 unzip -y
sudo systemctl enable apache2
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
echo '<html><h1>Bootstrap Demo</h1><h3>Availability Zone: ' > /var/www/html/index.html
curl http://169.254.169.254/latest/meta-data/placement/availability-zone >> /var/www/html/index.html
echo '</h3> <h3>Instance Id: ' >> /var/www/html/index.html
curl http://169.254.169.254/latest/meta-data/instance-id >> /var/www/html/index.html
echo '</h3> <h3>Public IP: ' >> /var/www/html/index.html
curl http://169.254.169.254/latest/meta-data/public-ipv4 >> /var/www/html/index.html
echo '</h3> <h3>Local IP: ' >> /var/www/html/index.html
curl http://169.254.169.254/latest/meta-data/local-ipv4 >> /var/www/html/index.html
echo '</h3></html> ' >> /var/www/html/index.html
sudo apt-get install mysql-server -y
sudo systemctl enable mysql

Note: This time, the -y flag for mysql has been added.

2- Click Launch Instance.

3- Once the instance launch is initiated, click View all instances.

4- Once the srv-web02 instance has passed status checks, select this instance, and click Connect.

5- Select srv-web02 from the Instances list, and copy the Public IPv4 address.

6- Paste the IP address in a new browser tab to access the Apache2 web page.

7- Select EC2 Instance Connect and click Connect to connect to the srv-web02 instance in a new terminal window.

8- In the terminal, check if Apache2 was installed correctly. The output should display Active: active (running):

sudo systemctl status apache2

9- Verify apache2 is running. The output should display a few apache2 processes:

ps aux | grep apache

10- Verify mysql was installed:

systemctl status mysql

Note: This time it is running.

11- Confirm mysql processes:

ps aux | grep mysql

12- Verify AWS CLI tool was installed:

aws --version

Congratulations — you have completed this hands-on lab covering the basics of creating and configuring AWS EC2 Instance Bootstrapping.

Thank you for reading. I hope you were able to understand and learn something helpful from my blog.

Please follow me on Hashnode and on LinkedIn franciscojblsouza