Skip to content

Instantly share code, notes, and snippets.

@colinhoglund
Last active May 6, 2021 16:11
Show Gist options
  • Save colinhoglund/d1400ca5c33ac4f57c057b8b3bfd33de to your computer and use it in GitHub Desktop.
Save colinhoglund/d1400ca5c33ac4f57c057b8b3bfd33de to your computer and use it in GitHub Desktop.
aws_cleanup
# EC2 available volumes
aws ec2 describe-volumes \
--filter Name=status,Values=available \
--query "Volumes[].[VolumeId,Size,CreateTime,Tags[?Key=='Name'].Value|[0]]" \
--output text
# EC2 stopped instances
aws ec2 describe-instances \
--filter Name=instance-state-name,Values=stopped \
--query "Reservations[].Instances[].[InstanceId,InstanceType,LaunchTime,Tags[?Key=='Name'].Value|[0]]" \
--output text
# AMIs
aws ec2 describe-images \
--owners $(aws sts get-caller-identity --query Account --output text) \
--query "Images[].[ImageId,CreationDate,Name]" \
--output text
# EC2 snapshots
aws ec2 describe-snapshots \
--owner-ids $(aws sts get-caller-identity --query Account --output text) \
--query "Snapshots[].[SnapshotId,VolumeSize,StartTime,Tags[?Key=='Name'].Value|[0]]" \
--output text
# EIP unassociated
aws ec2 describe-addresses --query "Addresses[?AssociationId==null]"
# RDS manual snapshots
aws rds describe-db-snapshots \
--query "DBSnapshots[?SnapshotType!='automated'].[DBSnapshotIdentifier,AllocatedStorage,SnapshotCreateTime]" \
--output text
# RDS instances no connections for a month
for i in $(aws rds describe-db-instances --query DBInstances[].DBInstanceIdentifier --output text); do
out=$(aws cloudwatch get-metric-statistics \
--metric-name DatabaseConnections \
--start-time $(/bin/date -v-1m "+%Y-%m-%dT%H:%M:%S") \
--end-time $(/bin/date "+%Y-%m-%dT%H:%M:%S") \
--period 1860 \
--namespace AWS/RDS \
--statistics Average \
--dimensions Name=DBInstanceIdentifier,Value=${i} \
--query 'Datapoints[?Average > `0`]')
if [ "$out" == '[]' ]; then
echo $i
fi
done
# ELBs with no requests in a month
for i in $(aws elb describe-load-balancers --query LoadBalancerDescriptions[].LoadBalancerName --output text); do
out=$(aws cloudwatch get-metric-statistics \
--metric-name RequestCount \
--namespace AWS/ELB \
--start-time $(/bin/date -v-1m "+%Y-%m-%dT%H:%M:%S") \
--end-time $(/bin/date "+%Y-%m-%dT%H:%M:%S") \
--period 1860 \
--statistics Sum \
--dimensions Name=LoadBalancerName,Value=${i} \
--query 'Datapoints[?Sum > `0`]')
if [ "$out" == '[]' ]; then
echo $i
fi
done
# S3 bucket sizes
get_bucket_size() {
size=$(aws cloudwatch get-metric-statistics \
--namespace AWS/S3 \
--start-time $(/bin/date -v-1d '+%Y-%m-%dT%H:%M:%S') \
--end-time $(/bin/date '+%Y-%m-%dT%H:%M:%S') \
--period 300 \
--statistics Average \
--metric-name BucketSizeBytes \
--dimensions Name=BucketName,Value=$1 \
Name=StorageType,Value=$2 \
--query 'Datapoints[].Average|[0]')
size_gb=$(echo $size / 1024 / 1024 / 1024 | bc)
if [ "$size" != 'null' ] && [ "$size_gb" != 0 ]; then
cost=$(echo "$size_gb * 0.023" | bc)
printf "$1\t$2\t$size_gb\t$cost\n"
fi
}
for bucket in $(aws s3api list-buckets --query Buckets[].Name --output text); do
get_bucket_size $bucket StandardStorage
get_bucket_size $bucket ReducedRedundancyStorage
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment