Terraform
Attribute Schema
Attributes define how users can configure values for your Terraform provider, resources, and data sources. Refer to Schemas - Attributes in the Framework documentation for details.
This page explains how to migrate an attribute from SDKv2 to the plugin Framework.
SDKv2
In SDKv2, attributes are defined by the Schema
field in the provider, resource, or data source schema. The Schema
field maps each attribute name (string) to the attribute's schema.Schema
struct. Both resources and data sources are
defined using the schema.Resource
struct.
The following code shows a basic implementation of attribute schema for a provider in SDKv2.
func ProviderExample() *schema.Provider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
/* ... */
},
In SDKv2, resource and data source attributes are defined the same way on their respective types.
func resourceExample() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
/* ... */
Framework
In the Framework, you define attributes by setting the Attributes
field on your provider, resource, or data type's
schema, as returned by the Schema
method. The schema.Schema
type returned by SchemaResponse
includes an
Attributes
field that maps each attribute name (string) to the attribute's definition.
The following code shows how to define an attribute for a resource with the Framework.
func (r *ThingResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"example": /* ... */
Migration Notes
Remember the following differences between SDKv2 and the Framework when completing the migration.
- In SDKv2, attributes are defined by a map from attribute names to
schema.Schema
structs in theSchema
field of your resource's schema. In the Framework, attributes are defined by a map from attribute names toschema.Attribute
implementations in your resource's schema, which is returned by the resourceSchema
method. - In SDKv2, the computed string
id
attribute was implicitly included in the schema. In the Framework, it must be explicitly defined in the schema. - There are several differences between the implementation of attributes in SDKv2 and the Framework. Refer to the other pages in the Attributes & Blocks section of this migration guide for more details.
Example
SDKv2
The following example shows the implementation of the example_attribute
attribute for the exampleDataSource
data source.
func exampleDataSource() *schema.Resource {
return &schema.Resource{
/* ... */
Schema: map[string]*schema.Schema{
"example_attribute": {
Type: schema.TypeString,
Required: true,
},
Framework
The following shows the same section of provider code after the migration.
This code implements the example_attribute
attribute for the exampleDataSource
data source with the Framework.
func (d *exampleDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
/* ... */
Attributes: map[string]schema.Attribute{
"example_attribute": schema.StringAttribute{
Required: true,
},
/* ... */