Skip to content

Instantly share code, notes, and snippets.

@vatshat
Last active February 16, 2022 19:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vatshat/7d462ac0f07b083f6e88748bd6b4c36a to your computer and use it in GitHub Desktop.
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
# 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