code-dumps/aws/cost-optimization.sh

30 lines
1.1 KiB
Bash
Raw Normal View History

2022-12-14 19:27:04 +08:00
#!/bin/bash
# Work in progress. This script does not work with organization. SP rouutines have not been written.
# Create sqlite database
rm -f co.db
sqlite3 co.db 'create table instances(type varchar(20) primary key,running int, ri int);'
2022-12-14 19:27:04 +08:00
# Count instance types in region
echo "Getting list of Ec2 instances..."
aws ec2 describe-instances --query 'Reservations[].Instances[].InstanceType' --filter Name=instance-state-name,Values=running --output text | tr '\t' '\n' | sort | uniq -c | while read count type; do
2022-12-14 19:38:29 +08:00
echo "insert into instances values(\"$type\", $count,0);"
done | sqlite3 co.db
2022-12-14 19:27:04 +08:00
# List RI in region
echo "Getting list of RI..."
aws ec2 describe-reserved-instances --query ReservedInstances[].[InstanceCount,InstanceType] --output text | while read count type; do
2022-12-14 19:38:29 +08:00
echo "update instances set ri = $count where type = \"$type\";"
done | sqlite3 co.db
2022-12-14 19:27:04 +08:00
# List ISP
echo "Getting ISP..."
aws savingsplans describe-savings-plans
# List CSP
echo "Getting CSP..."
aws savingsplans describe-savings-plans
# List table
sqlite3 -header -column co.db "select type, running, ri, running - ri as candidates from instances;"