From 7b4cc3b61a8b69fcb5446cc42c890bd11fe05a00 Mon Sep 17 00:00:00 2001 From: Gustavo Aguiar Date: Mon, 28 May 2018 12:53:00 -0400 Subject: [PATCH 1/2] Making account_id not required (account_aliases still required). --- clean.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/clean.py b/clean.py index 589d36f..aff9547 100755 --- a/clean.py +++ b/clean.py @@ -55,9 +55,10 @@ def _simple_delete(self, describe_function, delete_function, preserve_key, list_ self._delete_generic_resource(deletables, list_key, delete_function, item_key) def run_safety_checks(self, sts, iam, iam_resource): - # AWS Account ID in config.yml must match the account we are accessing using an API key + # AWS Account ID in config.yml must match the account we are accessing using an API key (if null then use only account_aliases) account_id = sts.get_caller_identity().get("Account") - assert account_id == self.config.get("assertions").get("account_id"), "Unexpected AWS Account ID, check configuration!" + if self.config.get("assertions").get("account_id"): + assert account_id == self.config.get("assertions").get("account_id"), "Unexpected AWS Account ID, check configuration!" # AWS Account alias in config.yml must match the account alias account_aliases = iam.list_account_aliases().get("AccountAliases") @@ -174,3 +175,5 @@ def get_boto_session(profile_name): cleaner.delete_securitygroups(ec2) cleaner.delete_key_pairs(ec2) cleaner.delete_buckets(s3, s3_resource) + + From a93782794d245a4d22e5dd860758bf26b300b648 Mon Sep 17 00:00:00 2001 From: Gustavo Aguiar Date: Mon, 28 May 2018 13:44:03 -0400 Subject: [PATCH 2/2] Adding support to EC2 instances. --- README.md | 1 + clean.py | 31 ++++++++++++++++++++++++++++--- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index e059259..973a1a3 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ This utility tool will delete all resources from your AWS account. Whitelisted r - EC2 key pairs - EC2 AMI images - EC2 security groups +- EC2 instances - EBS snapshots - CloudWatch alarms - SNS topics diff --git a/clean.py b/clean.py index aff9547..248c491 100755 --- a/clean.py +++ b/clean.py @@ -55,10 +55,9 @@ def _simple_delete(self, describe_function, delete_function, preserve_key, list_ self._delete_generic_resource(deletables, list_key, delete_function, item_key) def run_safety_checks(self, sts, iam, iam_resource): - # AWS Account ID in config.yml must match the account we are accessing using an API key (if null then use only account_aliases) + # AWS Account ID in config.yml must match the account we are accessing using an API key account_id = sts.get_caller_identity().get("Account") - if self.config.get("assertions").get("account_id"): - assert account_id == self.config.get("assertions").get("account_id"), "Unexpected AWS Account ID, check configuration!" + assert account_id == self.config.get("assertions").get("account_id"), "Unexpected AWS Account ID, check configuration!" # AWS Account alias in config.yml must match the account alias account_aliases = iam.list_account_aliases().get("AccountAliases") @@ -88,6 +87,31 @@ def delete_cloudformation_stacks(self, cf): } self._simple_delete(cf.list_stacks, cf.delete_stack, "cloudformation", "StackSummaries", "StackName", args) + def delete_ec2_instances(self, ec2): + instances = ec2.describe_instances( + Filters=[{ + 'Name': 'instance-state-name', + 'Values': ['running', 'stopped', 'stopping'], + }] + ) + instance_list = [] + for reservation in instances["Reservations"]: + for instance in reservation["Instances"]: + instance_list.append(instance["InstanceId"]) + print(instance["InstanceId"] + ":") + print("\tInstanceType: " + instance["InstanceType"]) + if instance_list: + if self._ask("\nDelete EC2 Instances?", "no"): + response = ec2.terminate_instances( + InstanceIds= + instance_list + , + DryRun=False + ) + waiter = ec2.get_waiter('instance_terminated') + waiter.wait(InstanceIds=instance_list) + #print("Response was: ", response) + def delete_key_pairs(self, ec2): self._simple_delete(ec2.describe_key_pairs, ec2.delete_key_pair, "ec2_key_pairs", "KeyPairs", "KeyName") @@ -172,6 +196,7 @@ def get_boto_session(profile_name): cleaner.delete_sns_topics(sns) cleaner.delete_amis(sts, ec2) cleaner.delete_snapshots(sts, ec2) + cleaner.delete_ec2_instances(ec2) cleaner.delete_securitygroups(ec2) cleaner.delete_key_pairs(ec2) cleaner.delete_buckets(s3, s3_resource)