As a user of Amazon Web Services, I am still constantly finding new tools, APIs and features to use within their mammoth system. Because of the huge amount of documentation available, it is difficult to find useful information until I need it for a specific reason. I was looking for a way to make sure that an .exe that I was building could only be run on an EC2 instance, and that’s when I came across EC2 Instance Metadata.
EC2 Instance Metadata is instance-specific metadata that can be accessed only from within the instance via a local server and an API. This is great for two reasons:
Because you can ensure that any .exe or script you are running can only be run on an Amazon EC2 instance by pinging or otherwise interacting with the API .
You have access to a whole lot of really useful information about the instance and how it was made.
The instance metadata can be easily retrieved with a HTTP
GET request, and all responses have the
text/plain content type.
All instance metadata is accessed at the following URI:
http://169.254.169.254/latest/meta-data/. This data is only accessible from within the instance. The
latest section of the URI is the version of the metadata API, which starts at version 1.0. There is a lot of different metadata you can access via the API, see here for a full list of the different options. Some of the more useful ones are:
Gets the ID of the AMI(Amazon Machine Image) that the EC2 instance was created from.
The private hostname of the EC2 instance.
The ID of the EC2 instance, which can also be found in the AWS console.
All of the security groups (if any) that the EC2 instance belongs to.
The public IP address. If an elastic IP address is associated with the instance, the value returned is the elastic IP address.
You can use something like curl to interact with the API or your favourite framework’s method of consuming APIs. For example, I used a WebClient in my .NET class library to get the response from the API, and then parsed the response using VB.NET.
EC2 instance metadata is a really useful way to get information about an instance from within the instance and it can also be used to create AMI configurations. This, paired with the CLI or the AWSSDK of your choosing can be used to create powerful tools for automating tasks and interacting with your AWS resources.