Shorten metadata timeout

When running in travis, metadata requests will timeout after 5 seconds.
After 24 such timeouts, we'll hit travis' build timeout of two minutes,
and the build will fail. Lowering it to 100 gets us in a safe time
limit. We _may_ need to expose a timeout env var with this logic,
however.
This commit is contained in:
Matthew Hooker 2017-10-30 15:14:42 -07:00
parent 314fc94bd8
commit d322fc6c19
No known key found for this signature in database
GPG Key ID: 7B5F933D9CE8C6A1
1 changed files with 9 additions and 2 deletions

View File

@ -4,11 +4,13 @@ import (
"fmt" "fmt"
"log" "log"
"os" "os"
"time"
"github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials" "github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/ec2metadata" "github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/aws/session"
"github.com/hashicorp/go-cleanhttp"
"github.com/hashicorp/packer/template/interpolate" "github.com/hashicorp/packer/template/interpolate"
) )
@ -86,8 +88,13 @@ func (c *AccessConfig) region() string {
return c.RawRegion return c.RawRegion
} }
sess := session.New() client := cleanhttp.DefaultClient()
ec2meta := ec2metadata.New(sess)
// Keep the default timeout (100ms) low as we don't want to wait in non-EC2 environments
client.Timeout = 100 * time.Millisecond
ec2meta := ec2metadata.New(session.New(), &aws.Config{
HTTPClient: client,
})
region, err := ec2meta.Region() region, err := ec2meta.Region()
if err != nil { if err != nil {
log.Println("Error getting region from metadata service, "+ log.Println("Error getting region from metadata service, "+