• HashiCorp Developer

  • HashiCorp Cloud Platform
  • Terraform
  • Packer
  • Consul
  • Vault
  • Boundary
  • Nomad
  • Waypoint
  • Vagrant
Terraform
  • Install
  • Tutorials
    • About the Docs
    • Configuration Language
    • Terraform CLI
    • Terraform Cloud
    • Terraform Enterprise
    • CDK for Terraform
    • Provider Use
    • Plugin Development
    • Registry Publishing
    • Integration Program
  • Registry(opens in new tab)
  • Try Cloud(opens in new tab)
  • Sign up
Plugin Development

Framework

Skip to main content
  • Framework
  • Provider Servers
  • Returning Errors and Warnings
  • Validation
  • Acceptance Tests
  • Debugging
    • Overview
    • Benefits
    • Testing
      • Overview
      • Timeouts

  • Resources

  • Tutorial Library
  • Certifications
  • Community Forum
    (opens in new tab)
  • Support
    (opens in new tab)
  • GitHub
    (opens in new tab)
  • Terraform Registry
    (opens in new tab)
  1. Developer
  2. Terraform
  3. Plugin Development
  4. Framework
  5. Migrating from SDK
  6. Data Sources
  7. Timeouts
  • Plugin Framework
  • v1.0.x
  • v0.17.x
  • v0.16.x
  • v0.15.x
  • v0.14.x
  • v0.13.x
  • v0.12.x
  • v0.11.x
  • v0.10.x
  • v0.9.x
  • v0.8.x
  • v0.7.x

»Timeouts

The Framework can be used in conjunction with the terraform-plugin-framework-timeouts module in order to allow defining timeouts in configuration and have them be available in Read functions.

Specifying Timeouts in Configuration

Timeouts can be defined using either nested blocks or nested attributes.

If you are writing a new provider using terraform-plugin-framework then we recommend using nested attributes.

If you are migrating a provider from SDKv2 to the Framework and you are already using timeouts you can either continue to use block syntax, or switch to using nested attributes. However, switching to using nested attributes will require that practitioners that are using your provider update their Terraform configuration.

Block

If your configuration is using a nested block to define timeouts, such as the following:

resource "timeouts_example" "example" {
  /* ... */

  timeouts {
    read = "60m"
  }
}

You can use this module to mutate the schema.Schema as follows:

func (d *ThingDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
    resp.Schema = schema.Schema{
        /* ... */

        Blocks: map[string]schema.Block{
            "timeouts": timeouts.Block(ctx, timeouts.Opts{
                Read: true,
            }),
        },

Attribute

If your configuration is using nested attributes to define timeouts, such as the following:

resource "timeouts_example" "example" {
  /* ... */

  timeouts = {
    read = "60m"
  }
}

You can use this module to mutate the schema.Schema as follows:

func (d *ThingDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
    resp.Schema = schema.Schema{
        Attributes: map[string]schema.Attribute{
            /* ... */
            "timeouts": timeouts.Attributes(ctx, timeouts.Opts{
                Read: true,
            }),
        },

Updating Models

Given a Read method which fetches the entire configuration:

func (e *exampleDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
    var data exampleDataSourceData

    diags := req.Config.Get(ctx, &data)
    resp.Diagnostics.Append(diags...)

Modify the exampleDataSourceData model to include a field for timeouts using a types.Object type.

type exampleDataSourceData struct {
    /* ... */
    Timeouts    types.Object `tfsdk:"timeouts"`

Accessing Timeouts in CRUD Functions

Call the timeouts.Read() function.

func (e *exampleDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
    var data exampleDataSourceData

    diags := req.Config.Get(ctx, &data)
    resp.Diagnostics.Append(diags...)
    if resp.Diagnostics.HasError() {
        return
    }

    defaultReadTimeout := 20 * time.Minute

    readTimeout := timeouts.Read(ctx, data.Timeouts, defaultReadTimeout)

    ctx, cancel := context.WithTimeout(ctx, readTimeout)
    defer cancel()

    /* ... */
}
Edit this page on GitHub

On this page

  1. Timeouts
  2. Specifying Timeouts in Configuration
  3. Updating Models
  4. Accessing Timeouts in CRUD Functions
Give Feedback(opens in new tab)
  • Certifications
  • System Status
  • Terms of Use
  • Security
  • Privacy
  • Trademark Policy
  • Trade Controls
  • Give Feedback(opens in new tab)