aws-sdk-go icon indicating copy to clipboard operation
aws-sdk-go copied to clipboard

Panic in elb.CreateLoadBalancer when AWS_REGION is not set

Open geekifier opened this issue 2 years ago • 1 comments

Describe the bug

Stack trace is received when using running elb.CreateLoadBalancer and the AWS_REGION env var is not set. This is despite the region being set in ~/.aws/credentials, and AWS_PROFILE being set to the relevant profile's name. When AWS_REGION env var is set, all is good.

Expected Behavior

Provide an actual error message instead of a panic.

Current Behavior


$ go version
go version go1.17.8 darwin/amd64

$ go test
TestR53Example 2022-04-20T11:50:36-04:00 r53_example_test.go:67: Creating mock resources: ELB with the following inputs:
 {
  Listeners: [{
      InstancePort: 80,
      InstanceProtocol: "HTTP",
      LoadBalancerPort: 80,
      Protocol: "HTTP"
    }],
  LoadBalancerName: "terratest-elb-AoZz5X",
  Scheme: "internal",
  Subnets: ["REDACTED"],
  Tags: [
    {REDACTED}
  ]
}
--- FAIL: TestR53Example (0.00s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
	panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x20eb9cf]

goroutine 50 [running]:
testing.tRunner.func1.2({0x2211ca0, 0x33787f0})
	/usr/local/Cellar/go/1.17.8/libexec/src/testing/testing.go:1209 +0x24e
testing.tRunner.func1()
	/usr/local/Cellar/go/1.17.8/libexec/src/testing/testing.go:1212 +0x218
panic({0x2211ca0, 0x33787f0})
	/usr/local/Cellar/go/1.17.8/libexec/src/runtime/panic.go:1038 +0x215
REDACTED_REPO_NAME.createMockResources(0x26d27a0, 0xc000183700, {0xc000048240, 0x14})
	/Users/kamil_markowicz/code/terraform/modules/isg-tf-mod-r53_lb_alias/tests/r53_example_test.go:71 +0x74f
REDACTED_REPO_NAME.TestR53Example(0xc000682d00)
	/Users/kamil_markowicz/code/terraform/modules/isg-tf-mod-r53_lb_alias/tests/r53_example_test.go:159 +0x13d
testing.tRunner(0xc000682d00, 0x24be958)
	/usr/local/Cellar/go/1.17.8/libexec/src/testing/testing.go:1259 +0x102
created by testing.(*T).Run
	/usr/local/Cellar/go/1.17.8/libexec/src/testing/testing.go:1306 +0x35a
exit status 2
FAIL	REDACTED_REPO_NAME	0.663s

Reproduction Steps

;;~/.aws/credentials 

[REACTED_ROLE_NAME]
output = json
region = us-east-1
aws_access_key_id = <secret>
aws_secret_access_key = <secret>
aws_session_token = <secret>
$ env | grep AWS
AWS_PROFILE=REACTED_ROLE_NAME
unset AWS_REGION

func createMockResources(t *testing.T, svc *elb.ELB, elbName string) (elbFQDN string) {

	resourceTags := []*elb.Tag{
		{
			Key:   aws.String("Name"),
			Value: aws.String(elbName),
		},
	}

	input := &elb.CreateLoadBalancerInput{
		Listeners: []*elb.Listener{
			{
				InstancePort:     aws.Int64(80),
				InstanceProtocol: aws.String("HTTP"),
				LoadBalancerPort: aws.Int64(80),
				Protocol:         aws.String("HTTP"),
			},
		},
		LoadBalancerName: aws.String(elbName),
		// Would be fun to looks this up, but the issue we need to know vpc-id anyway
		Subnets: []*string{
			aws.String("SUBNET_ID_HERE"),
		},
		Scheme: aws.String("internal"),
		Tags:   resourceTags,
	}

	logger.Log(t, "Creating mock resources: ELB with the following inputs:\n", input)

	resp, _ := svc.CreateLoadBalancer(input)

	assert.NotEmpty(t, *resp.DNSName, "DNS Name of created ELB must not be empty.")

	return *resp.DNSName
}
func TestR53Example(t *testing.T) {

	// Generate a unique suffix to use for test resources
	uniqueId := random.UniqueId()
	elbName := fmt.Sprintf("terratest-elb-%s", uniqueId)

	sess, err := session.NewSession()
	if err != nil {
		fmt.Println("failed to create session,", err)
		return
	}

	svc := elb.New(sess)
	elbFQDN := createMockResources(t, svc, elbName)
}

Possible Solution

Bug does not occur if I do the following:

export AWS_REGION = us-east-1

Additional Information/Context

// go.mod

module REDACTED_REPO_NAME

go 1.17

require (
	github.com/aws/aws-sdk-go v1.40.56
	github.com/gruntwork-io/terratest v0.40.7
	github.com/stretchr/testify v1.7.0
)

