Examples of EC2 Instance Metadata outputs
Compared to the output of Ansible facts
I was trying to compare Ansible facts with EC2 instance metadata, to check if instance metadata provides the same level of detail as Ansible does. But after a minute of searching, I didn’t find an example output returned from a call to 169.254.169.254
or fd00:ec2::254
, the IP addresses where the metadata service lives. It’s probably out there, but I figured I would make this more googleable for future me, and anyone out there with the same question.
A description of each of the items in EC2 instance metadata is available in the guide on dynamic data categories, but I wanted to see the full output with sample values.
Shout out to Ahmet Alp Balkan for writing a more comprehensive comparison of instance metadata across cloud providers. In this post, I’m just giving examples of outputs.
My takeaways
The output of the below example’s Ansible facts is 456 lines long, and the EC2 instance metadata output in the other example is 126 lines long. Ansible facts give you a lot more system-level information than EC2 instance metadata, down to details like:
- the package manager
- the date and time, in many formats
- info on eth0, like netmask
EC2 instance metadata, however, is able to give you details specific to AWS, pulling in data from other services. For example:
- the IAM role associated with the instance
- security groups
- reservation ID
Ansible, in its community.aws
collection, has modules that can retrieve AWS-specific information. Search for “info” or “facts” on the list of modules. And there’s a module from which you can return security group information with ec2_group_facts
.
Example output of Ansible facts for CentOS 7
Example output of EC2 Instance Metadata for Amazon Linux 2
To get the output in a readable format, I adapted Vipin Ajayakumar’s python script to include not just /meta-data
but also /dynamic
. The script recurses through 169.254.169.254/latest
and converts the output to JSON:
I ran it on an “Amazon Linux 2 AMI (HVM) — Kernel 5.10, SSD Volume Type — ami-002068ed284fb165b (64-bit x86)” instance. Here’s the redacted output of the meta-data
portion of the EC2 instance metadata:
The dynamic
portion of EC2 instance metadata:
Comparison of Access
With Ansible, you can refer to an item in the facts like {{ ansible_facts[‘devices’][‘xvda’][‘model’] }}
.
With EC2, you can curl 169.254.169.254/latest
and append the path you want to that, like curl 169.254.169.254/latest/meta-data/ami-id
.
I hoped that saved you a few minutes! Leave a comment to share your experiences with Ansible facts and EC2 instance metadata, or if you have any cool approaches to using these.