Reaching out to get more information around timeout behavior in Terraform Plugin SDK v2.
We have a requirement where in case a resource creation times out, we want to trigger another function.
To be specific we want to call the delete handler if resource creation times out. The timeout can be default amount of time or what may be specified by the user.
- Is this possible to implement if our plugin is currently using Terraform Plugin SDKv2?
- Does Terraform Plugin SDKv2 provide such a functionality out of the box? If not, are there any possible workarounds that you could recommend?
Follow up question: In case Plugin SDKv2 does not support this behavior out of the box, can this be achieved if we migrate to Terraform Plugin Framework?
The first question I would have in this case is whether you really want to use a timeout to trigger another function? The reason for asking is that if there is an error on resource creation then Terraform will automatically taint the resource. Consequently, re-running terraform apply will result in a delete-recreate cycle during the next execution. This also gives the practitioner the option to manually remediate the situation by un-tainting the resource.
If you do want to go the route of having logic triggered when a timeout occurs, then you should have access to the
context.Context in SDKv2 when using
CreateContext(), and in the Framework when using
Create(). So you can try and capture
<-ctx.Done and use this to branch into the logic that you want to execute if the create operation timesout.
Although this is not exactly what you asked for the SDKv2 documentation contains an example of using a Retry within a resource create function. The Framework documentation contains an example of Accessing Timeouts in CRUD Functions.
Thank you @bendbennett !
We need to use timeout as in certain specific cases our APIs tend to run indefinitely when creating a resource due to dependencies, and this leaves the customer waiting indefinitely and could potentially leave the TF state in an inconsistent state with actual resource should the user choose to abandon midway. Hope that helps.
Also could you please elaborate how could the Retry mechanism help in this case in SDKv2?
If you use a timeout and the create operation reaches this timeout and an error is generated then, as mentioned above, Terraform will automatically taint the resource. Consequently, re-running terraform apply will result in a delete-recreate cycle during the next execution.
The link to Retry was intended to illustrate how a timeout could be used in the context of SDKv2 to trigger the running of another function. In your use-case you should have access to
context.Context when using the
CreateContext() function so you can try and capture
<-ctx.Done and use this to branch into the logic that you want to execute if the create operation results in a timeout.
We will try to migrate to the Plugin Framework first and try out suggested approach. Thank you @bendbennett !