-
Notifications
You must be signed in to change notification settings - Fork 9.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
r/aws_eip fix case when aws returns multiple eips from read request #5331
Conversation
aws/resource_aws_eip.go
Outdated
// Sometimes AWS returns EIPs that *aren't* the EIP we're looking for | ||
// so we loop over the returned addresses to see if it's in the list of results | ||
for _, addr := range describeAddresses.Addresses { | ||
if (domain == "vpc" && *addr.AllocationId == id) || *address.PublicIp == id { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This new logic can cause Terraform to crash due to a typo:
=== RUN TestAccAWSEIP_importEc2Classic
------- Stderr: -------
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x38 pc=0x1ec009b]
goroutine 215 [running]:
github.com/terraform-providers/terraform-provider-aws/aws.resourceAwsEipRead(0xc420a6f8f0, 0x307b000, 0xc420592c00, 0x2f3d4e0, 0xc420952600)
/opt/teamcity-agent/work/7be301907ad6c6ab/src/github.com/terraform-providers/terraform-provider-aws/aws/resource_aws_eip.go:189 +0x3bb
While we're fixing this, we should also switch the *
dereferencing to the SDK provided aws.StringValue()
function to further prevent panics:
if (domain == "vpc" && aws.StringValue(addr.AllocationId) == id) || aws.StringValue(addr.PublicIp) == id {
aws/resource_aws_eip.go
Outdated
} | ||
} | ||
|
||
address := describeAddresses.Addresses[0] | ||
if address == nil { | ||
return fmt.Errorf("Unable to find EIP: %#v", describeAddresses.Addresses) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of returning an error in these cases, we generally prefer to remove the resource from the Terraform state to trigger a resource recreation:
if address == nil {
log.Printf("[WARN] EIP %q not found, removing from state", d.Id())
d.SetId("")
return nil
}
@bflad sorry for the delay! Made the changes you requested. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great, thanks @rv-aburdine! 🚀
10 tests passed (all tests)
=== RUN TestAccAWSEIP_disappears
--- PASS: TestAccAWSEIP_disappears (5.74s)
=== RUN TestAccAWSEIP_basic
--- PASS: TestAccAWSEIP_basic (7.59s)
=== RUN TestAccAWSEIP_tags
--- PASS: TestAccAWSEIP_tags (12.45s)
=== RUN TestAccAWSEIP_network_interface
--- PASS: TestAccAWSEIP_network_interface (21.67s)
=== RUN TestAccAWSEIP_twoEIPsOneNetworkInterface
--- PASS: TestAccAWSEIP_twoEIPsOneNetworkInterface (22.13s)
=== RUN TestAccAWSEIP_importVpc
--- PASS: TestAccAWSEIP_importVpc (22.72s)
=== RUN TestAccAWSEIP_associated_user_private_ip
--- PASS: TestAccAWSEIP_associated_user_private_ip (108.01s)
=== RUN TestAccAWSEIP_importEc2Classic
--- PASS: TestAccAWSEIP_importEc2Classic (244.25s)
=== RUN TestAccAWSEIP_classic_disassociate
--- PASS: TestAccAWSEIP_classic_disassociate (253.92s)
=== RUN TestAccAWSEIP_instance
--- PASS: TestAccAWSEIP_instance (343.30s)
This has been released in version 1.30.0 of the AWS provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading. |
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks! |
Fixes #5330
Changes proposed in this pull request:
Output from acceptance testing:
Wasn't entirely sure how to add an acceptance test for this, given that the acceptance tests hit the AWS api, and the bug in question seems like an inconsistency in AWS's api rather than what terraform itself is doing. If there's a good way to test this via an acceptance test please let me know though 😄