-
Notifications
You must be signed in to change notification settings - Fork 0
/
ec2_tags.go
50 lines (41 loc) · 920 Bytes
/
ec2_tags.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package agent
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/service/ec2"
)
type EC2Tags struct {
}
func (e EC2Tags) Get() (map[string]string, error) {
sess, err := awsSession()
if err != nil {
return nil, err
}
tags := make(map[string]string)
ec2metadataClient := ec2metadata.New(sess)
// Grab the current instances id
instanceId, err := ec2metadataClient.GetMetadata("instance-id")
if err != nil {
return tags, err
}
svc := ec2.New(sess)
// Describe the tags of the current instance
resp, err := svc.DescribeTags(&ec2.DescribeTagsInput{
Filters: []*ec2.Filter{
{
Name: aws.String("resource-id"),
Values: []*string{
aws.String(instanceId),
},
},
},
})
if err != nil {
return tags, err
}
// Collect the tags
for _, tag := range resp.Tags {
tags[*tag.Key] = *tag.Value
}
return tags, nil
}