aws: better error handling of region guessing from metadata

* this will fix #6919
This commit is contained in:
Adrien Delorme 2018-10-26 12:19:29 +02:00
parent 2b564eb563
commit 16aa21dffe
1 changed files with 13 additions and 11 deletions

View File

@ -50,11 +50,12 @@ func (c *AccessConfig) Session() (*session.Session, error) {
// default is 3, and when it was causing failures for users being throttled
config = config.WithMaxRetries(20)
if c.RawRegion != "" {
config = config.WithRegion(c.RawRegion)
} else if region := c.metadataRegion(); region != "" {
config = config.WithRegion(region)
region, err := c.region()
if err != nil {
return nil, fmt.Errorf("Could not get region, "+
"probably because it's not set or we're not running on AWS. %s", err)
}
config = config.WithRegion(region)
if c.CustomEndpointEc2 != "" {
config = config.WithEndpoint(c.CustomEndpointEc2)
@ -119,7 +120,7 @@ func (c *AccessConfig) IsChinaCloud() bool {
}
// metadataRegion returns the region from the metadata service
func (c *AccessConfig) metadataRegion() string {
func (c *AccessConfig) metadataRegion() (string, error) {
client := cleanhttp.DefaultClient()
@ -128,13 +129,14 @@ func (c *AccessConfig) metadataRegion() string {
ec2meta := ec2metadata.New(session.New(), &aws.Config{
HTTPClient: client,
})
region, err := ec2meta.Region()
if err != nil {
log.Println("Error getting region from metadata service, "+
"probably because we're not running on AWS.", err)
return ""
return ec2meta.Region()
}
func (c *AccessConfig) region() (string, error) {
if c.RawRegion != "" {
return c.RawRegion, nil
}
return region
return c.metadataRegion()
}
func (c *AccessConfig) Prepare(ctx *interpolate.Context) []error {