# 🧹 Build an AWS Orphan Resource Cleaner with Go (Step-by-Step + Visual Guide)

# 🧹 Build an AWS Orphan Resource Cleaner with Go (Step-by-Step + Visual Guide)sourav chakraborty

πŸ’Έ Stop wasting money on unused AWS resources β€” automate cleanup with Go πŸš€...

πŸ’Έ Stop wasting money on unused AWS resources β€” automate cleanup with Go


πŸš€ Introduction

Cloud environments grow fast… and so do unused resources.

Unattached EBS volumes, unused Elastic IPs β€” they sit quietly and increase your AWS bill every month.

In this tutorial, you’ll build a real-world Orphan Resource Cleaner using Go and the AWS SDK for Go v2.


🧠 What Are Orphan Resources?

Orphan resources are:

AWS resources that are no longer in use but still cost money

Examples:

  • πŸͺ« Unattached EBS volumes
  • 🌐 Unassociated Elastic IPs

πŸ–ΌοΈ Architecture Overview

Flow:

EventBridge (Scheduler)
        ↓
Go Program (Scanner)
        ↓
AWS APIs (EC2)
        ↓
Detect Orphan Resources
        ↓
Alert / Delete
Enter fullscreen mode Exit fullscreen mode

⚑ Why Use Go for This?

🟒 Single Binary Deployment

No runtime dependency β†’ perfect for automation & Lambda

⚑ High Performance

Fast execution for large AWS environments

🧡 Built-in Concurrency

Scan multiple regions in parallel using goroutines

πŸ” Safer Automation

Strong typing β†’ reduces accidental deletion risks


🟒 Go Basics (Quick Primer)

If you're new to Go, here’s what you need:

Hello World

package main

import "fmt"

func main() {
    fmt.Println("Hello, Go!")
}
Enter fullscreen mode Exit fullscreen mode

Variables

name := "AWS Cleaner"
count := 5
Enter fullscreen mode Exit fullscreen mode

Error Handling

if err != nil {
    fmt.Println("Error:", err)
}
Enter fullscreen mode Exit fullscreen mode

πŸ› οΈ Step-by-Step Setup

1️⃣ Install Go

Check:

go version
Enter fullscreen mode Exit fullscreen mode

If not installed:
πŸ‘‰ https://go.dev/dl/


2️⃣ Create Project

mkdir orphan-cleaner
cd orphan-cleaner
Enter fullscreen mode Exit fullscreen mode

3️⃣ Initialize Module

go mod init orphan-cleaner
Enter fullscreen mode Exit fullscreen mode

4️⃣ Add Code

Create file:

nano main.go
Enter fullscreen mode Exit fullscreen mode

Paste the Orphan Cleaner code.


5️⃣ Install Dependencies

go mod tidy
Enter fullscreen mode Exit fullscreen mode

6️⃣ Configure AWS Credentials

aws configure
Enter fullscreen mode Exit fullscreen mode

OR

export AWS_ACCESS_KEY_ID=xxx
export AWS_SECRET_ACCESS_KEY=xxx
export AWS_REGION=ap-south-1
Enter fullscreen mode Exit fullscreen mode

7️⃣ Run the Program

go run main.go
Enter fullscreen mode Exit fullscreen mode

πŸ–ΌοΈ Example Output

πŸ” Discovering regions...

🌍 Scanning region: ap-south-1
🌐 [ap-south-1] Orphan Elastic IP: 13.xxx.xxx.xxx

🌍 Scanning region: us-east-1
🌐 [us-east-1] Orphan Elastic IP: 100.23.233.31

πŸ“Š Summary Report
-------------------------
πŸͺ« Total Orphan EBS Volumes: 2
🌐 Total Orphan Elastic IPs: 3


Enter fullscreen mode Exit fullscreen mode


⚠️ Safety First

πŸ”’ Always Start with Dry Run

const autoDelete = false
Enter fullscreen mode Exit fullscreen mode

🏷️ Use Tags to Protect Resources

Keep=true
Critical=yes
Enter fullscreen mode Exit fullscreen mode

⏳ Add Grace Period

Only delete resources older than 7 days.


πŸ”” Add Alerts (Optional)

Use Amazon SNS to:

  • Send email alerts
  • Notify before deletion
  • Integrate with Slack

πŸ”₯ Real-World Impact

This simple tool can:

  • Save real money πŸ’°
  • Improve AWS hygiene
  • Reduce attack surface

πŸ€” Why Not Python?

Feature Go Python
Deployment Single binary Runtime needed
Performance Fast Slower
Concurrency Native Limited
Lambda cold start Low Higher

🎯 Conclusion

You’ve built a production-relevant AWS automation tool using Go.

This isn’t just a demo β€” it’s something:

  • DevOps teams use daily
  • FinOps teams rely on
  • Companies build internally

πŸš€ Next Steps

Want to take it further?

  • πŸ”” Add SNS alerts
  • ⚑ Add parallel scanning
  • 🌍 Multi-account support
  • ☁️ Deploy as Lambda

πŸ’‘ Final Thought

β€œUnused cloud resources are silent money leaks β€” automation is the only scalable fix.”