From d322fc6c192a73de4eb37c79a2b208bd798adc20 Mon Sep 17 00:00:00 2001 From: Matthew Hooker Date: Mon, 30 Oct 2017 15:14:42 -0700 Subject: [PATCH] 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. --- builder/amazon/common/access_config.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/builder/amazon/common/access_config.go b/builder/amazon/common/access_config.go index c692937a0..03deca8bf 100644 --- a/builder/amazon/common/access_config.go +++ b/builder/amazon/common/access_config.go @@ -4,11 +4,13 @@ import ( "fmt" "log" "os" + "time" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/credentials" "github.com/aws/aws-sdk-go/aws/ec2metadata" "github.com/aws/aws-sdk-go/aws/session" + "github.com/hashicorp/go-cleanhttp" "github.com/hashicorp/packer/template/interpolate" ) @@ -86,8 +88,13 @@ func (c *AccessConfig) region() string { return c.RawRegion } - sess := session.New() - ec2meta := ec2metadata.New(sess) + client := cleanhttp.DefaultClient() + + // 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() if err != nil { log.Println("Error getting region from metadata service, "+