diff --git a/builder/amazon/common/regions.go b/builder/amazon/common/regions.go index ad71a027f..ec2a06ed7 100644 --- a/builder/amazon/common/regions.go +++ b/builder/amazon/common/regions.go @@ -1,29 +1,26 @@ package common -// aws ec2 describe-regions --query 'Regions[].{Name:RegionName}' --output text | sed 's/\(.*\)/"\1",/' | sort +import ( + "github.com/aws/aws-sdk-go/aws/session" + "github.com/aws/aws-sdk-go/service/ec2" +) + func listEC2Regions() []string { - return []string{ - "ap-northeast-1", - "ap-northeast-2", - "ap-northeast-3", - "ap-south-1", - "ap-southeast-1", - "ap-southeast-2", - "ca-central-1", - "eu-central-1", - "eu-west-1", - "eu-west-2", - "eu-west-3", - "sa-east-1", - "us-east-1", - "us-east-2", - "us-west-1", - "us-west-2", - // not part of autogenerated list - "us-gov-west-1", - "cn-north-1", - "cn-northwest-1", + var regions []string + // append regions that are not part of autogenerated list + regions = append(regions, "us-gov-west-1", "cn-north-1", "cn-northwest-1") + + sess := session.Must(session.NewSessionWithOptions(session.Options{ + SharedConfigState: session.SharedConfigEnable, + })) + + ec2conn := ec2.New(sess) + resultRegions, _ := ec2conn.DescribeRegions(nil) + for _, region := range resultRegions.Regions { + regions = append(regions, *region.RegionName) } + + return regions } // ValidateRegion returns true if the supplied region is a valid AWS