Skip to content

AWS hygiene scripts

ErinWeisbart edited this page Oct 26, 2020 · 1 revision

Too many old alarms lying around?

Python:

import boto3
import time
client = boto3.client('cloudwatch')
NextToken='1'
while NextToken != '':
  alarms = client.describe_alarms()['MetricAlarms']
  for eachalarm in alarms:
    if eachalarm['StateValue'] == 'INSUFFICIENT_DATA':
        client.delete_alarms(AlarmNames = [eachalarm['AlarmName']])
        time.sleep(1) #avoid throttling
  try:
    NextToken = client.describe_alarms()['NextToken']
  except KeyError:
    NextToken = ''

Too many old empty log groups lying around?

Bash:

aws logs describe-log-groups| in2csv -f json --key logGroups > logs.csv

R:

(requires dplyr and readr)

library(dplyr)
library(readr)
read_csv(
  "logs.csv",
  col_types = cols_only(
    storedBytes = col_integer(),
    creationTime = col_double(),
    logGroupName = col_character()
  )
) %>%
  mutate(creationTime =
           as.POSIXct(creationTime / 1000,
                      origin = "1970-01-01")) %>%
  filter(storedBytes == 0) %>%
  select(logGroupName) %>%
  write_tsv("logs_clear.txt", col_names = F)

Bash:

parallel aws logs delete-log-group --log-group-name {1} :::: logs_clear.txt