Skip to content

Commit 1d08b2b

Browse files
Add support for AWS metadata (tags) (#187)
* Upgrade aws-sdk-go to v2 * Restyled by gofmt * Add support for AWS metadata (tags) * Fix spot instance tagging Co-authored-by: Restyled.io <commits@restyled.io>
1 parent 90a3ff6 commit 1d08b2b

File tree

1 file changed

+55
-27
lines changed

1 file changed

+55
-27
lines changed

iterative/aws/provider.go

Lines changed: 55 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
//ResourceMachineCreate creates AWS instance
2323
func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interface{}) error {
2424
userData := d.Get("startup_script").(string)
25-
instanceName := d.Get("name").(string)
2625
pairName := d.Id()
2726
hddSize := d.Get("instance_hdd_size").(int)
2827
region := GetRegion(d.Get("region").(string))
@@ -32,6 +31,15 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
3231
securityGroup := d.Get("aws_security_group").(string)
3332
spot := d.Get("spot").(bool)
3433
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+
3543
if ami == "" {
3644
ami = "iterative-cml"
3745
}
@@ -96,6 +104,7 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
96104
svc.ImportKeyPair(ctx, &ec2.ImportKeyPairInput{
97105
KeyName: aws.String(pairName),
98106
PublicKeyMaterial: []byte(keyPublic),
107+
TagSpecifications: resourceTagSpecifications(types.ResourceTypeKeyPair, metadata),
99108
})
100109

101110
// securityGroup
@@ -118,9 +127,10 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
118127
}
119128

120129
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),
124134
})
125135

126136
if err == nil {
@@ -136,13 +146,15 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
136146
}
137147

138148
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),
141152
})
142153

143154
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),
146158
})
147159
}
148160

@@ -225,7 +237,8 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
225237
SubnetId: aws.String(subnetID),
226238
BlockDeviceMappings: blockDeviceMappings,
227239
},
228-
InstanceCount: aws.Int32(1),
240+
InstanceCount: aws.Int32(1),
241+
TagSpecifications: resourceTagSpecifications(types.ResourceTypeSpotInstancesRequest, metadata),
229242
}
230243

231244
if spotPrice >= 0 {
@@ -244,8 +257,15 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
244257
SpotInstanceRequestIds: []string{spotInstanceRequestID},
245258
}, 10*time.Minute)
246259
if err != nil {
260+
_, cancelError := svc.CancelSpotInstanceRequests(ctx, &ec2.CancelSpotInstanceRequestsInput{
261+
SpotInstanceRequestIds: []string{spotInstanceRequestID},
262+
})
263+
if cancelError != nil {
264+
err = cancelError
265+
}
247266
return decodeAWSError(region, err)
248267
}
268+
249269
resolvedSpotInstance, err := svc.DescribeSpotInstanceRequests(ctx, &ec2.DescribeSpotInstanceRequestsInput{
250270
SpotInstanceRequestIds: []string{spotInstanceRequestID},
251271
})
@@ -254,6 +274,15 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
254274
}
255275

256276
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+
}
257286
} else {
258287
runResult, err := svc.RunInstances(ctx, &ec2.RunInstancesInput{
259288
UserData: aws.String(userData),
@@ -265,6 +294,7 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
265294
SecurityGroupIds: []string{sgID},
266295
SubnetId: aws.String(*subDesc.Subnets[0].SubnetId),
267296
BlockDeviceMappings: blockDeviceMappings,
297+
TagSpecifications: resourceTagSpecifications(types.ResourceTypeInstance, metadata),
268298
})
269299
if err != nil {
270300
return decodeAWSError(region, err)
@@ -273,24 +303,6 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
273303
instanceID = *runResult.Instances[0].InstanceId
274304
}
275305

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-
294306
statusInput := ec2.DescribeInstancesInput{
295307
InstanceIds: []string{instanceID},
296308
Filters: []types.Filter{
@@ -421,3 +433,19 @@ func decodeAWSError(region string, err error) error {
421433

422434
return fmt.Errorf("Not able to decode: %s", err.Error())
423435
}
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

Comments
 (0)