Created
June 1, 2016 19:28
-
-
Save dforocha/d588c3443c0fcc4d378237a09a4568a3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/bin/bash | |
# | |
# SCRIPT: | |
# clean_aws.sh | |
# | |
# FUNCTION: | |
# Delete all configuration and instances from aws like ec2,rds and cloudformation | |
# | |
# AUTHOR: | |
# Diego Rocha | |
# [email protected] | |
# | |
# DATE: | |
# 2016-06-01 | |
#---------------------------------------------------------------------------------------- | |
# How to use: | |
# 1. You might install the aws-cli | |
# Centos/Red Hat: | |
# yum install aws-cli | |
# Debian/Ubuntu: | |
# apt-get install aws-cli | |
# 2. type: | |
# aws configure | |
# You should inform your access key and secret key and also the region | |
# 3. call the script | |
# ./clean_aws.sh | |
# | |
# | |
# This function will wait 20 seconds and show the percent from the wait from 10% till 100% | |
function waiting () { | |
for c in `seq 1 10` | |
do | |
let c*=10 | |
echo -ne "$c%\033[0K\r" | |
sleep 2 | |
done | |
} | |
# This function will check if the return of a command was empty, case the return is empty meaning that the object already was deleted from amazon | |
function checkResult() | |
{ | |
result="$1" | |
if [ "z$result" == "z" ]; then | |
return 0 | |
else | |
echo "[Result: $result]" | |
waiting | |
return 1 | |
fi | |
} | |
echo "Excluding RDS instance" | |
echo "checking RDS instance name:" | |
for RDS_name in `aws rds describe-db-instances --query 'DBInstances[*].DBInstanceIdentifier' --output text` ; do | |
echo "Deleting: $RDS_name" | |
aws rds delete-db-instance --db-instance-identifier "$RDS_name" --skip-final-snapshot | |
waiting | |
done | |
echo "Excluding cloudformation: " | |
for formation in `aws cloudformation describe-stacks --query 'Stacks[*].StackId' --output text` | |
do | |
aws cloudformation delete-stack --stack-name $formation | |
waiting | |
done | |
# We need make sure that the RDS instance was deleted before try delete the db-parameter-group | |
checkDone=1 | |
while [ $checkDone -eq 1 ]; do | |
echo "Checking if the RDS instance still exist..." | |
result=`/usr/local/bin/aws rds describe-db-instances --query 'DBInstances[*].DBInstanceIdentifier' --output text` | |
checkResult $result | |
checkDone=$? | |
done | |
for RDS_db_param in `aws rds describe-db-parameter-groups --output text | grep -v default | awk '{print $3}'` ; do | |
echo "Deleting RDS db-parameter-group: $RDS_db_param" | |
aws rds delete-db-parameter-group --db-parameter-group-name $RDS_db_param | |
done | |
#echo "é necessário deletar o snapshot antes do option-groups, já que o option group está relacionado ao snapshot" | |
echo "Deleting RDS Snapshots" | |
for snapshot in `aws rds describe-db-snapshots --query 'DBSnapshots[*].DBSnapshotIdentifier' --output text` ; do echo "deleting : $snapshot" ; aws rds delete-db-snapshot --db-snapshot-identifier $snapshot ; done | |
#aws rds describe-db-snapshots --query 'DBSnapshots[*].DBSnapshotIdentifier' --output text | |
echo | |
echo "Deleting RDS option-groups:" | |
for optgroup in `aws rds describe-option-groups --query 'OptionGroupsList[*].OptionGroupName' --output text` ; do if [[ $optgroup != defaul* ]] ; then echo "Deleting: $optgroup" ; aws rds delete-option-group --option-group-name $optgroup ; fi ; done | |
echo | |
echo "Deleting RDS subnet-group" | |
for subnet in `aws rds describe-db-subnet-groups --query "DBSubnetGroups[*].DBSubnetGroupName" --output text` ; do echo "Deleting : $subnet" ; aws rds delete-db-subnet-group --db-subnet-group-name $subnet; done | |
#echo | |
#export VPC_name=`aws ec2 describe-vpcs --query 'Vpcs[*].VpcId' --output text` | |
#echo "Deleting VPC: $VPC_name" | |
#aws ec2 delete-vpc --vpc-id $VPC_name |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment