Created
September 10, 2021 01:23
-
-
Save rafaelliu/715ac2ea14d4d4c5ab652a35437bd63d to your computer and use it in GitHub Desktop.
Network cleanup
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
# ###### Delete Stack Sets and Instances | |
# CALL_AS="DELEGATED_ADMIN" | |
# RETAIN_PARAM="--retain-stacks" # "--no-retain-stacks" # | |
# BLACKLIST_PATTERN=("RecreateOrganizationControlRole-Isengard" ) | |
# SETS=$(aws cloudformation list-stack-sets --call-as $CALL_AS | jq -r '.Summaries[] | select(.Status == "ACTIVE") | .StackSetName' ) | |
# for S in $SETS; do | |
# echo "" | |
# echo "StackSet: $S" | |
# if [[ " ${BLACKLIST_PATTERN[*]} " =~ " ${S} " ]]; then | |
# echo "- Blacklisted. Skipping" | |
# continue | |
# fi | |
# JSON=$(aws cloudformation list-stack-instances --call-as $CALL_AS --stack-set-name $S ) | |
# REGIONS=$(echo $JSON | jq -r ".Summaries[].Region" | sort | uniq | paste -sd " " -) | |
# if [[ -z "$REGIONS" ]]; then | |
# aws cloudformation delete-stack-set --call-as $CALL_AS --stack-set-name $S | |
# echo "- Stack Set Deleted" | |
# continue | |
# fi | |
# OU_IDS=$(echo $JSON | jq -r ".Summaries[].OrganizationalUnitId" | sort | uniq | paste -sd "," -) | |
# if [[ ! -z "$OU_IDS" ]]; then | |
# aws cloudformation delete-stack-instances --call-as $CALL_AS --stack-set-name $S --regions $REGIONS --deployment-targets OrganizationalUnitIds=$OU_IDS $RETAIN_PARAM | |
# echo "- Cleaning up instances using Service Role. Wait until instances are deleted and run command again to delete Stack Set" | |
# continue | |
# fi | |
# ACCOUNTS=$(echo $JSON | jq -r ".Summaries[].Account" | sort | uniq | paste -sd "," -) | |
# if [[ ! -z "$ACCOUNTS" ]]; then | |
# aws cloudformation delete-stack-instances --call-as $CALL_AS --stack-set-name $S --regions $REGIONS --deployment-targets Accounts=$ACCOUNTS $RETAIN_PARAM | |
# echo "- Cleaning up using Self-managed Role. Wait until instances are deleted and run command again to delete Stack Set" | |
# continue | |
# fi | |
# echo "Unexpected error" | |
# done | |
###### Delete TGW Attachments | |
ATTACHMENTS=$(aws ec2 describe-transit-gateway-attachments) | |
PEERING_ATTCH=$(echo $ATTACHMENTS | jq -r '.TransitGatewayAttachments[] | select(.State == "available" and .ResourceType == "peering") | .TransitGatewayAttachmentId') | |
for A in $PEERING_ATTCH; do | |
echo "" | |
echo "Peering Attachment: $A" | |
aws ec2 delete-transit-gateway-peering-attachment --transit-gateway-attachment-id $A | |
echo "- Cleaning. Wait until instances are deleted and run command again to delete the TGW" | |
done | |
VPC_ATTCH=$(echo $ATTACHMENTS | jq -r '.TransitGatewayAttachments[] | select(.State == "available" and .ResourceType == "vpc") | .TransitGatewayAttachmentId') | |
for A in $VPC_ATTCH; do | |
echo "" | |
echo "Vpc Attachment: $A" | |
aws ec2 delete-transit-gateway-vpc-attachment --transit-gateway-attachment-id $A | |
echo "- Cleaning. Wait until instances are deleted and run command again to delete the TGW" | |
done | |
###### Delete TGWs | |
TGWS=$(aws ec2 describe-transit-gateways | jq -r '.TransitGateways[] | select(.State == "available") | .TransitGatewayId' ) | |
for T in $TGWS; do | |
echo "" | |
echo "TGW: $T" | |
aws ec2 delete-transit-gateway --transit-gateway-id $T | |
echo "- Deleted" | |
done | |
###### Delete VPCs | |
VPCS=$(aws ec2 describe-vpcs | jq -r '.Vpcs[].VpcId') | |
# This is bad. Don't do it | |
wget https://raw.githubusercontent.com/lianghong/delete_vpc/master/delete_vpc.sh -O /tmp/delete_vpc.sh | |
chmod +x /tmp/delete_vpc.sh | |
for V in $VPCS; do | |
echo "" | |
echo "VPC: $V" | |
echo "Y" | /tmp/delete_vpc.sh $AWS_REGION $V | |
echo "- Deleted" | |
done | |
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
$ AWS_REGION=us-east-1 AWS_PROFILE=account-profile ./cleanup.sh |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment