EC2 元数据 API return 对象可以吗?

Can the EC2 metadata API return objects?

是否可以从实例元数据中取回对象API,或者您是否总是必须直接指向您想要的对象?

# 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

没有对象也很容易,但如果可能的话,我很好奇

你可以试试/latest/dynamic/路径。确切的 URL 是:

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

它returns一个JSON的形式如下:

{
  "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
}

请注意,它没有 hostname 字段(以及其他一些可以通过 meta-data 调用访问的字段),但它确实有很多信息。