Blocks deprecated with the TF Plugin Framework?

Hi there,

I’m developing a new TF provider, and in the Schemas documentation I didn’t find a way to define a block like this:

data "myprovider_foo" "example" {
  foo {
    a = "one"
    b = "two"
  }
} 

The only documentation I found for Blocks is in the Migrating from SDK - Blocks, but then, I cannot define a nested Map like this:

data "myprovider_foo" "example" {
  foo {
    alpha = {
      a = "one"
      b = "two"
    }
    beta = {
      a = "three"
      b = "four"
    }
    ...
  }
} 

The only working solution I found is this:

func (d *MyDataSource) GetSchema(ctx context.Context) (tfsdk.Schema, diag.Diagnostics) {
	return tfsdk.Schema{
		Attributes: map[string]tfsdk.Attribute{
			"foo": {
				Optional:            true,
				Attributes: tfsdk.MapNestedAttributes(map[string]tfsdk.Attribute{
					"a": {
						Type:     types.StringType,
						Optional: true,
					},
					"b": {
						Type:     types.StringType,
						Optional: true,
					},
				}),
			},
		},
...

and to use it:

data "myprovider_foo" "example" {
  foo = {  <-- REQUIRES an `=` character
    alpha = {
      a = "one"
      b = "two"
    }
    beta = {
      a = "three"
      b = "four"
    }
    ...
  }
} 

Question: Is the Block deprecated with the TF Plugin Framework, and for new TF providers, should we favor more tfsdk.MapNestedAttributes() instead?

Hi @samuel-phan :wave: Thank you for raising this and apologies for the delayed response.

Blocks are supported in terraform-plugin-framework, however there’s a little bit of history surrounding their implementation which is why they were not documented as well in the website documentation until recently. There is no deprecation intentions for blocks at this time. The current documentation page for blocks can be found here: Plugin Development - Framework: Handling Data - Blocks | Terraform | HashiCorp Developer

Block support is generally suggested for migrating existing terraform-plugin-sdk providers which used that schema feature. Nested attributes are recommended for newer schema implementation as they are more practitioner-friendly. Blocks require using dynamic block syntax to specify whole blocks from other Terraform data while nested attributes can use any Terraform expression (directly set to lists/sets/objects as appropriate or for expressions).

1 Like

Hi @bflad , thanks for your answer! It clarified everything! :pray:

Since I’m developing a new TF provider, I guess the nested attributes are the recommended way then :sunglasses: