diff --git a/AUTHORS.md b/AUTHORS.md index 616545995b..57eaa5a968 100644 --- a/AUTHORS.md +++ b/AUTHORS.md @@ -29,6 +29,7 @@ The following individuals have contributed code to this repository * Joonas Bergius * Joseph Wilk * Julius Volz +* Kraig Amador * Laurie Malau * Marko Mikulicic * Matt T. Proud diff --git a/config/config.go b/config/config.go index 482671182c..34ee8c4881 100644 --- a/config/config.go +++ b/config/config.go @@ -907,6 +907,7 @@ type EC2SDConfig struct { Region string `yaml:"region"` AccessKey string `yaml:"access_key,omitempty"` SecretKey string `yaml:"secret_key,omitempty"` + Profile string `yaml:"profile,omitempty"` RefreshInterval model.Duration `yaml:"refresh_interval,omitempty"` Port int `yaml:"port"` diff --git a/config/config_test.go b/config/config_test.go index 1da06cf0f5..e4547b9826 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -292,6 +292,7 @@ var expectedConf = &Config{ Region: "us-east-1", AccessKey: "access", SecretKey: "secret", + Profile: "profile", RefreshInterval: model.Duration(60 * time.Second), Port: 80, }, diff --git a/config/testdata/conf.good.yml b/config/testdata/conf.good.yml index e2387f074c..da7883c16f 100644 --- a/config/testdata/conf.good.yml +++ b/config/testdata/conf.good.yml @@ -143,6 +143,7 @@ scrape_configs: - region: us-east-1 access_key: access secret_key: secret + profile: profile - job_name: service-azure azure_sd_configs: diff --git a/retrieval/discovery/ec2.go b/retrieval/discovery/ec2.go index 65aa7bac06..73a095613c 100644 --- a/retrieval/discovery/ec2.go +++ b/retrieval/discovery/ec2.go @@ -21,7 +21,7 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/credentials" - "github.com/aws/aws-sdk-go/aws/defaults" + "github.com/aws/aws-sdk-go/aws/session" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/common/log" "github.com/prometheus/common/model" @@ -72,6 +72,7 @@ func init() { type EC2Discovery struct { aws *aws.Config interval time.Duration + profile string port int } @@ -79,13 +80,14 @@ type EC2Discovery struct { func NewEC2Discovery(conf *config.EC2SDConfig) *EC2Discovery { creds := credentials.NewStaticCredentials(conf.AccessKey, conf.SecretKey, "") if conf.AccessKey == "" && conf.SecretKey == "" { - creds = defaults.DefaultChainCredentials + creds = nil } return &EC2Discovery{ aws: &aws.Config{ Region: &conf.Region, Credentials: creds, }, + profile: conf.Profile, interval: time.Duration(conf.RefreshInterval), port: conf.Port, } @@ -130,7 +132,15 @@ func (ed *EC2Discovery) refresh() (tg *config.TargetGroup, err error) { } }() - ec2s := ec2.New(ed.aws) + sess, err := session.NewSessionWithOptions(session.Options{ + Config: *ed.aws, + Profile: ed.profile, + }) + if err != nil { + return nil, fmt.Errorf("could not create aws session: %s", err) + } + + ec2s := ec2.New(sess) tg = &config.TargetGroup{ Source: *ed.aws.Region, }