gitlab.com/beacon-software/gadget@v0.0.0-20181217202115-54565ea1ed5e/environment/aws.go (about)

     1  package environment
     2  
     3  import (
     4  	"io/ioutil"
     5  	"net/http"
     6  	"strings"
     7  
     8  	"gitlab.com/beacon-software/gadget/log"
     9  )
    10  
    11  const awsMetaService = "aws-meta://"
    12  
    13  func isAWSMetaServiceLookup(host string) bool {
    14  	return strings.HasPrefix(host, awsMetaService)
    15  }
    16  
    17  // AWSLookup tries to perform a Get against the provided URL
    18  // This is a work around intended to get the Local Host IP / Name for the EC2 instance that service is running on
    19  // through the AWS Meta data service since we can't dynamically set that environment variable.
    20  func AWSLookup(host string) string {
    21  	target := strings.TrimLeft(host, awsMetaService)
    22  	resp, err := http.Get("http://" + target)
    23  	if err != nil {
    24  		log.Errorf("Unable to performs AWS Lookup against %s", target)
    25  		return host
    26  	}
    27  	defer resp.Body.Close()
    28  	body, err := ioutil.ReadAll(resp.Body)
    29  	if err != nil {
    30  		log.Errorf("Read from %s failed with %s", target, err)
    31  		return host
    32  	}
    33  	log.Infof("AWS Lookup: %s", string(body))
    34  	return string(body)
    35  }