|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "sort" |
| 7 | + "sync" |
| 8 | + |
| 9 | + "github.com/aws/aws-sdk-go/aws" |
| 10 | + "github.com/aws/aws-sdk-go/aws/session" |
| 11 | + "github.com/aws/aws-sdk-go/service/ec2" |
| 12 | +) |
| 13 | + |
| 14 | +type AMIInfo struct { |
| 15 | + ID string |
| 16 | + Name string |
| 17 | +} |
| 18 | + |
| 19 | +type RegionComparison struct { |
| 20 | + Region string |
| 21 | + TotalAMIs int |
| 22 | + MissingAMIs []string |
| 23 | + MissingCount int |
| 24 | +} |
| 25 | + |
| 26 | +func getPublicAMIs(sess *session.Session, region string) ([]AMIInfo, error) { |
| 27 | + ec2Svc := ec2.New(sess, &aws.Config{Region: aws.String(region)}) |
| 28 | + |
| 29 | + input := &ec2.DescribeImagesInput{ |
| 30 | + Owners: []*string{aws.String("self")}, |
| 31 | + Filters: []*ec2.Filter{ |
| 32 | + { |
| 33 | + Name: aws.String("is-public"), |
| 34 | + Values: []*string{aws.String("true")}, |
| 35 | + }, |
| 36 | + }, |
| 37 | + } |
| 38 | + |
| 39 | + output, err := ec2Svc.DescribeImages(input) |
| 40 | + if err != nil { |
| 41 | + return nil, err |
| 42 | + } |
| 43 | + |
| 44 | + var amis []AMIInfo |
| 45 | + for _, image := range output.Images { |
| 46 | + amis = append(amis, AMIInfo{ |
| 47 | + ID: aws.StringValue(image.ImageId), |
| 48 | + Name: aws.StringValue(image.Name), |
| 49 | + }) |
| 50 | + } |
| 51 | + |
| 52 | + return amis, nil |
| 53 | +} |
| 54 | + |
| 55 | +func compareRegions(sess *session.Session, sourceRegion string, regions []*string) []RegionComparison { |
| 56 | + // Get AMIs from source region (us-east-1) |
| 57 | + fmt.Printf("Fetching AMIs from source region: %s...\n", sourceRegion) |
| 58 | + sourceAMIs, err := getPublicAMIs(sess, sourceRegion) |
| 59 | + if err != nil { |
| 60 | + log.Fatalf("Error getting AMIs from %s: %v", sourceRegion, err) |
| 61 | + } |
| 62 | + |
| 63 | + // Create a map of AMI names for quick lookup |
| 64 | + sourceAMINames := make(map[string]bool) |
| 65 | + for _, ami := range sourceAMIs { |
| 66 | + sourceAMINames[ami.Name] = true |
| 67 | + } |
| 68 | + |
| 69 | + fmt.Printf("Source region %s has %d public AMIs\n\n", sourceRegion, len(sourceAMIs)) |
| 70 | + |
| 71 | + // Compare each region |
| 72 | + var comparisons []RegionComparison |
| 73 | + var mu sync.Mutex |
| 74 | + var wg sync.WaitGroup |
| 75 | + |
| 76 | + for _, region := range regions { |
| 77 | + regionName := aws.StringValue(region) |
| 78 | + if regionName == sourceRegion { |
| 79 | + continue // Skip source region |
| 80 | + } |
| 81 | + |
| 82 | + wg.Add(1) |
| 83 | + go func(regionName string) { |
| 84 | + defer wg.Done() |
| 85 | + |
| 86 | + regionAMIs, err := getPublicAMIs(sess, regionName) |
| 87 | + if err != nil { |
| 88 | + log.Printf("Error getting AMIs from %s: %v", regionName, err) |
| 89 | + return |
| 90 | + } |
| 91 | + |
| 92 | + // Create map of region AMI names |
| 93 | + regionAMINames := make(map[string]bool) |
| 94 | + for _, ami := range regionAMIs { |
| 95 | + regionAMINames[ami.Name] = true |
| 96 | + } |
| 97 | + |
| 98 | + // Find missing AMIs |
| 99 | + var missing []string |
| 100 | + for name := range sourceAMINames { |
| 101 | + if !regionAMINames[name] { |
| 102 | + missing = append(missing, name) |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + sort.Strings(missing) |
| 107 | + |
| 108 | + mu.Lock() |
| 109 | + comparisons = append(comparisons, RegionComparison{ |
| 110 | + Region: regionName, |
| 111 | + TotalAMIs: len(regionAMIs), |
| 112 | + MissingAMIs: missing, |
| 113 | + MissingCount: len(missing), |
| 114 | + }) |
| 115 | + mu.Unlock() |
| 116 | + }(regionName) |
| 117 | + } |
| 118 | + |
| 119 | + wg.Wait() |
| 120 | + |
| 121 | + // Sort comparisons by missing count (descending) |
| 122 | + sort.Slice(comparisons, func(i, j int) bool { |
| 123 | + return comparisons[i].MissingCount > comparisons[j].MissingCount |
| 124 | + }) |
| 125 | + |
| 126 | + return comparisons |
| 127 | +} |
| 128 | + |
| 129 | +func getRegions(ec2Svc *ec2.EC2) ([]*string, error) { |
| 130 | + input := &ec2.DescribeRegionsInput{} |
| 131 | + |
| 132 | + output, err := ec2Svc.DescribeRegions(input) |
| 133 | + if err != nil { |
| 134 | + return nil, err |
| 135 | + } |
| 136 | + |
| 137 | + var regions []*string |
| 138 | + for _, region := range output.Regions { |
| 139 | + regions = append(regions, region.RegionName) |
| 140 | + } |
| 141 | + |
| 142 | + return regions, nil |
| 143 | +} |
| 144 | + |
| 145 | +func main() { |
| 146 | + // Create session |
| 147 | + sess := session.Must(session.NewSessionWithOptions(session.Options{ |
| 148 | + SharedConfigState: session.SharedConfigEnable, |
| 149 | + })) |
| 150 | + |
| 151 | + // Create EC2 client |
| 152 | + ec2Svc := ec2.New(sess, &aws.Config{Region: aws.String("us-east-1")}) |
| 153 | + |
| 154 | + // Get all regions |
| 155 | + regions, err := getRegions(ec2Svc) |
| 156 | + if err != nil { |
| 157 | + log.Println("Error getting regions:", err) |
| 158 | + return |
| 159 | + } |
| 160 | + |
| 161 | + // Compare all regions against us-east-1 |
| 162 | + sourceRegion := "us-east-1" |
| 163 | + comparisons := compareRegions(sess, sourceRegion, regions) |
| 164 | + |
| 165 | + // Print results |
| 166 | + fmt.Println("=== AMI Comparison Results ===") |
| 167 | + fmt.Printf("Source: %s\n\n", sourceRegion) |
| 168 | + |
| 169 | + for _, comp := range comparisons { |
| 170 | + if comp.MissingCount > 0 { |
| 171 | + fmt.Printf("%s: %d AMIs (missing %d)\n", comp.Region, comp.TotalAMIs, comp.MissingCount) |
| 172 | + for _, amiName := range comp.MissingAMIs { |
| 173 | + fmt.Printf(" - %s\n", amiName) |
| 174 | + } |
| 175 | + fmt.Println() |
| 176 | + } else { |
| 177 | + fmt.Printf("%s: %d AMIs (complete ✓)\n", comp.Region, comp.TotalAMIs) |
| 178 | + } |
| 179 | + } |
| 180 | +} |
0 commit comments