From 5e0c4f79c63fe97db3da1c3d965b8237b75aac57 Mon Sep 17 00:00:00 2001 From: FaithKovi Date: Tue, 30 Jul 2024 15:18:18 +0100 Subject: [PATCH] add script to empty s3 bucket --- terraform/empty_bucket.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 terraform/empty_bucket.py diff --git a/terraform/empty_bucket.py b/terraform/empty_bucket.py new file mode 100644 index 0000000..f823ff8 --- /dev/null +++ b/terraform/empty_bucket.py @@ -0,0 +1,20 @@ +import boto3 +import os + +def get_bucket_name(tfvars_file): + with open(tfvars_file, 'r') as file: + for line in file: + if line.startswith("bucket_name"): + return line.split('=')[1].strip().replace('"', '') + +def empty_bucket(bucket_name): + s3 = boto3.resource('s3') + bucket = s3.Bucket(bucket_name) + bucket.objects.all().delete() + +if __name__ == "__main__": + tfvars_file = 'variables.tfvars' + bucket_name = get_bucket_name(tfvars_file) + print(f"Emptying bucket: {bucket_name}") + empty_bucket(bucket_name) + print("Bucket emptied successfully.")