# Callback function at Timeout in Terraform Plugin SDKv2

**URL:** <https://discuss.hashicorp.com/t/callback-function-at-timeout-in-terraform-plugin-sdkv2/55462>\
**Category:** Plugin Development\
**Created:** [June 27, 2023, 9:05am UTC](https://discuss.hashicorp.com/t/callback-function-at-timeout-in-terraform-plugin-sdkv2/55462 "2023-06-27T09:05:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![maastha](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/maastha/32/36289_2.png) [@maastha](https://discuss.hashicorp.com/u/maastha)\
**Post date:** [June 27, 2023, 9:05am UTC](https://discuss.hashicorp.com/t/callback-function-at-timeout-in-terraform-plugin-sdkv2/55462/1 "2023-06-27T09:05:01Z")

</div>

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.

1. Is this possible to implement if our plugin is currently using Terraform Plugin SDKv2?
2. 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?

---

<div class="post-metadata">

**Author:** ![bendbennett](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/bendbennett/32/30764_2.png) [@bendbennett](https://discuss.hashicorp.com/u/bendbennett)\
**Post date:** [June 27, 2023, 2:14pm UTC](https://discuss.hashicorp.com/t/callback-function-at-timeout-in-terraform-plugin-sdkv2/55462/2 "2023-06-27T14:14:58Z")

</div>

Hi @maastha 👋

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](https://developer.hashicorp.com/terraform/plugin/sdkv2/resources/retries-and-customizable-timeouts#retry) within a resource create function. The Framework documentation contains an example of [Accessing Timeouts in CRUD Functions](https://developer.hashicorp.com/terraform/plugin/framework/resources/timeouts#accessing-timeouts-in-crud-functions).

---

<div class="post-metadata">

**Author:** ![maastha](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/maastha/32/36289_2.png) [@maastha](https://discuss.hashicorp.com/u/maastha)\
**Post date:** [June 28, 2023, 9:47am UTC](https://discuss.hashicorp.com/t/callback-function-at-timeout-in-terraform-plugin-sdkv2/55462/3 "2023-06-28T09:47:14Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![bendbennett](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/bendbennett/32/30764_2.png) [@bendbennett](https://discuss.hashicorp.com/u/bendbennett)\
**Post date:** [June 29, 2023, 1:01pm UTC](https://discuss.hashicorp.com/t/callback-function-at-timeout-in-terraform-plugin-sdkv2/55462/4 "2023-06-29T13:01:09Z")

</div>

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](https://developer.hashicorp.com/terraform/plugin/sdkv2/resources/retries-and-customizable-timeouts#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.

---

<div class="post-metadata">

**Author:** ![maastha](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/maastha/32/36289_2.png) [@maastha](https://discuss.hashicorp.com/u/maastha)\
**Post date:** [July 6, 2023, 3:19pm UTC](https://discuss.hashicorp.com/t/callback-function-at-timeout-in-terraform-plugin-sdkv2/55462/5 "2023-07-06T15:19:54Z")

</div>

We will try to migrate to the Plugin Framework first and try out suggested approach. Thank you @bendbennett !

---

<div class="post-metadata">

**Author:** ![wk0189713](https://avatars.discourse-cdn.com/v4/letter/w/e9a140/32.png) [@wk0189713](https://discuss.hashicorp.com/u/wk0189713)\
**Post date:** [November 6, 2023, 12:33pm UTC](https://discuss.hashicorp.com/t/callback-function-at-timeout-in-terraform-plugin-sdkv2/55462/10 "2023-11-06T12:33:10Z")

</div>

I’m currently dealing with a specific challenge related to managing timeouts in Terraform. In my infrastructure provisioning workflow, I need to handle resource creation timeouts and execute specific actions when a timeout occurs. While Terraform offers great flexibility, it doesn’t provide a built-in feature for handling timeouts and triggering custom actions in response. My primary question is whether there are any best practices or recommended workarounds for implementing timeout handling in Terraform, especially when [a](https://carsforyourhelp.com) resource creation process exceeds a certain time limit.
