Looking for a way to make your applications more functional and versatile? Why not start passing data to your instances during launch? EC2 Metadata and User Data are here to help!
These features can provide critical information about your instance. And in this tutorial, you will learn how to use EC2 Metadata and User Data to get the most out of your EC2 instances.
Stay tuned and unlock your EC2 instances’ full potential!
Prerequisites
This tutorial comprises hands-on demonstrations. To follow along, be sure you have an AWS account with billing active enabled — a free tier account will suffice.
Launching an EC2 Instance with Personalized User Data
Typically, when you already have a lot on your plate, you would consider automating setup processes. But how? User Data is a feature of EC2 instances that allows you to pass data to the instance during launch. This data lets you configure the instance or install software or packages, easing your computing tasks.
To see how personalized user data works, you will launch an EC2 Linux instance and install a web server. This approach lets you quickly deploy an EC2 instance with the necessary software and configurations to host a website without the time-consuming manual setup tasks.
1. Open your favorite web browser, and log in to the AWS Management Console.
2. Next, search for and select the EC2 service from the list of services on the main page.
3. On the EC2 console, click the Launch Instance drop-down button, and choose the Launch Instance option to open the EC2 instance launch wizard.
4. Now configure your instance name and its application and OS images:
5. Scroll down and configure the instance type and key pair as follows:
- Instance Type – Select your preferred instance type, but this tutorial’s choice is t2.micro.
- Key pair (login) – Create a key pair or select an existing one. But you can select the Proceed without a key pair option if you plan not to connect to the instance via SSH.
6. Under the Network settings section, select Create security group and tick all the SSH, HTTP, and HTTPS checkboxes.
These selections create a security group that allows access to the instance over these ports. Your web server will need these ports open to be accessible via the web.
7. Scroll down to the bottom of the page, and paste the Bash script below to the User data field. This Bash script installs and configures a Linux, Apache, MySQL/MariaDB, and PHP (LAMP) stack on an EC2 instance running Amazon Linux.
If you launch a Windows instance in Amazon EC2, you can also use the User Data feature to run Batch and PowerShell scripts and Bash shell scripts.
#!/bin/bash
# Update the instance's package manager and install the LAMP stack
yum update -y
# Install the necessary packages for a LAMP stack
amazon-linux-extras install -y lamp-mariadb10.2-php7.2 php7.2
# Install Apache and MariaDB
yum install -y httpd mariadb-server
# Start and enable the Apache web server to start automatically on boot
systemctl start httpd
systemctl enable httpd
# Add current user to Apache group
usermod -a -G apache ec2-user
# Set ownership and permissions for the web content directory
chown -R ec2-user:apache /var/www
chmod 2775 /var/www
find /var/www -type d -exec chmod 2775 {} \;
find /var/www -type f -exec chmod 0664 {} \;
# Create a PHP file containing the phpinfo() function
# to confirm the PHP installation works
echo "<?php phpinfo(); ?>" > /var/www/html/phpinfo.php
The size of User Data you can pass to an instance is limited to 16 kilobytes. You must optimize your scripts and configurations to fit within this limit. Exceeding the limit truncates your User Data script, preventing the instance from launching properly.
8. After configuring your instance, click Launch Instance at the bottom of the Summary pane (right-most) to launch your EC2 instance against your User Data script. The data is passed to the instance during the launch and will be executed at the end of the launch process.
Wait for a few minutes for the instance launch to finish since the User Data script needs some time to execute.
9. Finally, open a new browser tab and navigate to the instance’s public IP address to verify that the Apache web server is installed and working correctly.
If all goes well, you will see the default Apache web page, as shown below, which indicates the web server is installed and running correctly.
Alternatively, verify if the PHP installation works by navigating to the instance’s public IP address followed by /phpinfo.php. If the PHP installation works, you will see the PHP information page like the one below.
Congratulations! Using the User Data feature, you have successfully deployed an Amazon EC2 instance with a web server.
As you can see, using the User Data feature is a great way to quickly and easily deploy an EC2 instance pre-configured with all of the necessary software and settings.
Retrieving EC2 Metadata via the Popular cURL CLI Tool
With your EC2 instance running, why not proceed to automation, monitoring, and troubleshooting? But first, you must retrieve EC2 metadata through a local endpoint (http://169.254.169.254/latest/meta-data/).
EC2 metadata holds information about your instance stored on the instance itself in a key-value pair format, only accessible from within the instance.
Note: You cannot retrieve metadata by simply connecting to your instance using SSH or any other remote access protocol.
To retrieve your instance’s metadata, the curl
command will do the trick by sending sending a GET request to the local metadata endpoint. But first, you will connect to your instance using EC2 Instance Connect.
1. On your EC2 console, locate and access your instance, and click the Connect (upper-right) to connect to access the EC2 Instance Connect (step two).
2. Next, keep the default settings as is, and click Connect. Doing so opens a terminal in your browser and establishes a connection to your instance via the EC2 Instance Connect service.
3. Once connected, run the following curl
command on the terminal window to retrieve your EC2 instance’s metadata
curl http://169.254.169.254/latest/meta-data/
If successful, you will see tons of your instance’s metadata categories, such as instance-id, ami-id, and more, as shown below.
4. Now, run the below command to retrieve metadata for a specific category by appending the category name to the URL (ami-id
), like the following. This command retrieves your instance’s Amazon Machine Image ID (AMI ID).
curl http://169.254.169.254/latest/meta-data/ami-id
Similarly, you can retrieve other metadata categories by replacing
ami-id
with instance-id, public-ipv4, and so on.
5. Lastly, run the following if-else statement, which uses the curl
and grep
commands to retrieve metadata about your EC2 instance.
# HOW DOES THIS IF-ELSE STATEMENT WORK?
# If the AMI ID starts with `ami-0` (AMI ID for Amazon Linux 2),
# print the AMI ID with a message saying it is an Amazon Linux 2 instance.
# Else, if the AMI ID does not start with `ami-0`,
# print a message saying the AMI ID was not found
# and the script only works on Amazon Linux 2 instances.
if curl -s http://169.254.169.254/latest/meta-data/ami-id | grep -q 'ami-0'; then
echo 'AMI ID: ami-0c55b159cbfafe1f0 (Amazon Linux 2)'
else
echo 'AMI ID: Not found. Sorry, this script only works on Amazon Linux 2 instances.'
fi
Just to ease your mind, you are not billed for HTTP requests to retrieve instance metadata and user data — what a relief, right? AWS allows customers to access this data free of charge.
Below, an AMI ID is found and printed to the terminal.
Retrieving EC2 Metadata via the ec2-metadata
CLI Tool
When cURL is unavailable, another way to retrieve metadata of an EC2 instance is via the ec2-metadata
CLI tool, which comes pre-installed in all Amazon AMI. Like cURL, the ec2-metadata CLI
tool lets you retrieve metadata that can be helpful when you are writing scripts to run from your instance.
To see how the ec2-metadata
CLI tool works:
1. Run the below command to retrieve all metadata associated with your instance.
ec2-metadata --all
2. Next, run the following command to retrieve a specific piece of data, such as the --ami-id
.
ec2-metadata --ami-id
To know more about the
ec2-metadata
command and its available options, run the below command to display the--help
menu:ec2-metadata --help
3. Now, create a script file named identifier.sh
with your preferred editor (nano
).
sudo nano identifier.sh
4. Copy and paste the following code into your script file (identifier.sh), and save and close the file.
This code retrieves and prints the AMI ID metadata of your EC2 instance.
#!/bin/bash
# HOW DOES THIS CODE WORK?
# Retrieve and save the instance's AMI ID to the AMI_ID variable.
# The cut command extracts the second field from the output,
# which contains the actual AMI ID.
# If the AMI ID is found, print the value to the console
# Else, print a message saying the AMI was not found
echo "Checking AMI ID of this amazing EC2 instance..."
AMI_ID=$(ec2-metadata --ami-id | cut -d " " -f 2)
if [[ -n "$AMI_ID" ]]; then
echo "AMI ID: $AMI_ID"
else
echo "AMI ID: Not found. This script only works on EC2 instances."
fi
5. Finally, run the below bash
command to execute the script (*identifier.sh*
).
sudo bash ./identifier.sh
If the script works, you will see an output similar to the one below.
Conclusion
Running scripts at instance launch time allows you to automate instance configurations, which you have realized in this tutorial. You have automated a web server installation on an EC2 instance with the help of the AWS EC2 User Data and EC2 Metadata.
Further automating processes and smooth troubleshooting is now within reach since you can retrieve EC2 instance metadata with a single command (via curl
or ec2-metadata
).
You can now create powerful automation workflows while saving time and effort when managing your EC2 instances. With the knowledge gained in this tutorial, you can simplify your EC2 automation workflows and efficiently manage large-scale deployments.
But why limit yourself when you can use User Data and AWS CLI together to automate the entire process?