Skip to content

Commit

Permalink
Merge pull request #57 from gruntwork-io/region-override
Browse files Browse the repository at this point in the history
Add the ability to override the region using TERRATEST_REGION
  • Loading branch information
brikis98 authored Mar 4, 2018
2 parents b2cfd06 + 65ef37a commit fad348c
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions aws/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"os"
)

const regionOverrideEnvVarName = "TERRATEST_REGION"

func GetGloballyForbiddenRegions() []string {
return []string{
"us-west-2", // Josh is using this region for his personal projects
Expand All @@ -18,7 +21,13 @@ func GetGloballyForbiddenRegions() []string {

// Get a randomly chosen AWS region that's not in the forbiddenRegions list
func GetRandomRegion(approvedRegions, forbiddenRegions []string) string {
log := log.NewLogger("GetRandomRegion")
logger := log.NewLogger("GetRandomRegion")

regionFromEnvVar := os.Getenv(regionOverrideEnvVarName)
if regionFromEnvVar != "" {
logger.Printf("Using AWS region %s from environment variable %s", regionFromEnvVar, regionOverrideEnvVarName)
return regionFromEnvVar
}

allRegions := []string{
"ap-south-1",
Expand Down Expand Up @@ -73,7 +82,7 @@ func GetRandomRegion(approvedRegions, forbiddenRegions []string) string {
}

if ! selectedRegionIsValid {
log.Println("WARNING: Attempted to select an AWS region 1,000 times and still couldn't find a valid region.")
logger.Println("WARNING: Attempted to select an AWS region 1,000 times and still couldn't find a valid region.")
return "<GetRandomRegions-could-not-select-a-region>"
} else {
return allRegions[randomIndex]
Expand Down

0 comments on commit fad348c

Please sign in to comment.