-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtagvolumes.sh
More file actions
30 lines (26 loc) · 1.29 KB
/
tagvolumes.sh
File metadata and controls
30 lines (26 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/bin/bash -e
#Variables
regionid=us-west-2
profile=default
tag_name=LT-Group
# DO NOT EDIT ANY THING BELOW WITHOUT DEVOPS APPROVAL
tagvalue=""
instid=""
echo "=> Fetching All EC2 Instances"
instarr=( $(aws ec2 describe-instances --profile ${profile} --region ${regionid} --query 'Reservations[*].Instances[*].{ID:InstanceId}' --output text) )
echo "=> Loop through all the EC2 Instances Found"
for instid in "${instarr[@]}"
do
printf "\n"
echo "=> Querying instance-id: " ${instid} "for tag value"
tagvalue=`aws ec2 describe-tags --profile ${profile} --region ${regionid} --filters "Name=resource-id,Values=${instid}" "Name=key,Values=${tag_name}" --query Tags[].{Value:Value} --output text`
echo "=> Fetching All EC2 Instances Attached to the Instance ${instid}"
volidarr=( $(aws ec2 describe-volumes --profile ${profile} --region ${regionid} --filters Name=attachment.instance-id,Values=${instid} --query 'Volumes[*].Attachments[*].{volid:VolumeId}' --output text) )
echo "=> Loop through all the EC2 Instances Found"
for volid in ${volidarr[@]}
do
echo "Adding tag ${tag_name} with value ${tagvalue} to volume-id: " ${volid}
aws ec2 create-tags --profile ${profile} --region ${regionid} --resources ${volid} --tags Key=${tag_name},Value="${tagvalue}"
echo "Done!"
done
done