From 3abf77220c6c389fa74265c0ebf2bbf9760191e7 Mon Sep 17 00:00:00 2001 From: Fabian Ponce Date: Thu, 15 Jun 2023 11:31:55 -0400 Subject: [PATCH 1/4] aws_eip: use retry logic when creating new resources --- internal/service/ec2/ec2_eip.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/internal/service/ec2/ec2_eip.go b/internal/service/ec2/ec2_eip.go index eac9a8e1649..a9c63c9fb06 100644 --- a/internal/service/ec2/ec2_eip.go +++ b/internal/service/ec2/ec2_eip.go @@ -202,7 +202,10 @@ func resourceEIPRead(ctx context.Context, d *schema.ResourceData, meta interface return sdkdiag.AppendErrorf(diags, `with the retirement of EC2-Classic %s domain EC2 EIPs are no longer supported`, ec2.DomainTypeStandard) } - address, err := FindEIPByAllocationID(ctx, conn, d.Id()) + res, err := tfresource.RetryWhenNewResourceNotFound(ctx, propagationTimeout, func() (interface{}, error) { + return FindEIPByAllocationID(ctx, conn, d.Id()) + }, d.IsNewResource()) + address := res.(*ec2.Address) if !d.IsNewResource() && tfresource.NotFound(err) { log.Printf("[WARN] EC2 EIP (%s) not found, removing from state", d.Id()) From 236bbdaf0c258e2aaeda96ef7c5ba8bbad749e21 Mon Sep 17 00:00:00 2001 From: Fabian Ponce Date: Thu, 15 Jun 2023 12:02:38 -0400 Subject: [PATCH 2/4] fix crash due to cast prior to error check --- internal/service/ec2/ec2_eip.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/internal/service/ec2/ec2_eip.go b/internal/service/ec2/ec2_eip.go index a9c63c9fb06..b2a0f0f7387 100644 --- a/internal/service/ec2/ec2_eip.go +++ b/internal/service/ec2/ec2_eip.go @@ -205,7 +205,6 @@ func resourceEIPRead(ctx context.Context, d *schema.ResourceData, meta interface res, err := tfresource.RetryWhenNewResourceNotFound(ctx, propagationTimeout, func() (interface{}, error) { return FindEIPByAllocationID(ctx, conn, d.Id()) }, d.IsNewResource()) - address := res.(*ec2.Address) if !d.IsNewResource() && tfresource.NotFound(err) { log.Printf("[WARN] EC2 EIP (%s) not found, removing from state", d.Id()) @@ -217,6 +216,8 @@ func resourceEIPRead(ctx context.Context, d *schema.ResourceData, meta interface return sdkdiag.AppendErrorf(diags, "reading EC2 EIP (%s): %s", d.Id(), err) } + address := res.(*ec2.Address) + d.Set("allocation_id", address.AllocationId) d.Set("association_id", address.AssociationId) d.Set("carrier_ip", address.CarrierIp) From 3c1fa114eff111b5ff3d68f0d5ec6304051f1fef Mon Sep 17 00:00:00 2001 From: Fabian Ponce Date: Thu, 15 Jun 2023 12:07:30 -0400 Subject: [PATCH 3/4] add changelog --- .changelog/32016.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/32016.txt diff --git a/.changelog/32016.txt b/.changelog/32016.txt new file mode 100644 index 00000000000..5537436b70b --- /dev/null +++ b/.changelog/32016.txt @@ -0,0 +1,3 @@ +```release-note:bug +resource/aws_eip: Fix `reading EC2 EIP (eipalloc-abcd1234): couldn't find resource` errors when reading new resource +``` From 0f0f6b989c093279adf7db2ad42623103a528737 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Fri, 16 Jun 2023 09:44:58 -0400 Subject: [PATCH 4/4] Cosmetics. --- internal/service/ec2/ec2_eip.go | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/internal/service/ec2/ec2_eip.go b/internal/service/ec2/ec2_eip.go index b2a0f0f7387..5f0a8839a7e 100644 --- a/internal/service/ec2/ec2_eip.go +++ b/internal/service/ec2/ec2_eip.go @@ -202,7 +202,7 @@ func resourceEIPRead(ctx context.Context, d *schema.ResourceData, meta interface return sdkdiag.AppendErrorf(diags, `with the retirement of EC2-Classic %s domain EC2 EIPs are no longer supported`, ec2.DomainTypeStandard) } - res, err := tfresource.RetryWhenNewResourceNotFound(ctx, propagationTimeout, func() (interface{}, error) { + outputRaw, err := tfresource.RetryWhenNewResourceNotFound(ctx, propagationTimeout, func() (interface{}, error) { return FindEIPByAllocationID(ctx, conn, d.Id()) }, d.IsNewResource()) @@ -216,8 +216,7 @@ func resourceEIPRead(ctx context.Context, d *schema.ResourceData, meta interface return sdkdiag.AppendErrorf(diags, "reading EC2 EIP (%s): %s", d.Id(), err) } - address := res.(*ec2.Address) - + address := outputRaw.(*ec2.Address) d.Set("allocation_id", address.AllocationId) d.Set("association_id", address.AssociationId) d.Set("carrier_ip", address.CarrierIp) @@ -228,17 +227,15 @@ func resourceEIPRead(ctx context.Context, d *schema.ResourceData, meta interface d.Set("network_border_group", address.NetworkBorderGroup) d.Set("network_interface", address.NetworkInterfaceId) d.Set("public_ipv4_pool", address.PublicIpv4Pool) - d.Set("vpc", aws.StringValue(address.Domain) == ec2.DomainTypeVpc) - d.Set("private_ip", address.PrivateIpAddress) if v := aws.StringValue(address.PrivateIpAddress); v != "" { d.Set("private_dns", PrivateDNSNameForIP(meta.(*conns.AWSClient), v)) } - d.Set("public_ip", address.PublicIp) if v := aws.StringValue(address.PublicIp); v != "" { d.Set("public_dns", PublicDNSNameForIP(meta.(*conns.AWSClient), v)) } + d.Set("vpc", aws.StringValue(address.Domain) == ec2.DomainTypeVpc) // Force ID to be an Allocation ID if we're on a VPC. // This allows users to import the EIP based on the IP if they are in a VPC.