Automating AWS S3 bucket cleanup with Go and Cobra CLI. A step by step guide. This podcast is brought to you by Business Compass, LLC. We are an Amazon Web Services advanced consulting partner. We build and migrate workloads to Amazon Cloud. Our website is Business compassllc.com. Our phone number is 973-638-2228. Our e-mail address is Contact at Business Compass, LLC dot US and cloud environments. Managing storage costs is crucial for optimizing infrastructure. AWS S3 buckets can accumulate unused data overtime, resulting in unnecessary expenses. Automating the cleanup of these buckets not only helps maintain a clean and cost effective environment, but simplifies bucket management. This podcast will explore how to automate a WS3 cleanup using Go and the Cobra CLI. Introduction to automating AWS S3 cleanup. AWS 3 is a powerful object storage service, but unused buckets and old objects can lead to high costs overtime. Automating the cleanup process ensures that only necessary resources are retained, saving time and money. By leveraging the Go programming language and Cobra CLI, we can create a streamlined, efficient command line tool to list, empty, and delete unwanted buckets, setting up the Go project environment. Setting UA Clean Development Environment for your project is essential before diving into the code. First, make sure you have Go installed on your machine and create a new project directory for the cleanup utility. Install Go. You can download and install Go from the official website. Create a new Go module. MCR 3 cleanup. CDs 3 cleanup. Go mad in a github.com/your username slash S3 cleanup. Three, install Cobra CLI. Cobra CLI is a library for creating CLI applications in. Go install it using. Gogetyougithub.com/SPF 13 Cobra. Cobra. Integrating AWS SDK and Cobra CLI. We will integrate the AWS SDK for Go to interact with AWS 3. This allows us to programmatically access S3 list buckets and perform operations like emptying and deleting them. Install the AWSDK. Gogetgithub.com slash AW/AW SDK go. Set up Cobra CLI. Create a new Cobra based CLI command to serve as the entry point for your S3 cleanup tool. Cobra in it. This command will scaffold a basic CLI structure. Add a new command to handle bucket cleanup, listing and selecting buckets for deletion. The first step is to list all the S3 buckets in your AWS account and allow users to choose which ones to delete. Using the AWS SDK, we can fetch and display the list of buckets. List buckets code. Funk list buckets SVC S3S3S3 bucket error. Result air equals SVC dot list buckets and S3 dot list buckets input if R equals nil. Return nil air. Return result dot buckets nil. Display buckets to user in the Cobra command. Prompt the user to select one or more buckets for deletion, implementing bucket emptying and deletion logic. Once the user selects the buckets, the next step is to ensure that the buckets are emptied before deletion. S3 does not allow the deletion of nonempty buckets, so it's essential to 1st remove all objects within them Empty bucket code. Funk empty bucket SVC S3S3 bucket name string error. Slash slash code to list and delete all objects in the bucket. Delete bucket. Once the bucket is emptied, delete the bucket. Funk delete bucket SVC S3S3 bucket name string error. Under score air equals SVC delete bucket and S3 delete bucket input. Bucket AWS string bucket name. Return air. Adding user confirmation and error handling. It's important to implement a confirmation step to prevent accidental deletion of buckets. You can prompt users with a simple yes slash no confirmation before proceeding with the deletion user confirmation prompt. Funk confirmed election bucket name string bool. FMT printf Are you sure you want to delete bucket percent SYN bucket name var response string FMT Scanlon and response return strings dot to lower response equals Y? Error handling. Ensure proper error handling for cases where the bucket cannot be emptied or deleted due to permissions or other issues. Finalizing and testing the cleanup utility. After implementing the core features, finalize your tool by thoroughly testing it. Test it in different environments, such as accounts with various permissions and buckets with different configurations. For example, versioning enabled test cases. Deleting an empty bucket. Deleting a bucket with objects, Handling permission errors. Ensuring confirmation prompt works. Run tests. Make sure the tool handles different edge cases without crashing, and provides meaningful feedback to the user in case of failures, enhancements, and future directions. While the basic functionality covers listing, emptying and deleting buckets, there are several enhancements you could add in the future. Logging. Add logging for audit purposes. Scheduled cleanup. Implement a scheduler, for example using AWS Lambda to automatically run the cleanup periodically. Tag based filtering. Delete buckets based on specific tags or criteria. DRY run mode Allow users to simulate the deletion process without actually deleting any buckets. Conclusion. Automating AWS 3 cleanup with Go and Cobra CLI streamlines the bucket management process, helping to maintain a cost effective cloud environment. By integrating the AWS SDK and Cobra CLI, we've built a powerful and efficient tool that not only automates the cleanup, but also ensures user control and error handling throughout the process. Thank you for listening to our podcast. Please share this podcast, visit our website, and follow our social media links.