Query EC2 tags from within instance

前端 未结 14 1745
暗喜
暗喜 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:45

    A variation on some of the answers above but this is how I got the value of a specific tag from the user-data script on an instance

    REGION=$(curl http://instance-data/latest/meta-data/placement/availability-zone | sed 's/.$//')
    
    INSTANCE_ID=$(curl -s http://instance-data/latest/meta-data/instance-id)
    
    TAG_VALUE=$(aws ec2 describe-tags --region $REGION --filters "Name=resource-id,Values=$INSTANCE_ID" "Name=key,Values=''" | jq -r '.Tags[].Value')
    

提交回复
热议问题