try again to get region from metadata

This commit is contained in:
Matthew Hooker 2017-06-14 16:30:18 -07:00
parent 8dae6b60c1
commit febc65aa7c
No known key found for this signature in database
GPG Key ID: 7B5F933D9CE8C6A1
1 changed files with 10 additions and 1 deletions

View File

@ -7,6 +7,7 @@ import (
"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/packer/template/interpolate"
)
@ -91,7 +92,15 @@ func (c *AccessConfig) Region() (string, error) {
return c.RawRegion, nil
}
return "", nil
sess := session.New()
ec2meta := ec2metadata.New(sess)
identity, err := ec2meta.GetInstanceIdentityDocument()
if err != nil {
log.Println("Error getting region from metadata service, "+
"probably because we're not running on AWS.", err)
return "", nil
}
return identity.Region, nil
}
func (c *AccessConfig) Prepare(ctx *interpolate.Context) []error {