Skip to content

Commit

Permalink
add support for profile within provider configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
twang-rs committed Jan 10, 2018
1 parent fa9a867 commit 36382de
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions aws/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,21 +265,27 @@ func (c *Config) Client() (interface{}, error) {
cp, err := creds.Get()
if err != nil {
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NoCredentialProviders" {
// The session may still be able to resolve credentials from shared config.
sess, err := session.NewSession()
if err != nil {
return nil, errors.New(`No valid credential sources found for AWS Provider.
Please see https://terraform.io/docs/providers/aws/index.html for more information on
providing credentials for the AWS Provider`)
}
_, err = sess.Config.Credentials.Get()
if err != nil {
return nil, errors.New(`No valid credential sources found for AWS Provider.
Please see https://terraform.io/docs/providers/aws/index.html for more information on
providing credentials for the AWS Provider`)
// If a profile wasn't specified, the session may still be able to resolve credentials from shared config.
if c.Profile == "" {
sess, err := session.NewSession()
if err != nil {
return nil, errors.New(`No valid credential sources found for AWS Provider.
Please see https://terraform.io/docs/providers/aws/index.html for more information on
providing credentials for the AWS Provider`)
}
_, err = sess.Config.Credentials.Get()
if err != nil {
return nil, errors.New(`No valid credential sources found for AWS Provider.
Please see https://terraform.io/docs/providers/aws/index.html for more information on
providing credentials for the AWS Provider`)
}
log.Printf("[INFO] Using session-derived AWS Auth")
opt.Config.Credentials = sess.Config.Credentials
} else {
log.Printf("[INFO] AWS Auth using Profile: %q", c.Profile)
opt.Profile = c.Profile
opt.SharedConfigState = session.SharedConfigEnable
}
log.Printf("[INFO] Using session-derived AWS Auth")
opt.Config.Credentials = sess.Config.Credentials
} else {
return nil, fmt.Errorf("Error loading credentials for AWS Provider: %s", err)
}
Expand Down

0 comments on commit 36382de

Please sign in to comment.