@@ -21,6 +21,7 @@ import (
2121 "flag"
2222 "fmt"
2323 "os"
24+ "strings"
2425
2526 "github.com/aws/aws-sdk-go-v2/aws"
2627 "github.com/aws/aws-sdk-go-v2/config"
@@ -29,6 +30,14 @@ import (
2930)
3031
3132func main () {
33+ ctx := context .Background ()
34+ if err := run (ctx ); err != nil {
35+ fmt .Fprintf (os .Stderr , "Error: %v\n " , err )
36+ os .Exit (1 )
37+ }
38+ }
39+
40+ func run (ctx context.Context ) error {
3241 var region string
3342 var instanceType string
3443
@@ -37,15 +46,12 @@ func main() {
3746 flag .Parse ()
3847
3948 if region == "" || instanceType == "" {
40- fmt .Println ("Usage: check-aws-availability -region <region> -instance-type <type>" )
41- os .Exit (1 )
49+ return fmt .Errorf ("Usage: check-aws-availability -region <region> -instance-type <type>" )
4250 }
4351
44- ctx := context .TODO ()
4552 cfg , err := config .LoadDefaultConfig (ctx , config .WithRegion (region ))
4653 if err != nil {
47- fmt .Printf ("Error loading configuration: %v\n " , err )
48- os .Exit (1 )
54+ return fmt .Errorf ("Error loading configuration: %w" , err )
4955 }
5056
5157 client := ec2 .NewFromConfig (cfg )
@@ -62,13 +68,21 @@ func main() {
6268
6369 result , err := client .DescribeInstanceTypeOfferings (ctx , input )
6470 if err != nil {
65- fmt .Printf ("Error describing instance type offerings: %v\n " , err )
66- os .Exit (1 )
71+ return fmt .Errorf ("Error describing instance type offerings: %w" , err )
6772 }
6873
69- if len (result .InstanceTypeOfferings ) > 0 {
70- fmt .Println ("true" )
71- } else {
72- fmt .Println ("false" )
74+ var zones []string
75+
76+ // Gather the availability zones where the instance type is offered
77+ for _ , offering := range result .InstanceTypeOfferings {
78+ zone := aws .ToString (offering .Location )
79+ zones = append (zones , zone )
7380 }
81+
82+ if len (zones ) == 0 {
83+ return fmt .Errorf ("Instance type %s is not available in any availability zones in region %s" , instanceType , region )
84+ }
85+
86+ fmt .Fprintf (os .Stdout , "ZONES=%s\n " , strings .Join (zones , "," ))
87+ return nil
7488}
0 commit comments