Terraform
Migrating attribute default values
This page explains how to migrate attribute defaults in SDKv2 to the framework.
Background
Default values set a value for an attribute when the Terraform configuration
does not provide one. Use the Default
field on an attribute's schema to
set default values in SDKv2 and the framework. Refer to
Default in the framework
documentation for details.
Default values support is only available in the framework for resource
attributes. Handle default values for data source attributes within the data
source Read
method and
default values for provider attributes within the provider Configure
method.
Migrate an attribute with a default value
In SDKv2, you define default values for a primitive attribute type by the Default
field on the attribute's schema.
These primitive attriutes include the TypeBool
, TypeFloat
, TypeInt
, TypeString
types.
Alternatively, the you can use the DefaultFunc
function to compute
a default value for an attribute.
In the framework, you set default values with the Default
field on your
attribute's definition.
The following code shows a basic implementation of a default value for a primitive attribute type in SDKv2.
SDKv2
func resourceExample() *schema.Resource {
return &schema.Resource{
/* ... */
Schema: map[string]*schema.Schema{
"attribute_example": {
Default: 2048,
/* ... */
},
/* ... */
The following shows the same section of provider code after the migration.
Framework
import (
"context"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault"
)
func (r *resourceExample) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
/* ... */
Attributes: map[string]schema.Attribute{
"attribute_example": schema.BoolAttribute{
Default: booldefault.StaticBool(true),
/* ... */
Migration Notes
Remember the following differences between SDKv2 and the framework when completing the migration.
- In SDKv2, default values are set with the
Default
orDefaultFunc
fields on an attribute'sschema.Schema
struct. In the framework, you must assign set theDefault
field on an attribute to set a default value.
Examples
The following examples demostrate how to migrate default values to the framework.
Resource attribute default values
Migrate a resource default value to the framework.
The following example shows the implementation of the Default
field for the
example_attribute
attribute on the exampleResource
resource with SDKv2.
SDKv2
func exampleResource() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"example_attribute": {
Default: 2048,
/* ... */
},
/* ... */
The following shows the same section of code after the migration.
This code implements the PlanModifiers
field for the example_attribute
attribute with the framework.
Framework
func (r *exampleResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"example_attribute": schema.Int64Attribute{
Default: int64default.StaticInt64(2048)
/* ... */
},
/* ... */
},
}, nil
}