Terraform
Migrating computed blocks
This page explains how to migrate computed-only blocks from SDKv2 to the framework. Refer to Blocks if you are looking for information about migrating blocks that are practitioner configurable.
Background
Some providers, resources, and data sources include repeatable nested blocks in
their attributes. Some blocks contain fields with Computed: true
, which means
that the provider code can define the value or that it could come from the
output of terraform apply (e.g., the ID of an EC2 instance).
Migrating computed blocks in protocol version 5
If your provider uses protocol version 5, migrate computed blocks by using a list attribute whose elements are objects.
In SDKv2, blocks are defined by an attribute whose type is either TypeList
or
TypeSet
, and whose Elem
field is set to a schema.Resource
that contains a
map of the block's attribute names to corresponding schemaSchema
structs.
In the framework, when working with protocol version 5, implement computed
blocks by using a ListAttribute
which has an ElementType
of
types.ObjectType
.
SDKv2
map[string]*schema.Schema{
"example": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"nested_example": {
Type: schema.TypeString,
Computed: true,
/* ... */
The following example shows how the block is defined with the framework after the migration.
Framework
map[string]schema.Attribute{
"example": schema.ListAttribute{
Computed: true,
ElementType: types.ObjectType{
AttrTypes: map[string]attr.Type{
"nested_example": types.StringType,
/* ... */
Migrating computed blocks in protocol version 6
If your provider uses protocol version 6, migrate computed blocks by using nested attributes.
In SDKv2, blocks are defined by an attribute whose type is either TypeList
or
TypeSet
, and whose Elem
field is set to a schema.Resource
that contains a
map of the block's attribute names to corresponding schemaSchema
structs.
In the framework, when working with protocol version 6, we recommend that you
define computed blocks using nested attributes. This example shows usage of
ListNestedAttribute
as this provides configuration references with list index
syntax as is the case when using schema.TypeList
in SDKv2.
SingleNestedAttribute
is a good choice for single underlying objects which
results in a breaking change but also allows dropping [0]
in configuration
references. Remember to only introduce breaking changes to your provider in
major version
updates.
Framework
map[string]schema.Attribute{
"example": schema.ListNestedAttribute{
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"nested_example": schema.StringAttribute{
Computed: true,
/* ... */
Migration Notes
- When using protocol version 5 specify
ElementType
astypes.ObjectType
when migrating blocks that are computed from SDKv2 to the framework. - When using protocol version 6, use nested attributes when migrating blocks that are computed from SDKv2 to the framework.
Examples
Migrate a computed block
The following example shows the implementation of the example_nested_block
with SDKv2.
Schema: map[string]*schema.Schema{
"example_nested_block": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"example_block_attribute": {
Type: schema.TypeString,
Computed: true,
/* ... */
},