github.com/ezbercih/terraform@v0.1.1-0.20140729011846-3c33865e0839/builtin/providers/aws/config.go (about) 1 package aws 2 3 import ( 4 "os" 5 "strings" 6 "unicode" 7 8 "github.com/mitchellh/goamz/aws" 9 ) 10 11 type Config struct { 12 AccessKey string `mapstructure:"access_key"` 13 SecretKey string `mapstructure:"secret_key"` 14 Region string `mapstructure:"region"` 15 } 16 17 // AWSAuth returns a valid aws.Auth object for access to AWS services, or 18 // an error if the authentication couldn't be resolved. 19 // 20 // TODO(mitchellh): Test in some way. 21 func (c *Config) AWSAuth() (aws.Auth, error) { 22 auth, err := aws.GetAuth(c.AccessKey, c.SecretKey) 23 if err == nil { 24 // Store the accesskey and secret that we got... 25 c.AccessKey = auth.AccessKey 26 c.SecretKey = auth.SecretKey 27 } 28 29 return auth, err 30 } 31 32 // AWSRegion returns the configured region. 33 // 34 // TODO(mitchellh): Test in some way. 35 func (c *Config) AWSRegion() (aws.Region, error) { 36 if c.Region != "" { 37 return aws.Regions[c.Region], nil 38 } 39 40 if v := os.Getenv("AWS_REGION"); v != "" { 41 return aws.Regions[v], nil 42 } 43 44 md, err := aws.GetMetaData("placement/availability-zone") 45 if err != nil { 46 return aws.Region{}, err 47 } 48 49 region := strings.TrimRightFunc(string(md), unicode.IsLetter) 50 return aws.Regions[region], nil 51 }