Shall TF Provider handle 409 status code?

Viewed 63

Let's assume there's a existing resource foo with name bar and then there's a TF Provider for creating these resources so a user tries to create

resource "foo" "example" {
   name = "bar"
}

and when a user runs terraform apply, they receive 409 status code (after an internal POST call from TF Provider) since such a resource exists already.

Is it a thing for TF Provider to handle it automatically and figure out that there's the exact same resource already or it's OK to fail with 409 to let a user figure it out manually?

1 Answers

From a programming point of view, it is enough to return any error as diag.Diagnostics, see the tutorial how to implement the create function.

From a compliance point of view, you only need to add documentation when uploading your provider to Terraform Registry. There are no restrictions on how useful your error messages should be.

But in general, it is recommended to give the user the best feedback possible. This is especially true when you as the provider programmer only have to program this logic once to save each of your (hopefully) many provider consumers this thought process each time they encounter a 409.

You can take for example the AWS Provider, which is mentioned by Hashicorp as reference provider for new provider developers. If you create a resource for an AWS S3 bucket that already exists, it gives you this error message:

Error creating S3 bucket: BucketAlreadyOwnedByYou: Your previous request to create the named bucket succeeded and you already own it.

If you instead try to create an S3 bucket that was already create by someone else (since names of S3 buckets need to be globally unique in AWS), then the error message states:

Error creating S3 bucket: BucketAlreadyExists: The requested bucket name is not available. The bucket namespace is shared by all users of the system. Please select a different name and try again.

Since terraform itself and at least all providers created by Hashicorp have very self-explanatory error message, I suggest you do the same.

Related