red888
red888

Reputation: 31610

Can the EC2 metadata API return objects?

Is it possible to get objects back from the instance metadata API or do you always have to point to what you want directly?

# So I could do this:
$obj = Invoke-Restmethod -uri http://169.254.169.254/latest/meta-data/
$obj.placement.availability-zone
$obj.hostname

# Instead of this:
$zone = Invoke-Restmethod -uri http://169.254.169.254/latest/meta-data/placement/availability-zone
$name = Invoke-Restmethod -uri http://169.254.169.254/latest/meta-data/hostname

Its easy enough without objects, but just curious if its possible

Upvotes: 1

Views: 65

Answers (1)

hjpotter92
hjpotter92

Reputation: 80649

You can try the /latest/dynamic/ path. The exact URL is:

http://169.254.169.254/latest/dynamic/instance-identity/document

It returns a JSON of the following form:

{
  "devpayProductCodes" : null,
  "privateIp" : "172.31.1.178",
  "availabilityZone" : "us-east-1c",
  "accountId" : "XXXXXXXXXXX",
  "version" : "2010-08-31",
  "region" : "us-east-1",
  "instanceId" : "i-XXXXXXXXXX",
  "billingProducts" : null,
  "instanceType" : "t2.micro",
  "pendingTime" : "2016-10-20T16:16:48Z",
  "imageId" : "ami-XXXXXXXX",
  "architecture" : "x86_64",
  "kernelId" : null,
  "ramdiskId" : null
}

Note that it does not have hostname field (and a few others which can be accessed via meta-data call), but it does have quite a lot of information.

Upvotes: 1

Related Questions