bash - 使用 AWS CLI 选择实例内的 Autoscaling 组标签

标签 bash shell amazon-web-services aws-cli

当我在具有自动缩放组的实例内部时。我正在尝试选择自动缩放组的某些特定标签。我知道我可以使用 AWS CLI 命令“aws autoscaling describe-tags”。 所以我写了一些.sh 文件。比包含:

#!/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

它不起作用只是因为我无法以这种方式选择值。谁能告诉我如何正确选择某些特定标签?

谢谢!

最佳答案

看起来很糟糕,但它有效:

#!/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

关于bash - 使用 AWS CLI 选择实例内的 Autoscaling 组标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35197978/

相关文章:

regex - Sed 命令找不到我的正则表达式

amazon-web-services - EKS 无法使用 Kubectl 对 Kubernetes 进行身份验证 - "User: is not authorized to perform: sts:AssumeRole"

ruby-on-rails - 将 docker 与 RDS (Prod) 一起使用,但对本地数据库设置 (dev) 感到困惑

linux - 将 netstat 输出附加到文件的 Bash 脚本?

ios - 如何在 iOS 中连接 amazon rds mysql

bash - .bash_profile 中的错误无法使用终端

linux - bash - 如何匹配要在循环中使用的文件名

bash - X 分钟后脚本重复自身

linux - 如何使用 shell 脚本验证用户密码?

shell - 使用 ansible 打印文件的内容