Collect VM metadata
The metadata service enables a VM to retrieve data about itself. In addition to basic queries of VM metadata, the service allows users to provide any user data when creating the VM. This data can then be used by CloudInit to facilitate and speed up the provisioning of cloud servers.
Although the specific details and endpoints of the metadata service may vary at centron, the basic concept is similar. Here is a general guide on how to retrieve metadata for a VM:
Metadata Service IP
VMs can access the metadata service via a specific static IP address, such as 169.254.169.254. This allows similar scripts to be used on different VMs without having to change the destination IP address.
Retrieving a VM’s metadata
You can retrieve metadata from a VM by sending an HTTP GET request to a link-local address, which might look something like this:
http://169.254.169.254/metadata/v1/
Top-Level_Index
Here is an example of how you can use the curl command to send an HTTP GET request to the top level of the metadata endpoint:
curl http://169.254.169.254/metadata/v1/
This returns an index of the available VM metadata and can be viewed as a directory listing. You can send further curl commands to the indexes to view more entries.
User data
Here is an example of how you can use curl to retrieve the user data for a VM:
curl http://169.254.169.254/metadata/v1/user-data
Public network interface
Here is an example of how you can use curl to retrieve the public IP address of a VM:
curl http://169.254.169.254/metadata/v1/interfaces/public/0/ipv4/address
Conclusion
By accessing the specific IP address (e.g. 169.254.169.254) from within a VM, you can retrieve metadata about that VM. Full details of the metadata that can be accessed are usually available on the respective cloud provider’s developer portal.