Query EC2 tags from within instance

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

    For those crazy enough to use Fish shell on EC2, here's a handy snippet for your /home/ec2-user/.config/fish/config.fish. The hostdata command now will list all your tags as well as the public IP and hostname.

    set -x INSTANCE_ID (wget -qO- http://instance-data/latest/meta-data/instance-id)
    set -x REGION (wget -qO- http://instance-data/latest/meta-data/placement/availability-zone | sed 's/.$//')
    
    function hostdata
        aws ec2 describe-tags --region $REGION --filter "Name=resource-id,Values=$INSTANCE_ID" --output=text | sed -r 's/TAGS\t(.*)\t.*\t.*\t(.*)/\1="\2"/'
        ec2-metadata | grep public-hostname
        ec2-metadata | grep public-ipv4
    end
    

提交回复
热议问题