Skip to content

Instantly share code, notes, and snippets.

@marknca
Last active March 27, 2023 15:29
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save marknca/6324493 to your computer and use it in GitHub Desktop.
Save marknca/6324493 to your computer and use it in GitHub Desktop.
Remove OpsWorks security groups from a given region
#! /usr/bin/env bash
# Remove OpsWorks security groups from the given region
# Available regions:
# ====================
# ap-northeast-1 => Asia Pacific (Tokyo) Region
# ap-southeast-1 => Asia Pacific (Singapore) Region
# ap-southeast-2 => Asia Pacific (Sydney) Region
# eu-west-1 => EU (Ireland) Region
# sa-east-1 => South America (Sao Paulo) Region
# us-east-1 => US East (Northern Virginia) Region
# us-west-1 => US West (Northern California) Region
# us-west-2 => US West (Oregon) Region
regions=( "ap-northeast-1" "ap-southeast-1" "ap-southeast-2" "eu-west-1" "sa-east-1" "us-east-1" "us-west-1" "us-west-2" )
cmd="aws ec2 delete-security-group --group-name "
if test -z "$1"
then
echo "Using the default region you've configured for the AWS cli"
else
echo "You've requested a region: $1"
requested_region=$1
valid_region=false
for region in "${regions[@]}"
do
if [ $region == $requested_region ]
then
valid_region=true
fi
done
if [ $valid_region == true ]
then
cmd="aws --region $requested_region ec2 delete-security-group --group-name "
else
echo "The region you requested doesn't exist. Using the default region you've configured for the AWS cli"
fi
fi
# Order to delete the OpsWorks groups
# 1 AWS-OpsWorks-Monitoring-Master-Server
# 2 AWS-OpsWorks-DB-Master-Server
# 3 AWS-OpsWorks-Blank-Server
# 4 AWS-OpsWorks-Memcached-Server
# 5 AWS-OpsWorks-Custom-Server
# 6 AWS-OpsWorks-nodejs-App-Server
# 7 AWS-OpsWorks-PHP-App-Server
# 8 AWS-OpsWorks-Rails-App-Server
# 9 AWS-OpsWorks-Default-Server
# 10 AWS-OpsWorks-Web-Server
# 11 AWS-OpsWorks-LB-Server
groups=( "AWS-OpsWorks-Monitoring-Master-Server" "AWS-OpsWorks-DB-Master-Server" "AWS-OpsWorks-Blank-Server" "AWS-OpsWorks-Memcached-Server" "AWS-OpsWorks-Custom-Server" "AWS-OpsWorks-nodejs-App-Server" "AWS-OpsWorks-PHP-App-Server" "AWS-OpsWorks-Rails-App-Server" "AWS-OpsWorks-Default-Server" "AWS-OpsWorks-Web-Server" "AWS-OpsWorks-LB-Server" )
for group in "${groups[@]}"
do
$cmd $group
if [ $? -gt 0 ]
then
echo "...error removing $group. Subsequent groups will probably fail as well"
else
echo "...removed $group"
fi
done
@marknca
Copy link
Author

marknca commented Aug 23, 2013

I thinks OpsWorks is fantastic but it does do one thing that drives me crazy. When you enable it, it creates a set of 11 EC2 security groups in all AWS regions. There are quite a few dependencies between the groups so you have to remove them in order...of course I can never remember the order.

No idea why it bugs me so much but mine is not to question just to solve with a tiny shell script.

Usage;

remove-opsworks-security-groups.sh
# => removes OpsWorks security groups from the default region for the AWS cli

remove-opsworks-security-groups.sh us-east-1
# => removes OpsWorks security groups from the specified region if it exists, if not it'll use the default region for the AWS cli

@richid
Copy link

richid commented Nov 12, 2015

👏 👏 👏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment