Don't set region from metadata if profile is set.
This commit is contained in:
parent
52fc0100eb
commit
c106e7c26c
|
@ -34,15 +34,15 @@ func (c *AccessConfig) Session() (*session.Session, error) {
|
||||||
return c.session, nil
|
return c.session, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
config := aws.NewConfig().WithMaxRetries(11).WithCredentialsChainVerboseErrors(true)
|
||||||
|
|
||||||
if c.ProfileName != "" {
|
if c.ProfileName != "" {
|
||||||
if err := os.Setenv("AWS_PROFILE", c.ProfileName); err != nil {
|
if err := os.Setenv("AWS_PROFILE", c.ProfileName); err != nil {
|
||||||
return nil, fmt.Errorf("Set env error: %s", err)
|
return nil, fmt.Errorf("Set env error: %s", err)
|
||||||
}
|
}
|
||||||
}
|
} else if c.RawRegion != "" {
|
||||||
|
config = config.WithRegion(c.RawRegion)
|
||||||
config := aws.NewConfig().WithMaxRetries(11).WithCredentialsChainVerboseErrors(true)
|
} else if region := c.metadataRegion(); region != "" {
|
||||||
|
|
||||||
if region := c.region(); region != "" {
|
|
||||||
config = config.WithRegion(region)
|
config = config.WithRegion(region)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -68,25 +68,26 @@ func (c *AccessConfig) Session() (*session.Session, error) {
|
||||||
SharedConfigState: session.SharedConfigEnable,
|
SharedConfigState: session.SharedConfigEnable,
|
||||||
Config: *config,
|
Config: *config,
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.MFACode != "" {
|
if c.MFACode != "" {
|
||||||
opts.AssumeRoleTokenProvider = func() (string, error) {
|
opts.AssumeRoleTokenProvider = func() (string, error) {
|
||||||
return c.MFACode, nil
|
return c.MFACode, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var err error
|
|
||||||
c.session, err = session.NewSessionWithOptions(opts)
|
if session, err := session.NewSessionWithOptions(opts); err != nil {
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
|
} else if *session.Config.Region == "" {
|
||||||
|
return nil, fmt.Errorf("Could not find AWS region, make sure it's set.")
|
||||||
|
} else {
|
||||||
|
c.session = session
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.session, nil
|
return c.session, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// region returns either the region from config or region from metadata service
|
// metadataRegion returns the region from the metadata service
|
||||||
func (c *AccessConfig) region() string {
|
func (c *AccessConfig) metadataRegion() string {
|
||||||
if c.RawRegion != "" {
|
|
||||||
return c.RawRegion
|
|
||||||
}
|
|
||||||
|
|
||||||
client := cleanhttp.DefaultClient()
|
client := cleanhttp.DefaultClient()
|
||||||
|
|
||||||
|
@ -112,9 +113,5 @@ func (c *AccessConfig) Prepare(ctx *interpolate.Context) []error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(errs) > 0 {
|
return errs
|
||||||
return errs
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,10 +45,11 @@ func (c *AMIConfig) Prepare(accessConfig *AccessConfig, ctx *interpolate.Context
|
||||||
session, err := accessConfig.Session()
|
session, err := accessConfig.Session()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errs = append(errs, err)
|
errs = append(errs, err)
|
||||||
}
|
} else {
|
||||||
region := *session.Config.Region
|
region := *session.Config.Region
|
||||||
if stringInSlice(c.AMIRegions, region) {
|
if stringInSlice(c.AMIRegions, region) {
|
||||||
errs = append(errs, fmt.Errorf("Cannot copy AMI to AWS session region '%s', please remove it from `ami_regions`.", region))
|
errs = append(errs, fmt.Errorf("Cannot copy AMI to AWS session region '%s', please remove it from `ami_regions`.", region))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue