aws通过ip获取实例id并自动挂盘
由于使用aws cli ,请先进行aws configure配置
#!/bin/bash
declare -A IP_VOL
#定义盘的id和ip的对应关系
IP_VOL=([192.168.1.14]="vol-033f2ef4d2d739a04" [192.168.1.15]="vol-033f2af4e2d739a04" )
#通过ip获取实例id
function get_instanceid() {
aws ec2 describe-instances --filters "Name=network-interface.addresses.private-ip-address,Values=$1" --query 'Reservations[*].Instances[*].[InstanceId]' --output text
}
function main() {
for key in ${
!IP_VOL[*]}; do
vol_id=${
IP_VOL[$key]}
#echo $key ${IP_VOL[$key]}
ip=$key
instance=$(get_instanceid $key)
echo "aws ec2 attach-volume --volume-id $vol_id --instance-id $instance --device /dev/sdf"
done
}