Query EC2 tags from within instance

前端 未结 14 1739
暗喜
暗喜 2020-12-02 05:57

Amazon recently added the wonderful feature of tagging EC2 instances with key-value pairs to make management of large numbers of VMs a bit easier.

Is there some way

14条回答
  •  执笔经年
    2020-12-02 06:42

    Once you've got ec2-metadata and ec2-describe-tags installed (as mentioned in Ranieri's answer above), here's an example shell command to get the "name" of the current instance, assuming you have a "Name=Foo" tag on it.

    Assumes EC2_PRIVATE_KEY and EC2_CERT environment variables are set.

    ec2-describe-tags \
      --filter "resource-type=instance" \
      --filter "resource-id=$(ec2-metadata -i | cut -d ' ' -f2)" \
      --filter "key=Name" | cut -f5
    

    This returns Foo.

提交回复
热议问题