Reading tags from CLI or bash in ec2 instances

# To read the Name tag:

TAG_NAME=”Name”

INSTANCE_ID=”`./ec2-metadata -i | cut -f 2 -d ” ” `”

REGION=”`././ec2-metadata -z | cut -f 2 -d ” ” `”

REGION=${REGION%?}

TAG_VALUE=”`aws ec2 describe-tags –filters “Name=resource-id,Values=$INSTANCE_ID” “Name=key,Values=$TAG_NAME” –region $REGION –output=text | cut -f5`”

Gives:

set |grep TAG_VALUE

TAG_VALUE=’brad test box’

# To see ALL tags to see WTF: (use region from above)

aws ec2 describe-tags –region us-east-1 –output=text

# To read the Created-by custom tag:

TAG_NAME=”created-by”

INSTANCE_ID=”`./ec2-metadata -i | cut -f 2 -d ” ” `”

REGION=”`././ec2-metadata -z | cut -f 2 -d ” ” `”

REGION=${REGION%?}

TAG_VALUE=”`aws ec2 describe-tags –filters “Name=resource-id,Values=$INSTANCE_ID” “Name=key,Values=$TAG_NAME” –region $REGION –output=text | cut -f5`”

set |grep TAG_VALUE

TAG_VALUE=test-kitchen

# To read the nifi-node-num custom tag:

TAG_NAME=”nifi-node-num”

echo “TAG_NAME is: ” ${TAG_NAME}

INSTANCE_ID=”`./ec2-metadata -i | cut -f 2 -d ” ” `”

echo “INSTANCE_ID is: ” ${INSTANCE_ID}

REGION=”`././ec2-metadata -z | cut -f 2 -d ” ” `”

REGION=${REGION%?}

echo “REGION is: ” ${REGION}

TAG_VALUE=”`aws ec2 describe-tags –filters “Name=resource-id,Values=$INSTANCE_ID” “Name=key,Values=$TAG_NAME” –region $REGION –output=text | cut -f5`”

echo “TAG_VALUE is: ” ${TAG_VALUE}

# You need the EC2 Metadata binary for the above to work

wget http://s3.amazonaws.com/ec2metadata/ec2-metadata

chmod u+x ec2-metadata

./ec2-metadata –help

You can also do this IF you have credentials:

aws ec2 describe-instances –region us-east-1 –instance-id i-09301dcede4431741|grep -A 200 Tag

                    “Tags”: [

                        {

                            “Value”: “False”,

                            “Key”: “data_sensitive”

                        },

                        {

                            “Value”: “10/31/2017”,

                            “Key”: “valid_thru”

                        },

                        {

                            “Value”: “nifi-al”,

                            “Key”: “Application”

                        },

                        {

                            “Value”: “Aplha”,

                            “Key”: “Cluster”

                        },

                        {

                            “Value”: “False”,

                            “Key”: “Docker”

                        },

                        {

                            “Value”: “TBD”,

                            “Key”: “BAPP_ID”

                        },

                        {

                            “Value”: “nifi-al-latest”,

                            “Key”: “Name”

                        },

                        {

                            “Value”: “WDPRTechnologyIAParksDataPlatform@disney.com“,

                            “Key”: “Owner”

                        },

                        {

                            “Value”: “1”,

                            “Key”: “nifi-node-num”

                        },

                        {

                            “Value”: “nifi-al-latest-asg”,

                            “Key”: “aws:autoscaling:groupName”

                        },

                        {

                            “Value”: “Non-Prod Sandbox”,

                            “Key”: “Environment”

                        },

                        {

                            “Value”: “yes”,

                            “Key”: “tag_compliance”

                        }

                    ],

                    “AmiLaunchIndex”: 2

                }

            ],

            “ReservationId”: “r-0a402050d68688b53”,

            “RequesterId”: “226008221399”,

            “Groups”: [],

            “OwnerId”: “876496569223”

        }

    ]

}

ALL ec2-metadata options:

Usage: ec2-metadata <option>

Options:

–all                     Show all metadata information for this host (also default).

-a/–ami-id               The AMI ID used to launch this instance

-l/–ami-launch-index     The index of this instance in the reservation (per AMI).

-m/–ami-manifest-path    The manifest path of the AMI with which the instance was launched.

-n/–ancestor-ami-ids     The AMI IDs of any instances that were rebundled to create this AMI.

-b/–block-device-mapping Defines native device names to use when exposing virtual devices.

-i/–instance-id          The ID of this instance

-t/–instance-type        The type of instance to launch. For more information, see Instance Types.

-h/–local-hostname       The local hostname of the instance.

-o/–local-ipv4           Public IP address if launched with direct addressing; private IP address if launched with public addressing.

-k/–kernel-id            The ID of the kernel launched with this instance, if applicable.

-z/–availability-zone    The availability zone in which the instance launched. Same as placement

-c/–product-codes        Product codes associated with this instance.

-p/–public-hostname      The public hostname of the instance.

-v/–public-ipv4          NATted public IP Address

-u/–public-keys          Public keys. Only available if supplied at instance launch time

-r/–ramdisk-id           The ID of the RAM disk launched with this instance, if applicable.

-e/–reservation-id       ID of the reservation.

-s/–security-groups      Names of the security groups the instance is launched in. Only available if supplied at instance launch time

-d/–user-data            User-supplied data.Only available if supplied at instance launch time.

Leave a Reply

Your email address will not be published. Required fields are marked *