@@ -65,21 +65,21 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
65
65
KeyName : aws .String (pairName ),
66
66
PublicKeyMaterial : []byte (keyPublic ),
67
67
})
68
- //if err != nil {
69
- //return err
70
- //}
71
68
72
69
// securityGroup
73
70
var vpcID , sgID string
74
71
if len (securityGroup ) == 0 {
75
72
securityGroup = "cml"
76
73
77
74
vpcsDesc , _ := svc .DescribeVpcs (& ec2.DescribeVpcsInput {})
75
+ if len (vpcsDesc .Vpcs ) == 0 {
76
+ return errors .New ("no VPCs found" )
77
+ }
78
78
vpcID = * vpcsDesc .Vpcs [0 ].VpcId
79
79
80
80
gpResult , err := svc .CreateSecurityGroup (& ec2.CreateSecurityGroupInput {
81
81
GroupName : aws .String (securityGroup ),
82
- Description : aws .String ("CML security group" ),
82
+ Description : aws .String ("Iterative security group" ),
83
83
VpcId : aws .String (vpcID ),
84
84
})
85
85
@@ -117,18 +117,27 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
117
117
if err != nil {
118
118
return err
119
119
}
120
+ if len (sgDesc .SecurityGroups ) == 0 {
121
+ return errors .New ("no Security Groups found" )
122
+ }
120
123
121
124
sgID = * sgDesc .SecurityGroups [0 ].GroupId
122
125
vpcID = * sgDesc .SecurityGroups [0 ].VpcId
123
126
124
- subDesc , _ := svc .DescribeSubnetsWithContext (ctx , & ec2.DescribeSubnetsInput {
127
+ subDesc , err := svc .DescribeSubnetsWithContext (ctx , & ec2.DescribeSubnetsInput {
125
128
Filters : []* ec2.Filter {
126
129
{
127
130
Name : aws .String ("vpc-id" ),
128
131
Values : []* string {aws .String (vpcID )},
129
132
},
130
133
},
131
134
})
135
+ if err != nil {
136
+ return err
137
+ }
138
+ if len (subDesc .Subnets ) == 0 {
139
+ return errors .New ("no subnets found" )
140
+ }
132
141
133
142
//launch instance
134
143
runResult , err := svc .RunInstancesWithContext (ctx , & ec2.RunInstancesInput {
@@ -212,6 +221,10 @@ func ResourceMachineDelete(ctx context.Context, d *schema.ResourceData, m interf
212
221
return err
213
222
}
214
223
224
+ svc .DeleteKeyPair (& ec2.DeleteKeyPairInput {
225
+ KeyName : id ,
226
+ })
227
+
215
228
descResult , err := svc .DescribeInstancesWithContext (ctx , & ec2.DescribeInstancesInput {
216
229
Filters : []* ec2.Filter {
217
230
{
@@ -223,20 +236,16 @@ func ResourceMachineDelete(ctx context.Context, d *schema.ResourceData, m interf
223
236
if err != nil {
224
237
return err
225
238
}
226
-
227
- svc .DeleteKeyPair (& ec2.DeleteKeyPairInput {
228
- KeyName : id ,
229
- })
230
-
231
- instanceID := * descResult .Reservations [0 ].Instances [0 ].InstanceId
232
- _ , err = svc .TerminateInstances (& ec2.TerminateInstancesInput {
233
- InstanceIds : []* string {
234
- aws .String (instanceID ),
235
- },
236
- DryRun : aws .Bool (false ),
237
- })
238
- if err != nil {
239
- return err
239
+ if len (descResult .Reservations ) > 0 && len (descResult .Reservations [0 ].Instances ) > 0 {
240
+ instanceID := * descResult .Reservations [0 ].Instances [0 ].InstanceId
241
+ _ , err = svc .TerminateInstances (& ec2.TerminateInstancesInput {
242
+ InstanceIds : []* string {
243
+ aws .String (instanceID ),
244
+ },
245
+ })
246
+ if err != nil {
247
+ return err
248
+ }
240
249
}
241
250
242
251
return nil
0 commit comments