Last active
February 16, 2022 19:56
-
-
Save vatshat/7d462ac0f07b083f6e88748bd6b4c36a to your computer and use it in GitHub Desktop.
Script to update CloudWatch Dashboard body Widgets without label with EC2 name tag
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Gets instances without labels in the widgets | |
labelLessInstance=$( cat temp.json | | |
jq ' | |
. | | |
( | |
[ | |
.widgets[] | | |
select( | |
(.type == "metric") and (.properties.metrics[0][0] == "AWS/EC2") | |
) | | |
[ | |
( .properties.metrics[1:][] | select(.[2].label == null) | .[1] ) | |
] as $instanceIds | | |
{ | |
(.properties.metrics[0][1]) : ($instanceIds) | |
} | |
] | |
) as $instances | | |
( | |
to_entries[0].value | | |
map( | |
(.type == "metric") and (.properties.metrics[0][0] == "AWS/EC2" ) | |
) | |
| indices(true) | |
) as $indices | | |
reduce range($instances|length) as $i | |
( {}; . + { ( $indices[ $i ] | tostring ) : $instances[ $i ] }) | |
') | |
# Gets tags of missing labels with tags | |
instanceIds=$( echo $labelLessInstance | jq --raw-output '[ .[][][] ] | unique | join(" ")' ) | |
tags=$(aws ec2 describe-instances --region us-east-1 --instance-ids $instanceIds | | |
jq '[ | |
.Reservations[].Instances[] | | |
{ | |
(.InstanceId): | |
(.Tags[] | select( (.Key == "Name") and ( .Value != "" ) ) | .Value) | |
} | |
] | reduce .[] as $item ({}; . + $item) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment