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
- v1.0.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
Note: The Plugin Framework is in beta.
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
The following examples show how to migrate portions of the http provider.
For a complete example, clone the
terraform-provider-http repository and compare the data_source.go file in
v2.2.0
with the data_source_http.go file
after the migration.
SDKv2
The following example from the data_source.go file shows the implementation of the url attribute for the http
data source.
func dataSource() *schema.Resource {
    return &schema.Resource{
        /* ... */
        Schema: map[string]*schema.Schema{
            "url": {
                Description: "The URL for the request. Supported schemes are `http` and `https`.",
                Type:        schema.TypeString,
                Required:    true,
            },
Framework
The following shows the same section of provider code after the migration.
This code implements the url attribute for the http data source with the Framework.
func (d *httpDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
    return schema.Schema{
        /* ... */
        Attributes: map[string]schema.Attribute{
            "url": schema.StringAttribute{
                Description: "The URL for the request. Supported schemes are `http` and `https`.",
                Required:    true,
            },
            /* ... */