github.com/pquerna/agent@v2.1.8+incompatible/agent/ec2_tags.go (about)

     1  package agent
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"time"
     7  
     8  	"github.com/AdRoll/goamz/aws"
     9  	"github.com/AdRoll/goamz/ec2"
    10  )
    11  
    12  type EC2Tags struct {
    13  }
    14  
    15  func (e EC2Tags) Get() (map[string]string, error) {
    16  	tags := make(map[string]string)
    17  
    18  	// Passing blank values here instructs the AWS library to look at the
    19  	// current instances meta data for the security credentials.
    20  	auth, err := aws.GetAuth("", "", "", time.Time{})
    21  	if err != nil {
    22  		return tags, errors.New(fmt.Sprintf("Error creating AWS authentication: %s", err.Error()))
    23  	}
    24  
    25  	// Find the current region and create a new EC2 connection
    26  	region := aws.GetRegion(aws.InstanceRegion())
    27  	ec2Client := ec2.New(auth, region)
    28  
    29  	// Filter by the current machines instance-id
    30  	filter := ec2.NewFilter()
    31  	filter.Add("resource-id", aws.InstanceId())
    32  
    33  	// Describe the tags for the current instance
    34  	resp, err := ec2Client.DescribeTags(filter)
    35  	if err != nil {
    36  		return tags, errors.New(fmt.Sprintf("Error downloading tags: %s", err.Error()))
    37  	}
    38  
    39  	// Collect the tags
    40  	for _, tag := range resp.Tags {
    41  		tags[tag.Key] = tag.Value
    42  	}
    43  
    44  	return tags, nil
    45  }