@@ -33,8 +33,8 @@ var networkCheckTag = []iam_types.Tag{
33
33
},
34
34
}
35
35
36
- func initAwsConfig (ctx context.Context ) (aws.Config , error ) {
37
- return config .LoadDefaultConfig (ctx )
36
+ func initAwsConfig (ctx context.Context , region string ) (aws.Config , error ) {
37
+ return config .LoadDefaultConfig (ctx , config . WithRegion ( region ) )
38
38
}
39
39
40
40
// this will be useful when we are cleaning up things at the end
@@ -51,7 +51,7 @@ var checkCommand = &cobra.Command{ // nolint:gochecknoglobals
51
51
Short : "Runs the network check diagnosis" ,
52
52
SilenceUsage : false ,
53
53
RunE : func (cmd * cobra.Command , args []string ) error {
54
- cfg , err := initAwsConfig (cmd .Context ())
54
+ cfg , err := initAwsConfig (cmd .Context (), networkConfig . AwsRegion )
55
55
if err != nil {
56
56
return err
57
57
}
@@ -61,7 +61,10 @@ var checkCommand = &cobra.Command{ // nolint:gochecknoglobals
61
61
iamClient := iam .NewFromConfig (cfg )
62
62
63
63
defer cleanup (cmd .Context (), ec2Client , iamClient )
64
- checkSMPrerequisites (cmd .Context (), ec2Client )
64
+ err = checkSMPrerequisites (cmd .Context (), ec2Client )
65
+ if err != nil {
66
+ return fmt .Errorf ("❌ failed to check prerequisites: %v" , err )
67
+ }
65
68
66
69
role , err := createIAMRoleAndAttachPolicy (cmd .Context (), iamClient )
67
70
if err != nil {
@@ -75,18 +78,19 @@ var checkCommand = &cobra.Command{ // nolint:gochecknoglobals
75
78
}
76
79
InstanceProfile = aws .ToString (instanceProfile .InstanceProfileName )
77
80
78
- log .Infof ("ℹ️ Launching EC2 instance in a Main subnet " )
79
- mainInstanceId , err := launchInstance (cmd .Context (), ec2Client , networkConfig .MainSubnets [ 0 ] , instanceProfile .Arn )
81
+ log .Infof ("ℹ️ Launching EC2 instances in Main subnets " )
82
+ mainInstanceIds , err := launchInstances (cmd .Context (), ec2Client , networkConfig .MainSubnets , instanceProfile .Arn )
80
83
if err != nil {
81
- return fmt . Errorf ( "❌ Failed to launch instances in subnet %s: %v" , networkConfig . MainSubnets [ 0 ], err )
84
+ return err
82
85
}
86
+ InstanceIds = append (InstanceIds , mainInstanceIds ... )
83
87
84
- log .Infof ("ℹ️ Launching EC2 instance in a Pod subnet " )
85
- podInstanceId , err := launchInstance (cmd .Context (), ec2Client , networkConfig .PodSubnets [ 0 ] , instanceProfile .Arn )
88
+ log .Infof ("ℹ️ Launching EC2 instances in a Pod subnets " )
89
+ podInstanceIds , err := launchInstances (cmd .Context (), ec2Client , networkConfig .PodSubnets , instanceProfile .Arn )
86
90
if err != nil {
87
- return fmt . Errorf ( "❌ Failed to launch instances in subnet %s: %v" , networkConfig . PodSubnets [ 0 ], err )
91
+ return err
88
92
}
89
- InstanceIds = [] string { mainInstanceId , podInstanceId }
93
+ InstanceIds = append ( InstanceIds , podInstanceIds ... )
90
94
91
95
log .Infof ("ℹ️ Waiting for EC2 instances to become ready (can take up to 2 minutes)" )
92
96
waiter := ec2 .NewInstanceRunningWaiter (ec2Client , func (irwo * ec2.InstanceRunningWaiterOptions ) {
@@ -129,7 +133,7 @@ var checkCommand = &cobra.Command{ // nolint:gochecknoglobals
129
133
"S3" : fmt .Sprintf ("https://s3.%s.amazonaws.com" , networkConfig .AwsRegion ),
130
134
"DynamoDB" : fmt .Sprintf ("https://dynamodb.%s.amazonaws.com" , networkConfig .AwsRegion ),
131
135
}
132
- checkServicesAvailability (cmd .Context (), ssmClient , [] string { mainInstanceId } , serviceEndpointsForMain )
136
+ checkServicesAvailability (cmd .Context (), ssmClient , mainInstanceIds , serviceEndpointsForMain )
133
137
134
138
return nil
135
139
},
@@ -240,8 +244,22 @@ func validateSubnets(cmd *cobra.Command, args []string) error {
240
244
return nil
241
245
}
242
246
243
- func launchInstance (ctx context.Context , ec2Svc * ec2.Client , subnetID string , instanceProfileName * string ) (string , error ) {
244
- regionalAMI , err := findUbuntuAMI (ctx , ec2Svc )
247
+ func launchInstances (ctx context.Context , ec2Client * ec2.Client , subnets []string , profileArn * string ) ([]string , error ) {
248
+ var instanceIds []string
249
+ for _ , subnet := range subnets {
250
+ instanceId , err := launchInstanceInSubnet (ctx , ec2Client , subnet , profileArn )
251
+ if err != nil {
252
+ return nil , fmt .Errorf ("❌ Failed to launch instances in subnet %s: %v" , subnet , err )
253
+ }
254
+
255
+ instanceIds = append (instanceIds , instanceId )
256
+ }
257
+
258
+ return instanceIds , nil
259
+ }
260
+
261
+ func launchInstanceInSubnet (ctx context.Context , ec2Client * ec2.Client , subnetID string , instanceProfileName * string ) (string , error ) {
262
+ regionalAMI , err := findUbuntuAMI (ctx , ec2Client )
245
263
if err != nil {
246
264
return "" , err
247
265
}
@@ -275,7 +293,7 @@ func launchInstance(ctx context.Context, ec2Svc *ec2.Client, subnetID string, in
275
293
276
294
var result * ec2.RunInstancesOutput
277
295
err = wait .PollUntilContextTimeout (ctx , 500 * time .Millisecond , 10 * time .Second , false , func (ctx context.Context ) (done bool , err error ) {
278
- result , err = ec2Svc .RunInstances (ctx , input )
296
+ result , err = ec2Client .RunInstances (ctx , input )
279
297
280
298
if err != nil {
281
299
if strings .Contains (err .Error (), "Invalid IAM Instance Profile ARN" ) {
0 commit comments