Skip to content

Instantly share code, notes, and snippets.

@niski84
Created April 24, 2024 08:26
Show Gist options
  • Save niski84/30910ae1f85e5f249f2b83c9b36f8b3d to your computer and use it in GitHub Desktop.
Save niski84/30910ae1f85e5f249f2b83c9b36f8b3d to your computer and use it in GitHub Desktop.
estimate ebs snapshot costs
package main
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/ec2"
)
// SnapshotCostEstimator fetches the size of each snapshot and estimates its cost.
func SnapshotCostEstimator(ctx context.Context) error {
cfg, err := config.LoadDefaultConfig(ctx)
if err != nil {
return fmt.Errorf("failed to load AWS SDK config: %w", err)
}
ec2Client := ec2.NewFromConfig(cfg)
// Assuming US-East-1 prices for example purposes: $0.05 per GB-month
costPerGB := 0.05
paginator := ec2.NewDescribeSnapshotsPaginator(ec2Client, &ec2.DescribeSnapshotsInput{
OwnerIds: []string{"self"},
})
fmt.Println("Calculating snapshot costs...")
totalCost := 0.0
for paginator.HasMorePages() {
out, err := paginator.NextPage(ctx)
if err != nil {
return fmt.Errorf("failed to fetch snapshots: %w", err)
}
for _, snapshot := range out.Snapshots {
sizeGB := float64(*snapshot.VolumeSize) // Size of the volume from which snapshot was taken
snapshotCost := sizeGB * costPerGB
totalCost += snapshotCost
fmt.Printf("Snapshot ID: %s, Size: %d GB, Estimated Monthly Cost: $%.2f\n", *snapshot.SnapshotId, *snapshot.VolumeSize, snapshotCost)
}
}
fmt.Printf("Total Estimated Monthly Cost for All Snapshots: $%.2f\n", totalCost)
return nil
}
func main() {
ctx := context.Background()
err := SnapshotCostEstimator(ctx)
if err != nil {
fmt.Printf("Error: %s\n", err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment