@@ -22,7 +22,6 @@ import (
22
22
//ResourceMachineCreate creates AWS instance
23
23
func ResourceMachineCreate (ctx context.Context , d * schema.ResourceData , m interface {}) error {
24
24
userData := d .Get ("startup_script" ).(string )
25
- instanceName := d .Get ("name" ).(string )
26
25
pairName := d .Id ()
27
26
hddSize := d .Get ("instance_hdd_size" ).(int )
28
27
region := GetRegion (d .Get ("region" ).(string ))
@@ -32,6 +31,15 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
32
31
securityGroup := d .Get ("aws_security_group" ).(string )
33
32
spot := d .Get ("spot" ).(bool )
34
33
spotPrice := d .Get ("spot_price" ).(float64 )
34
+
35
+ metadata := map [string ]string {
36
+ "Name" : d .Get ("name" ).(string ),
37
+ "Id" : d .Id (),
38
+ }
39
+ for key , value := range d .Get ("metadata" ).(map [string ]interface {}) {
40
+ metadata [key ] = value .(string )
41
+ }
42
+
35
43
if ami == "" {
36
44
ami = "iterative-cml"
37
45
}
@@ -96,6 +104,7 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
96
104
svc .ImportKeyPair (ctx , & ec2.ImportKeyPairInput {
97
105
KeyName : aws .String (pairName ),
98
106
PublicKeyMaterial : []byte (keyPublic ),
107
+ TagSpecifications : resourceTagSpecifications (types .ResourceTypeKeyPair , metadata ),
99
108
})
100
109
101
110
// securityGroup
@@ -118,9 +127,10 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
118
127
}
119
128
120
129
gpResult , err := svc .CreateSecurityGroup (ctx , & ec2.CreateSecurityGroupInput {
121
- GroupName : aws .String (securityGroup ),
122
- Description : aws .String ("Iterative security group" ),
123
- VpcId : aws .String (vpcID ),
130
+ GroupName : aws .String (securityGroup ),
131
+ Description : aws .String ("Iterative security group" ),
132
+ VpcId : aws .String (vpcID ),
133
+ TagSpecifications : resourceTagSpecifications (types .ResourceTypeSecurityGroup , metadata ),
124
134
})
125
135
126
136
if err == nil {
@@ -136,13 +146,15 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
136
146
}
137
147
138
148
svc .AuthorizeSecurityGroupIngress (ctx , & ec2.AuthorizeSecurityGroupIngressInput {
139
- GroupId : aws .String (* gpResult .GroupId ),
140
- IpPermissions : ipPermissions ,
149
+ GroupId : aws .String (* gpResult .GroupId ),
150
+ IpPermissions : ipPermissions ,
151
+ TagSpecifications : resourceTagSpecifications (types .ResourceTypeSecurityGroupRule , metadata ),
141
152
})
142
153
143
154
svc .AuthorizeSecurityGroupEgress (ctx , & ec2.AuthorizeSecurityGroupEgressInput {
144
- GroupId : aws .String (* gpResult .GroupId ),
145
- IpPermissions : ipPermissions ,
155
+ GroupId : aws .String (* gpResult .GroupId ),
156
+ IpPermissions : ipPermissions ,
157
+ TagSpecifications : resourceTagSpecifications (types .ResourceTypeSecurityGroupRule , metadata ),
146
158
})
147
159
}
148
160
@@ -225,7 +237,8 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
225
237
SubnetId : aws .String (subnetID ),
226
238
BlockDeviceMappings : blockDeviceMappings ,
227
239
},
228
- InstanceCount : aws .Int32 (1 ),
240
+ InstanceCount : aws .Int32 (1 ),
241
+ TagSpecifications : resourceTagSpecifications (types .ResourceTypeSpotInstancesRequest , metadata ),
229
242
}
230
243
231
244
if spotPrice >= 0 {
@@ -244,8 +257,15 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
244
257
SpotInstanceRequestIds : []string {spotInstanceRequestID },
245
258
}, 10 * time .Minute )
246
259
if err != nil {
260
+ _ , cancelError := svc .CancelSpotInstanceRequests (ctx , & ec2.CancelSpotInstanceRequestsInput {
261
+ SpotInstanceRequestIds : []string {spotInstanceRequestID },
262
+ })
263
+ if cancelError != nil {
264
+ err = cancelError
265
+ }
247
266
return decodeAWSError (region , err )
248
267
}
268
+
249
269
resolvedSpotInstance , err := svc .DescribeSpotInstanceRequests (ctx , & ec2.DescribeSpotInstanceRequestsInput {
250
270
SpotInstanceRequestIds : []string {spotInstanceRequestID },
251
271
})
@@ -254,6 +274,15 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
254
274
}
255
275
256
276
instanceID = * resolvedSpotInstance .SpotInstanceRequests [0 ].InstanceId
277
+
278
+ // Add tags to the created instance
279
+ _ , err = svc .CreateTags (ctx , & ec2.CreateTagsInput {
280
+ Resources : []string {instanceID },
281
+ Tags : resourceTagSpecifications (types .ResourceTypeInstance , metadata )[0 ].Tags ,
282
+ })
283
+ if err != nil {
284
+ return decodeAWSError (region , err )
285
+ }
257
286
} else {
258
287
runResult , err := svc .RunInstances (ctx , & ec2.RunInstancesInput {
259
288
UserData : aws .String (userData ),
@@ -265,6 +294,7 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
265
294
SecurityGroupIds : []string {sgID },
266
295
SubnetId : aws .String (* subDesc .Subnets [0 ].SubnetId ),
267
296
BlockDeviceMappings : blockDeviceMappings ,
297
+ TagSpecifications : resourceTagSpecifications (types .ResourceTypeInstance , metadata ),
268
298
})
269
299
if err != nil {
270
300
return decodeAWSError (region , err )
@@ -273,24 +303,6 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
273
303
instanceID = * runResult .Instances [0 ].InstanceId
274
304
}
275
305
276
- // Add name to the created instance
277
- _ , err = svc .CreateTags (ctx , & ec2.CreateTagsInput {
278
- Resources : []string {instanceID },
279
- Tags : []types.Tag {
280
- {
281
- Key : aws .String ("Name" ),
282
- Value : aws .String (instanceName ),
283
- },
284
- {
285
- Key : aws .String ("Id" ),
286
- Value : aws .String (d .Id ()),
287
- },
288
- },
289
- })
290
- if err != nil {
291
- return decodeAWSError (region , err )
292
- }
293
-
294
306
statusInput := ec2.DescribeInstancesInput {
295
307
InstanceIds : []string {instanceID },
296
308
Filters : []types.Filter {
@@ -421,3 +433,19 @@ func decodeAWSError(region string, err error) error {
421
433
422
434
return fmt .Errorf ("Not able to decode: %s" , err .Error ())
423
435
}
436
+
437
+ func resourceTagSpecifications (resourceType types.ResourceType , tags map [string ]string ) []types.TagSpecification {
438
+ var tagStructs []types.Tag
439
+ for key , value := range tags {
440
+ tagStructs = append (tagStructs , types.Tag {
441
+ Key : aws .String (key ),
442
+ Value : aws .String (value ),
443
+ })
444
+ }
445
+ return []types.TagSpecification {
446
+ {
447
+ ResourceType : resourceType ,
448
+ Tags : tagStructs ,
449
+ },
450
+ }
451
+ }
0 commit comments