Last active
July 15, 2022 01:32
-
-
Save BlueDoge/43a0274c0e99aa420cf96a35ca145678 to your computer and use it in GitHub Desktop.
Get all AWS EC2 Instances that lack a cloudwatch metric or composite alarm
This file contains 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
#Originally created by Liz Clements in 2022 | |
#License: MIT | |
$AWSRegion = 'us-west-2' | |
$AWSProfileLocation = ("$env:USERPROFILE\.aws\credentials") | |
$AWSProfileName = 'default' | |
#Grab all alarms, that have instance ids directly associated with them | |
$AlarmData = (Get-CWAlarm ` | |
-ProfileLocation $AWSProfileLocation -ProfileName $AWSProfileName -Region $AWSRegion) ` | |
| Where-Object { $_.Dimensions.Name -eq 'InstanceId' } | |
#Parse the InstanceIds and store the type of alarms that are associated to each | |
$InstanceAlarmMap = @{}; | |
$InstanceAlarmCountMap = @{}; | |
try { | |
ForEach($Alarm in $AlarmData) | |
{ | |
$Alarm_InstanceId = $Alarm.Dimensions ` | |
| Where-Object { $_.Name -eq "InstanceId" } ` | |
| Select-Object Value -ExpandProperty Value | |
#Store the Alarm type | |
$InstanceAlarmMap[$Alarm_InstanceId] = $true # update this to include composites | |
#Store the count | |
if($InstanceAlarmCountMap.ContainsKey($Alarm_InstanceId) -eq $true) | |
{ | |
$InstanceAlarmCountMap[$Alarm_InstanceId] = $InstanceAlarmCountMap[$Alarm_InstanceId] + 1 | |
} | |
else | |
{ | |
$InstanceAlarmCountMap[$Alarm_InstanceId] = 1 | |
} | |
} | |
} | |
catch { | |
Write-Error "Grabbing alarm data failed!!" | |
Exit 1 | |
} | |
$ListOfAllInstances = ` | |
(Get-EC2Instance -Region $AWSRegion -ProfileLocation $AWSProfileLocation -ProfileName $AWSProfileName).Instances ` | |
$ListOfInstances_WithAlarmData = $ListOfAllInstances ` | |
| Select-Object ` | |
@{ ` | |
Name="ServerAlias"; ` | |
Expression= { ` | |
$_.Tags | Where-Object Key -eq "Name" | Select-Object Value -ExpandProperty Value ` | |
} ` | |
} ` | |
, @{ ` | |
Name="State"; ` | |
Expression={ $_.State.Name } ` | |
} ` | |
, @{ ` | |
Name="AlarmType"; ` | |
Expression= { ` | |
if($InstanceAlarmMap[$_.InstanceId]) { "Metric" } ` | |
else { "None" } ` | |
} ` | |
} ` | |
, @{ ` | |
Name="AlarmCount"; ` | |
Expression= { ` | |
if($InstanceAlarmCountMap.ContainsKey($_.InstanceId) -eq $false) { "0" } ` | |
else { $InstanceAlarmCountMap[$_.InstanceId] } ` | |
} ` | |
}, InstanceId, PrivateIpAddress | |
#$ListOfInstances_WithAlarmData | Sort-Object -Property AlarmType, State, ServerAlias | Format-Table | |
$Gridview_Selection = $ListOfInstances_WithAlarmData | Sort-Object -Property AlarmType, State, ServerAlias | Out-Gridview -PassThru | |
#Output the selection | |
$Gridview_Selection | Format-Table | |
#Copy to clipboard all of the private ip addresses | |
Set-Clipboard -Value $Gridview_Selection.PrivateIpAddress |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment