• 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
    • Terraform Concepts
    • Schemas
    • Attributes
    • Blocks
    • Paths
    • Path Expressions
    • Accessing Terraform Data
    • Writing Data
    • Conversion Rules
    • Custom Types
  • Returning Errors and Warnings
  • Validation
  • Acceptance Tests
  • Debugging

  • 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. Handling Data
  6. Blocks
  • 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

»Blocks

The Terraform language uses a block as a container for other attributes and blocks. Terraform implements many top level blocks, such as provider and resource, while providers can implement nested blocks in their schema to enable practitioners to configure data.

Use nested attributes for new schema implementations. Block support is mainly for migrating prior SDK-based providers.

In this example, the Terraform-defined resource block contains a provider-defined ami attribute and network_interface block.

resource "aws_instance" "example" {
  ami = "abc123"

  network_interface {
    # ...
  }
}

The configuration syntax for a provider-defined block is a type (block name) and the body is delimited by the { and } characters. Each block has an associated nesting mode, which declares whether a block is repeatable. The available nesting modes are:

  • List: Ordered collection of objects
  • Set: Unordered collection of objects
  • Single: One object

For list and set blocks, configurations can implement dynamic block expressions to reduce hardcoded block values.

Terraform Configuration

Use the following syntax in Terraform configuration for nested blocks:

resource "example_resource" "example" {
  list_nested_block {
    bool_attribute    = true
    float64_attribute = 1234.5
    int64_attribute   = 9223372036854775807
    list_attribute    = ["list-element", "list-element"]
    list_nested_nested_block {
      bool_attribute = true
    }
    list_nested_nested_block {
      bool_attribute = false
    }
  }
  list_nested_block {
    bool_attribute    = true
    float64_attribute = 1234.5
    int64_attribute   = 9223372036854775807
    list_attribute    = ["list-element", "list-element"]
    list_nested_nested_block {
      bool_attribute = true
    }
    list_nested_nested_block {
      bool_attribute = false
    }
  }

  set_nested_block {
    map_attribute    = { "map-key-1" : "map-value-1" }
    number_attribute = 1.79769313486231570814527423731704356798070e+1000
    object_attribute = {
      bool_attribute    = true
      float64_attribute = 1234.5
      int64_attribute   = 9223372036854775807
      list_attribute    = ["obj-list-element", "obj-list-element"]
      map_attribute     = { "obj-map-key-1" : "obj-map-value-1" }
      number_attribute  = 1.79769313486231570814527423731704356798070e+1000
      set_attribute     = ["obj-set-element-1", "obj-set-element-2"]
      string_attribute  = "obj-string"
    }
    set_attribute = ["set-element-1", "set-element-2"]
    set_nested_nested_block {
      bool_attribute = true
    }
    set_nested_nested_block {
      bool_attribute = false
    }
  }
  set_nested_block {
    map_attribute    = { "map-key-1" : "map-value-1" }
    number_attribute = 1.79769313486231570814527423731704356798070e+1000
    object_attribute = {
      bool_attribute    = false
      float64_attribute = 1234.5
      int64_attribute   = 9223372036854775807
      list_attribute    = ["obj-list-element", "obj-list-element"]
      map_attribute     = { "obj-map-key-1" : "obj-map-value-1" }
      number_attribute  = 1.79769313486231570814527423731704356798070e+1000
      set_attribute     = ["obj-set-element-1", "obj-set-element-2"]
      string_attribute  = "obj-string"
    }
    set_attribute = ["set-element-1", "set-element-2"]
    set_nested_nested_block {
      bool_attribute = true
    }
    set_nested_nested_block {
      bool_attribute = false
    }
  }

  single_nested_block {
    bool_attribute    = true
    float64_attribute = 1234.5
  }
}

Schema

Define nested blocks in the schema as follows:

func (e *exampleResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
    resp.Schema = schema.Schema{
        Blocks: map[string]schema.Block{
            "list_nested_block": schema.ListNestedBlock{
                NestedObject: schema.NestedBlockObject{
                    Attributes: map[string]schema.Attribute{
                        "bool_attribute": schema.BoolAttribute{
                            Optional: true,
                        },
                        "float64_attribute": schema.Float64Attribute{
                            Optional: true,
                        },

                        "int64_attribute": schema.Int64Attribute{
                            Optional: true,
                        },
                        "list_attribute": schema.ListAttribute{
                            Optional:    true,
                            ElementType: types.StringType,
                        },
                    },
                    Blocks: map[string]schema.Block{
                        "list_nested_nested_block": schema.ListNestedBlock{
                            NestedObject: schema.NestedBlockObject{
                                Attributes: map[string]schema.Attribute{
                                    "bool_attribute": schema.BoolAttribute{
                                        Optional: true,
                                    },
                                },
                            },
                        },
                    },
                },
            },

            "set_nested_block": schema.SetNestedBlock{
                NestedObject: schema.NestedBlockObject{
                    Attributes: map[string]schema.Attribute{
                        "map_attribute": schema.MapAttribute{
                            Optional:    true,
                            ElementType: types.StringType,
                        },
                        "number_attribute": schema.NumberAttribute{
                            Optional: true,
                        },
                        "object_attribute": schema.ObjectAttribute{
                            Optional: true,
                            AttributeTypes: map[string]attr.Type{
                                "bool_attribute":    types.BoolType,
                                "float64_attribute": types.Float64Type,
                                "int64_attribute":   types.Int64Type,
                                "list_attribute":    types.ListType{ElemType: types.StringType},
                                "map_attribute":     types.MapType{ElemType: types.StringType},
                                "number_attribute":  types.NumberType,
                                "set_attribute":     types.ListType{ElemType: types.StringType},
                                "string_attribute":  types.StringType,
                            },
                        },
                        "set_attribute": schema.SetAttribute{
                            Optional:    true,
                            ElementType: types.StringType,
                        },
                    },
                    Blocks: map[string]schema.Block{
                        "set_nested_nested_block": schema.SetNestedBlock{
                            NestedObject: schema.NestedBlockObject{
                                Attributes: map[string]schema.Attribute{
                                    "bool_attribute": schema.BoolAttribute{
                                        Optional: true,
                                    },
                                },
                            },
                        },
                    },
                },
            },

            "single_nested_block": schema.SingleNestedBlock{
                Attributes: map[string]schema.Attribute{
                    "bool_attribute": schema.BoolAttribute{
                        Optional: true,
                    },
                    "float64_attribute": schema.Float64Attribute{
                        Optional: true,
                    },
                },
            },
        },
    }
}
Edit this page on GitHub

On this page

  1. Blocks
  2. Terraform Configuration
  3. Schema
Give Feedback(opens in new tab)
  • Certifications
  • System Status
  • Terms of Use
  • Security
  • Privacy
  • Trademark Policy
  • Trade Controls
  • Give Feedback(opens in new tab)