github.com/stevenmatthewt/agent@v3.5.4+incompatible/agent/ec2_tags.go (about)

     1  package agent
     2  
     3  import (
     4  	"github.com/aws/aws-sdk-go/aws"
     5  	"github.com/aws/aws-sdk-go/aws/ec2metadata"
     6  	"github.com/aws/aws-sdk-go/service/ec2"
     7  )
     8  
     9  type EC2Tags struct {
    10  }
    11  
    12  func (e EC2Tags) Get() (map[string]string, error) {
    13  	sess, err := awsSession()
    14  	if err != nil {
    15  		return nil, err
    16  	}
    17  
    18  	tags := make(map[string]string)
    19  	ec2metadataClient := ec2metadata.New(sess)
    20  
    21  	// Grab the current instances id
    22  	instanceId, err := ec2metadataClient.GetMetadata("instance-id")
    23  	if err != nil {
    24  		return tags, err
    25  	}
    26  
    27  	svc := ec2.New(sess)
    28  
    29  	// Describe the tags of the current instance
    30  	resp, err := svc.DescribeTags(&ec2.DescribeTagsInput{
    31  		Filters: []*ec2.Filter{
    32  			{
    33  				Name: aws.String("resource-id"),
    34  				Values: []*string{
    35  					aws.String(instanceId),
    36  				},
    37  			},
    38  		},
    39  	})
    40  	if err != nil {
    41  		return tags, err
    42  	}
    43  
    44  	// Collect the tags
    45  	for _, tag := range resp.Tags {
    46  		tags[*tag.Key] = *tag.Value
    47  	}
    48  
    49  	return tags, nil
    50  }