require (
	cloud.google.com/go v0.83.0 // indirect
	cloud.google.com/go/storage v1.10.0 // indirect
	github.com/agext/levenshtein v1.2.3 // indirect
	github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
	github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
	github.com/boombuler/barcode v1.0.1-0.20190219062509-6c824513bacc // indirect
	github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect
	github.com/davecgh/go-spew v1.1.1 // indirect
	github.com/docker/spdystream v0.0.0-20181023171402-6480d4af844c // indirect
	github.com/go-errors/errors v1.0.2-0.20180813162953-d98b870cc4e0 // indirect
	github.com/go-logr/logr v0.2.0 // indirect
	github.com/go-sql-driver/mysql v1.4.1 // indirect
	github.com/gogo/protobuf v1.3.2 // indirect
	github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
	github.com/golang/protobuf v1.5.2 // indirect
	github.com/golang/snappy v0.0.3 // indirect
	github.com/google/gofuzz v1.1.0 // indirect
	github.com/google/uuid v1.2.0 // indirect
	github.com/googleapis/gax-go/v2 v2.0.5 // indirect
	github.com/googleapis/gnostic v0.4.1 // indirect
	github.com/gruntwork-io/go-commons v0.8.0 // indirect
	github.com/hashicorp/errwrap v1.0.0 // indirect
	github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
	github.com/hashicorp/go-getter v1.5.9 // indirect
	github.com/hashicorp/go-multierror v1.1.0 // indirect
	github.com/hashicorp/go-safetemp v1.0.0 // indirect
	github.com/hashicorp/go-version v1.3.0 // indirect
	github.com/hashicorp/hcl/v2 v2.9.1 // indirect
	github.com/hashicorp/terraform-json v0.13.0 // indirect
	github.com/imdario/mergo v0.3.11 // indirect
	github.com/jinzhu/copier v0.0.0-20190924061706-b57f9002281a // indirect
	github.com/jmespath/go-jmespath v0.4.0 // indirect
	github.com/json-iterator/go v1.1.11 // indirect
	github.com/jstemmer/go-junit-report v0.9.1 // indirect
	github.com/klauspost/compress v1.13.0 // indirect
	github.com/mattn/go-zglob v0.0.2-0.20190814121620-e3c945676326 // indirect
	github.com/miekg/dns v1.1.31 // indirect
	github.com/mitchellh/go-homedir v1.1.0 // indirect
	github.com/mitchellh/go-testing-interface v1.0.0 // indirect
	github.com/mitchellh/go-wordwrap v1.0.1 // indirect
	github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
	github.com/modern-go/reflect2 v1.0.1 // indirect
	github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
	github.com/pmezard/go-difflib v1.0.0 // indirect
	github.com/pquerna/otp v1.2.0 // indirect
	github.com/russross/blackfriday/v2 v2.1.0 // indirect
	github.com/spf13/pflag v1.0.5 // indirect
	github.com/tmccombs/hcl2json v0.3.3 // indirect
	github.com/ulikunitz/xz v0.5.8 // indirect
	github.com/urfave/cli v1.22.2 // indirect
	github.com/zclconf/go-cty v1.9.1 // indirect
	go.opencensus.io v0.23.0 // indirect
	golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a // indirect
	golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 // indirect
	golang.org/x/mod v0.4.2 // indirect
	golang.org/x/net v0.0.0-20210614182718-04defd469f4e // indirect
	golang.org/x/oauth2 v0.0.0-20210514164344-f6687ab2804c // indirect
	golang.org/x/sys v0.0.0-20210603125802-9665404d3644 // indirect
	golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 // indirect
	golang.org/x/text v0.3.6 // indirect
	golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e // indirect
	golang.org/x/tools v0.1.2 // indirect
	golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
	google.golang.org/api v0.47.0 // indirect
	google.golang.org/appengine v1.6.7 // indirect
	google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c // indirect
	google.golang.org/grpc v1.38.0 // indirect
	google.golang.org/protobuf v1.26.0 // indirect
	gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
	gopkg.in/inf.v0 v0.9.1 // indirect
	gopkg.in/yaml.v2 v2.4.0 // indirect
	gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
	k8s.io/api v0.20.6 // indirect
	k8s.io/apimachinery v0.20.6 // indirect
	k8s.io/client-go v0.20.6 // indirect
	k8s.io/klog/v2 v2.4.0 // indirect
	k8s.io/utils v0.0.0-20201110183641-67b214c5f920 // indirect
	sigs.k8s.io/structured-merge-diff/v4 v4.0.3 // indirect
	sigs.k8s.io/yaml v1.2.0 // indirect
)

SDK version used

v1.40.56

Environment details (Version of Go (go version)? OS name and version, etc.)

go1.17.8 darwin/amd64, macOS 12.3.1

geekifier avatar Apr 20 '22 16:04 geekifier

Hi @geekifier thanks for bringing this to our attention. I have reproduced this issue and will discuss with the team.

vudh1 avatar Jun 13 '22 18:06 vudh1