-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws-unused-resources.sh
51 lines (40 loc) · 1.83 KB
/
aws-unused-resources.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/bin/bash
# Loop through all regions
for region in $(aws ec2 describe-regions --output text | awk '{print $NF}'); do
echo "Checking unused resources in region: $region"
# Get all EC2 instances
instances=$(aws ec2 describe-instances --region $region --output text | awk '{print $NF}')
# Loop through all EC2 instances
for instance in $instances; do
# Get the tags for each instance
tags=$(aws ec2 describe-tags --filters "Name=resource-id,Values=$instance" --region $region --output text)
# Check if the instance is tagged
if [ -z "$tags" ]; then
echo "Unused EC2 instance: $instance"
fi
done
# Get all EBS volumes
volumes=$(aws ec2 describe-volumes --region $region --output text | awk '{print $NF}')
# Loop through all EBS volumes
for volume in $volumes; do
# Get the attached instance for each volume
attached_instance=$(aws ec2 describe-volumes --volume-ids $volume --region $region --output text | awk '$1 == "ATTACHMENTS" {print $3}')
# Check if the volume is unattached
if [ -z "$attached_instance" ]; then
echo "Unused EBS volume: $volume"
fi
done
# Get all snapshots
snapshots=$(aws ec2 describe-snapshots --owner-ids self --region $region --output text | awk '{print $NF}')
# Loop through all snapshots
for snapshot in $snapshots; do
# Get the volume for each snapshot
volume=$(aws ec2 describe-snapshots --snapshot-ids $snapshot --region $region --output text | awk '$1 == "VOLUMEID" {print $NF}')
# Check if the volume exists
volume_exists=$(aws ec2 describe-volumes --volume-ids $volume --region $region)
# Check if the volume is deleted
if [ -z "$volume_exists" ]; then
echo "Unused snapshot: $snapshot"
fi
done
done