Terraform
Schema
Providers, resources, and data sources all use schema to define their attributes and behavior. Schemas specify the constraints of Terraform configuration blocks and how the provider, resource, or data source behaves. Refer to Schemas in the Framework documentation for details.
This page explains the differences between the schema used by SDKv2 and the Framework. We also recommend reviewing these additional schema guides throughout the migration: /attributes-blocks/
- Attributes where the schema defines practitioner or provider data associated with a value and type.
- Attribute types where the schema defines the expected data structure and syntax.
- Attribute fields where the behaviors of an attribute are defined, such as
Required
,Optional
,Computed
, andSensitive
. - Attribute defaults where the schema defines a value for an attribute which should be automatically included in a Terraform plan if it is not configured by the practitioner.
- Attributes without in-place updates where the schema defines an attribute that requires resource replacement if the value is updated.
- Attribute predefined validations and custom validations where the schema defines the syntax, constraints, or encoding expectations of a value.
- Blocks and computed blocks where the schema defines structural configuration sections of data, typically with nested attributes or further blocks.
Schema Structs
SDKv2 uses schema.Schema
structs to define the structure, type, and behavior of values drawn from configuration,
state, or plan data. The same schema.Schema
struct type is used for providers, resources, and data sources. The
schema struct is returned by the function that creates the provider, resource, or data source in question.
The Framework uses schema.Schema
structs for providers, resources, and data sources. The schema struct is returned by
a Schema
method you define for the provider and each resource type and data source type. Refer to
Framework for details.
SDKv2
The following code shows basic implementations using schema.Schema
structs to define schemas for providers, resources,
and data sources with SDKv2.
func New() *schema.Provider {
return &schema.Provider{
Schema: map[string]*schema.Schema{},
/* ... */
}
}
func resourceExample() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{},
/* ... */
}
}
func dataSourceExample() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{},
/* ... */
}
}
SDKv2 defines the schema.Schema
struct as follows.
type Schema struct {
Type ValueType
ConfigMode SchemaConfigMode
Required bool
Optional bool
Computed bool
ForceNew bool
DiffSuppressFunc SchemaDiffSuppressFunc
DiffSuppressOnRefresh bool
Default interface{}
DefaultFunc SchemaDefaultFunc
Description string
StateFunc SchemaStateFunc
Elem interface{}
MaxItems int
MinItems int
Set SchemaSetFunc
ConflictsWith []string
ExactlyOneOf []string
AtLeastOneOf []string
RequiredWith []string
Deprecated string
ValidateFunc SchemaValidateFunc
ValidateDiagFunc SchemaValidateDiagFunc
Sensitive bool
}
Framework
In the Framework, you implement Schema
method for your provider, resources, and data sources. This function is
required by the provider.Provider
, resource.Resource
, and datasource.DataSource
interfaces, respectively.
The following code shows how you define the Schema
method for your provider, resources, and data sources.
func (p *ExampleCloudProvider) Schema(ctx context.Context, req provider.SchemaRequest, resp *provider.SchemaResponse) {
resp.Schema = schema.Schema{/* ... */}
}
func (r *resourceExample) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{/* ... */}
}
func (r *dataSourceExample) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{/* ... */}
}
You use the Attributes
field to define attributes for your provider, resources, and data sources. You use the
Blocks
field to define named blocks.
Migration Notes
Remember the following differences between SDKv2 and the Framework when completing the migration.
- SDKv2 uses
schema.Schema
structs to define the provider, resources, and data sources. The Framework uses concept-specificschema.Schema
structs instead. - In SDKv2, schema structs are returned when a provider, resource, or data type is created. In the Framework, the
provider and each resource and data type have a
Schema
method that returns the schema. - In SDKv2, schema structs have a
Set
field which can be populated with aSchemaSetFunc
which is used for hashing. In the Framework, this is not required and does not need to be migrated. - The
schema.Schema
struct includes fields that you use to define attributes and blocks for your provider and each resource and data source. - When you populate the
Version
field inschema.Schema
for a resource in the Framework, copy theVersion
field inschema.Schema
from the SDKv2 version of that resource.