-
Notifications
You must be signed in to change notification settings - Fork 0
/
ec2.go
35 lines (31 loc) · 879 Bytes
/
ec2.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package main
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
)
// AutoScalingTagName indicates tag name created when it runs under auto scaling group.
const AutoScalingTagName = "aws:autoscaling:groupName"
func getInstanceID(svc *ec2metadata.EC2Metadata) (string, error) {
return svc.GetMetadata("instance-id")
}
func getAutoScalingGroupName(svc ec2iface.EC2API, instanceID string) (string, error) {
result, err := svc.DescribeTags(&ec2.DescribeTagsInput{
Filters: []*ec2.Filter{
{
Name: aws.String("resource-id"),
Values: []*string{aws.String(instanceID)},
},
},
})
if err != nil {
return "", err
}
for _, t := range result.Tags {
if *t.Key == AutoScalingTagName {
return *t.Value, nil
}
}
return "", nil
}