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.6.x
- v1.5.x
- v1.4.x
- v1.3.x
- v1.2.x
- v1.1.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
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.Schemastructs in theSchemafield of your resource's schema. In the Framework, attributes are defined by a map from attribute names toschema.Attributeimplementations in your resource's schema, which is returned by the resourceSchemamethod. - 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) {
return schema.Schema{
/* ... */
Attributes: map[string]schema.Attribute{
"example_attribute": schema.StringAttribute{
Required: true,
},
/* ... */