---
title: "Getting Started with EC2 Metadata and User Data"
description: "This article provides an introduction to using EC2 metadata and user data. Learn how to use these features to customize and automate EC2 instances!"
canonical: "https://adamtheautomator.com/ec2-metadata/"
---

# Getting Started with EC2 Metadata and User Data

> This article provides an introduction to using EC2 metadata and user data. Learn how to use these features to customize and automate EC2 instances!

Source: https://adamtheautomator.com/ec2-metadata/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![Getting Started with EC2 Metadata and User Data](https://adamtheautomator.com/wp-content/uploads/2023/03/ec2-metadata.jpg)

# Getting Started with EC2 Metadata and User Data

[![](https://secure.gravatar.com/avatar/1bc5e1d52466fc3739f550c8d78be310684747bf1466a98d698320627ea243e2?s=192&d=mm&r=g)Michael Nguyen Tu](https://adamtheautomator.com/author/michael-nguyen-tu/)22 March 20237 min. read

Categories: [Cloud](/category/cloud/)

Tags:[AWS](/tag/aws/)[AWS EC2](/tag/aws-ec2/)

Table of Contents

*   [Prerequisites](#prerequisites)
*   [Launching an EC2 Instance with Personalized User Data](#launching-an-ec2-instance-with-personalized-user-data)
*   [Retrieving EC2 Metadata via the Popular cURL CLI Tool](#retrieving-ec2-metadata-via-the-popular-curl-cli-tool)
*   [Retrieving EC2 Metadata via the ec2-metadata CLI Tool](#retrieving-ec2-metadata-via-the-ec2-metadata-cli-tool)
*   [Conclusion](#conclusion)

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](https://aws.amazon.com/free/) 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](https://aws.amazon.com/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.

![Opening the EC2 instance launch wizard](https://adamtheautomator.com/wp-content/uploads/2023/03/image-177.png)

Opening the EC2 instance launch wizard

4\. Now configure your instance name and its application and OS images:

![Configuring the instance name and its OS image](https://adamtheautomator.com/wp-content/uploads/2023/03/image-178.png)

Configuring the instance name and its OS image

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.

Related:[Connect to Private EC2 Instances without an AWS Bastion Host](https://adamtheautomator.com/aws-bastion-host/)

![Configuring instance type and key pair](https://adamtheautomator.com/wp-content/uploads/2023/03/image-179.png)

Configuring instance type and key pair

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.

![Configuring the instance network settings](https://adamtheautomator.com/wp-content/uploads/2023/03/image-180.png)

Configuring the instance network settings

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)](https://aws.amazon.com/what-is/lamp-stack/) 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._

```bash
#!/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._

Related:[How to Run a PowerShell Script From the Command Line and More](https://adamtheautomator.com/run-powershell-script/)

![](https://adamtheautomator.com/wp-content/uploads/2023/03/image-181.png)

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.

![Launching the EC2 instance](https://adamtheautomator.com/wp-content/uploads/2023/03/image-182.png)

Launching the EC2 instance

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.

![Copying the EC2 instance’s public IPv4 address ](https://adamtheautomator.com/wp-content/uploads/2023/03/image-183.png)

Copying the EC2 instance’s public IPv4 address

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.

![Accessing the EC2 instance](https://adamtheautomator.com/wp-content/uploads/2023/03/image-184.png)

Accessing the EC2 instance

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.

![Viewing the PHP page](https://adamtheautomator.com/wp-content/uploads/2023/03/image-185.png)

Viewing the PHP page

## 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/lat](http://169.254.169.254/latest/meta-data/*)[e](http://169.254.169.254/latest/meta-data/*)[st/meta-data/](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](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Connect-using-EC2-Instance-Connect.html).

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).

![Accessing the EC2 Instance Connect](https://adamtheautomator.com/wp-content/uploads/2023/03/image-186.png)

Accessing the EC2 Instance Connect

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.

![Connecting to the EC2 instance](https://adamtheautomator.com/wp-content/uploads/2023/03/image-187.png)

Connecting to the EC2 instance

3\. Once connected, run the following [`curl` command](https://adamtheautomator.com/curl-linux/) on the terminal window to retrieve your EC2 instance’s metadata

```bash
curl http://169.254.169.254/latest/meta-data/
```

Related:[CURL Linux Command : Learning By Example](https://adamtheautomator.com/curl-linux/)

If successful, you will see tons of your instance’s metadata categories, such as **instance-id**, **ami-id**, and more, as shown below.

![Retrieving EC2 instance metadata](https://adamtheautomator.com/wp-content/uploads/2023/03/image-188.png)

Retrieving EC2 instance metadata

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).

```bash
curl http://169.254.169.254/latest/meta-data/ami-id
```

![Retrieving the Amazon Machine Image ID (AMI ID) of your instance. ](https://adamtheautomator.com/wp-content/uploads/2023/03/image-189.png)

Retrieving the Amazon Machine Image ID (AMI ID) of your instance.

> _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.

Related:[Master Grep with Regex and make Troubleshooting a Breeze](https://adamtheautomator.com/grep-with-regex/)

```bash
# 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 an instance’s AMI ID metadata](https://adamtheautomator.com/wp-content/uploads/2023/03/image-190.png)

Retrieving an instance’s AMI ID metadata

## 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.

```bash
ec2-metadata --all
```

![Retrieving all metadata associated with the instance.](https://adamtheautomator.com/wp-content/uploads/2023/03/image-191.png)

Retrieving all metadata associated with the instance.

2\. Next, run the following command to retrieve a specific piece of data, such as the `--ami-id`.

```bash
ec2-metadata --ami-id
```

![Retrieving the AMI ID of an instance](https://adamtheautomator.com/wp-content/uploads/2023/03/image-192.png)

Retrieving the AMI ID of an instance

> _To know more about the `ec2-metadata` command and its available options, run the below command to display the `--help` menu: `ec2-metadata --help`_

![Displaying the help menu](https://adamtheautomator.com/wp-content/uploads/2023/03/image-193.png)

Displaying the help menu

3\. Now, create a script file named `identifier.sh` with your preferred editor (`nano`).

```bash
sudo nano identifier.sh
```

4\. Copy and paste the following code into your script file (_[identifier.sh](http://identifier.sh)_), and save and close the file.

This code retrieves and prints the AMI ID metadata of your EC2 instance.

```bash
#!/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*`).

```bash
sudo bash ./identifier.sh
```

If the script works, you will see an output similar to the one below.

![Retrieving an instance’s AMI ID metadata via script](https://adamtheautomator.com/wp-content/uploads/2023/03/image-194.png)

Retrieving an instance’s AMI ID metadata via script

## 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](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html#user-data-api-cli) to automate the entire process?

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fec2-metadata%2F&text=Getting%20Started%20with%20EC2%20Metadata%20and%20User%20Data)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fec2-metadata%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fec2-metadata%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2021/05/How-to-Connect-to-Private-AWS-EC2-Instances-without-a-Bastion-Host.jpg)

### [Connect to Private EC2 Instances without an AWS Bastion Host](/aws-bastion-host/)

Learn how to do away with the AWS bastion host and leverage AWS Systems Manager’s already-secure connection to SSH to EC2 instances!

![](https://adamtheautomator.com/wp-content/uploads/2026/08/featured_image-1.webp)

### [Survive the Senior Cloud Engineer AWS Interview Loop](/senior-cloud-engineer-interview-loop/)

Prepare for a senior cloud engineer interview with AWS EKS and Control Tower system design reasoning, Terraform locking, and salary negotiation tactics.

![](https://adamtheautomator.com/wp-content/uploads/2026/02/featured_image-1-scaled.jpg)

### [Transition from SysAdmin to Cloud Engineer](/transition-sysadmin-cloud-engineer/)

System administrators already have the networking, Linux, and operational expertise that cloud engineering demands. This guide maps the philosophy shift, technical domains, certifications, and portfolio steps to make the transition.

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
