简体   繁体   中英

Select tag of Autoscaling Group inside of instance using AWS CLI

When I inside of instance that have Autoscaling Group. I'm trying to select some specific tag of Autoscaling Group. i know that I can use AWS CLI command "aws autoscaling describe-tags". So I wrote some .sh file. Than contain:

#!/bin/sh
#Instance ID
InstanceID=`/usr/bin/curl -s http://169.254.169.254/latest/meta-data/instance-id`

#Allocation ID from Autoscaling tags
Autoscaling=`aws ec2 describe-tags --filters "Name=resource-id,Values=$InstanceID" "Name=key,Values=aws:autoscaling:groupName"`
AllocationID=`aws autoscaling describe-tags --filter "Name=key,Values=$Autoscaling"| select(["Value"] | contains("eipalloc"))`

#Assigning VPC Elastic IP to Instance via Allocation ID
aws ec2 associate-address --instance-id $InstanceID --allocation-id $AllocationID

It doesn't work only because I can't select value in this way. Who can tell me how I can properly select some specific tag?

Thanks!

Looks terrible, but it works:

#!/bin/sh
#Instance ID
InstanceID=`/usr/bin/curl -s http://169.254.169.254/latest/meta-data/instance-id`

#Elastic IP captured through the AllocationID Autoscaling tag
ScalingGroup=`aws ec2 describe-tags --filters "Name=resource-id,Values=$InstanceID" "Name=key,Values=aws:autoscaling:groupName" | jq '.Tags[] | .Value'`
AllocationID=`aws autoscaling describe-tags --filters "Name=auto-scaling-group,Values=$ScalingGroup" | jq '.Tags[] | select(.["Key"] | contains("AllocationID")) | .Value'`
AllocationID=`echo $AllocationID | tr -d '"'`

#Assigning Elastic IP to Instance
aws ec2 associate-address --instance-id $InstanceID --allocation-id $AllocationID

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM