From 512c72c2ba3f8ca73c90a386de0a0527d02d119d Mon Sep 17 00:00:00 2001 From: Ryuta Otaki Date: Mon, 10 Jan 2022 19:36:28 +0900 Subject: [PATCH] add ec2-metadata tags support. --- ec2-metadata | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/ec2-metadata b/ec2-metadata index 218306c..30249d1 100755 --- a/ec2-metadata +++ b/ec2-metadata @@ -37,7 +37,8 @@ Options: -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." +-d/--user-data User-supplied data.Only available if supplied at instance launch time. +-g/--tags Tags assigned to this instance." } METADATA_BASEURL="http://169.254.169.254" @@ -108,6 +109,20 @@ function print_public-keys() fi } +#print tags +function print_tags() +{ + echo 'tags: ' + x=$(get_meta meta-data/tags/instance/) + if [ -n "${x}" ]; then + for i in $x; do + echo -e '\t' $i: $(get_meta meta-data/tags/instance/$i) + done + else + echo not available + fi +} + function print_all() { print_normal_metric ami-id meta-data/ami-id @@ -129,6 +144,7 @@ function print_all() print_normal_metric reservation-id /meta-data/reservation-id print_normal_metric security-groups meta-data/security-groups print_normal_metric user-data user-data + print_tags } #check if run inside an EC2 instance @@ -180,6 +196,8 @@ while [ "$1" != "" ]; do ;; -d | --user-data ) print_normal_metric user-data user-data ;; + -g | --tags ) print_tags + ;; -h | --help ) print_help exit ;;