DEV Community

Cover image for Cloud-Reset: Reset a Cloud (AWS, GCP, Azure) account to default (delete all unneeded resources!)
An Rodriguez
An Rodriguez

Posted on • Updated on • Originally published at Medium

Cloud-Reset: Reset a Cloud (AWS, GCP, Azure) account to default (delete all unneeded resources!)

Ever wanted to reset (delete all the resources) from a cloud account (AWS/GCP/...)? Now you can, and its really easy.

Easily throw away (DELETE) unneeded resources.

Easily throw away (DELETE) unneeded resources.

Freedom

We want to give everyone the freedom to explore all that AWS has to offer. So we decided to create a 'sandbox' account where everybody could create any resource they want.

Freedom has its cost

Of course, resources cost money. And as good as we are in giving everyone the freedom to explore, we didn't want to spend more money than necessary by having lingering zombie resources that nobody knew even existed.

So, we also wanted to delete all resources periodically.

This was more easily said than done.

Solution

I tried finding some GitHub project that already did this, or at least that we could collaborate.

Although we found a couple (listed below in case you are interested), these we’re written in Go and we’re too complicated to my test.

The project

So I literally this afternoon started a GitHub open-source project to achieve this.

All types of resources are handled by its own class, so its easy to extend.

The filenames are named after the Boto3 resource name (ec2, ec2, ses, s3, etc…). prefixed by the provider (aws_, gcp_, etc)

If you’re interested in this project, I’d love to see your collaborations. We can keep adding resources.

The first couple of commits are a little messy, just because it’s the start of the project. But the idea is to keep it clean and tidy, tested and following best practices.

Here’s the cloud-reset repo:

Let’s disect a bit

There is a main script delete_resoures.py that instantiates a class called CloudReset.

When instantiating the class, you have to pass the name of a YAML configuration file. For example, a file named resources_to_delete.yml can have the following contents.

This is valid YAML and its specified to delete resources of type ec2 and s3.

Notice that S3 buckets can be filtered by name (using a regular expression!). If the bucket is not empty you can specify the optional force: true so to delete the contents.

- aws_ec2:
- aws_s3:
    exclude:
        - Name: /-terraform-/ # regular expression
        - Name: /-xxx-/
    options:
        force: true           # deletes bucket contents
- aws_kms:
Enter fullscreen mode Exit fullscreen mode

Down the river, this instantiates the class in the file lib/modules/aws_ec2.py which takes care of finding the ids of all the ec2s and deleting them.

The aws_ec2.py file (or any other resource file) is quite simple. They consist of a class Resource that inherits from the BaseResource class, which enforces to have at least 3 methods (and some properties):

  • get_resources()
  • list_resources()
  • delete_resources()

How to run

Before calling the script, be sure to install the requirements:

pip3 install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

The script can be called as:

python delete_resources.py -f resources_to_delete.yml
Enter fullscreen mode Exit fullscreen mode

The script is configured with a dry_run flag set initially to True, so it should not delete anything by default. But be careful anyway.

Future plans

In the future, we’re planning to specify in the YAML configuration file options to filter by tags, creation date and more. Also, we’d like to add other cloud providers as GCP or Azure.

If you like the idea, clone the repo and show what you’ve got. There are many things we can do.

That’s it for now, but I hope to know more about you!

Full article here

Top comments (0)