Terraform
- Plugin Framework
- v1.16.x (latest)
- No versions of this document exist before v1.15.x. Click below to redirect to the version homepage.
- v1.15.x
- v1.14.x
- v1.13.x
- v1.12.x
- v1.11.x
- v1.10.x
- v1.9.x
- v1.8.x
- v1.7.x
- v1.5.x
- v1.4.x
- v1.3.x
- v1.2.x
- v1.1.x
- 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
Default Values
Default values support is only available in the Framework for resources. Handle default values for data source attributes within the data source Read method and default values for provider attributes within the provider Configure method.
Default values set a value for an attribute when the Terraform configuration does not provide one. In SDKv2 and the
Framework default values are set via the Default field on an attribute's schema.
Refer to
Default
in the Framework documentation for details.
This page explains how to migrate attribute defaults in SDKv2 to the Framework.
SDKv2
In SDKv2, default values are defined for a primitive attribute type (i.e., TypeBool, TypeFloat, TypeInt,
TypeString) by the Default field on the attribute's schema. Alternatively, the DefaultFunc function is used to
compute a default value for an attribute.
The following code shows a basic implementation of a default value for a primitive attribute type in SDKv2.
func resourceExample() *schema.Resource {
return &schema.Resource{
/* ... */
Schema: map[string]*schema.Schema{
"attribute_example": {
Default: 2048,
/* ... */
},
/* ... */
Framework
In the Framework, you set default values with the Default field on your attribute's definition.
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
DefaultorDefaultFuncfields on an attribute'sschema.Schemastruct. In the Framework, you must assign set theDefaultfield on an attribute to set a default value.
Example
SDKv2
The following example shows the implementation of the Default field for the
example_attribute attribute on the exampleResource resource with SDKv2.
func exampleResource() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"example_attribute": {
Default: 2048,
/* ... */
},
/* ... */
Framework
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.
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
}