Open woranhun opened 11 hours ago
Voting for Prioritization
Volunteering to Work on This Issue
@woranhun seems to me a normal behavior of the EC2 API. https://docs.aws.amazon.com/ec2/latest/devguide/eventual-consistency.html
@alexbacchin Can you elaborate on why do you think is it a normal behavior? I think, It is the same issue as happened before with RDS (https://github.com/hashicorp/terraform-provider-aws/issues/38251). And also this behavior causes issues with managing AWS resources from Crossplane (https://github.com/crossplane-contrib/provider-upjet-aws/issues/1482)
@woranhun When you CTRL+C Terraform, but the AWS API action is successfully received, AWS control plane will execute the action and the Terraform state will have no record of the resource been successfully created. Thus, it will try again in the subsequent apply, as there is already a subnet with the same CIDR, you get an error.
@alexbacchin Yes, but If a resource creation was triggered from TF, then TF should be aware that the resource exists, because it was created earlier by itself.
output, err := conn.CreateSubnet(ctx, input)
if err != nil {
return sdkdiag.AppendErrorf(diags, "creating EC2 Subnet: %s", err)
}
d.SetId(aws.ToString(output.Subnet.SubnetId))
For example in this case: The resource was created in AWS AND err is not nil (for whatever reason), then the SubnetId is lost forever (because of the return).
My plan is to move d.SetId(aws.ToString(output.Subnet.SubnetId))
line above to the error check.
Terraform Core Version
v1.9.7
AWS Provider Version
5.72.0
Affected Resource(s)
Expected Behavior
If a resource is created on AWS side, then it should not be leaked.
Actual Behavior
This is can happen because
conn.CreateSubnet
returns earlier during an error and therefore thesubnedId
is not saved. https://github.com/hashicorp/terraform-provider-aws/blob/12339906a1803312219d9bfd257c025aaad05513/internal/service/ec2/vpc_subnet.go#L197Relevant Error/Panic Output Snippet
Terraform Configuration Files
Steps to Reproduce
Create the terraform resource
Terraform apply the resource and after a few seconds CTRL+C the execution. (This can also happen, when for any reason the
conn.CreateSubnet(ctx, input)
returns with an error. ) Apply it again and a resource creation fails, because a subnet with the CIDR already exists. The subnet-id was leaked during the first creation.This is can happen because
conn.CreateSubnet
returns earlier during an error and therefore thesubnedId
is not saved. https://github.com/hashicorp/terraform-provider-aws/blob/12339906a1803312219d9bfd257c025aaad05513/internal/service/ec2/vpc_subnet.go#L197Debug Output
No response
Panic Output
No response
Important Factoids
We have seen issues with only the 4 resources I mentioned before. I think because these are the ones we create in high quantity. However, I think other resources might be affected as well...
References
relates to: https://github.com/crossplane-contrib/provider-upjet-aws/issues/1482 relates to: https://github.com/hashicorp/terraform-provider-aws/issues/38251
Would you like to implement a fix?
Yes