Terraform
Migrating attribute fields
This page explains how to migrate attribute fields to the framework. In SDKv2, attribute fields define an attribute's behavior, such as indicating that an attribute is required or optional. These fields are implemented in various ways in the framework.
Background
A subset of attribute fields, such as required, optional, computed, or sensitive, define attribute behavior as boolean flags. Refer to Schemas - Attributes in the framework documentation for details.
Migrating schema fields
The following table describes the mapping between SDK Schema Fields and the framework.
| SDK Schema Field | Framework | 
|---|---|
| Type | Attribute Types | 
| ConfigMode | Schema must be explictly defined using Attributes and Blocks | 
| Required | Requiredboolean field on attribute | 
| Optional | Optionalboolean field on attribute | 
| Computed | Computedboolean field on attribute | 
| ForceNew | RequiresReplace on PlanModifiersfield on attribute or implementation of ResourceWithModifyPlan interface | 
| DiffSuppressFunc | Custom Types, PlanModifiers field on attribute, or implementation of ResourceWithModifyPlan interface | 
| DiffSuppressOnRefresh | Custom Types semantic equality logic or manual logic in Read method on resource | 
| Default | Defaultfield on attribute using one of the predefined Defaults or implementing one of theschemapackage interfaces | 
| DefaultFunc | Defaultfield on attribute using one of the predefined Defaults or implementing one of theschemapackage interfaces | 
| Description | Descriptionfield on attribute | 
| InputDefault | N/A - no longer valid | 
| StateFunc | Requires implementation of bespoke logic before storing state, for instance in resource Create method | 
| Elem | ElementTypeon ListAttribute, MapAttribute or SetAttribute. Refer to Blocks ifschema.Resourceis present inElem. | 
| MaxItems | Use listValidator.SizeAtMost, mapvalidator.SizeAtMost or setvalidator.SizeAtMost on Validatorsfield on list, map or set attribute | 
| MinItems | Use listValidator.SizeAtLeast, mapvalidator.SizeAtLeast or setvalidator.SizeAtLeast on Validatorsfield on list, map or set attribute | 
| Set | N/A - no implementation required | 
| ComputedWhen | N/A - no longer valid | 
| ConflictsWith | Predefined Validators | 
| ExactlyOneOf | Predefined Validators | 
| AtLeastOneOf | Predefined Validators | 
| RequiredWith | Predefined Validators | 
| Deprecated | DeprecationMessagefield on attribute | 
| ValidateFunc | Predefined Validators, Custom Validators, or Custom Types validation logic | 
| ValidateDiagFunc | Predefined Validators, Custom Validators, or Custom Types validation logic | 
| Sensitive | Sensitivefield on attribute | 
Migrating boolean fields
Migrate required, optional, computed, and sensitive attribute fields from SDKv2 to the framework. Refer to the table above to migrate other kinds of fields.
In SDKv2, Required, Optional, Computed, and Sensitive are boolean fields
on the attribute's schema.
In the framework, you set the same fields on the schema.Attribute
implementation, with the same behavior as the corresponding fields from SDKv2.
The following code block shows implementations of these fields for a resource
attribute using the schema.Schema struct with SDKv2.
SDKv2
func resourceExample() *schema.Resource {
    return &schema.Resource{
         /* ... */
        Schema: map[string]*schema.Schema{
            "attribute_example": {
                Required:    bool
                Optional:    bool
                Computed:    bool
                Sensitive:   bool
                /* ... */
The following shows the same section of provider code after the migration.
Framework
func (r *ThingResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
    resp.Schema = schema.Schema{
        /* ... */
        Attributes: map[string]schema.Attribute{
            "attribute_example": schema.XXXAttribute{
                Required:   bool
                Optional:   bool
                Computed:   bool
                Sensitive:  bool
                /* ... */
Examples
Required fields
The following example shows how the example_attribute attribute on the
exampleDataSource data source is set to be required with SDKv2.
SDKv2
func exampleDataSource() *schema.Resource {
    return &schema.Resource{
        Schema: map[string]*schema.Schema{
            "example_attribute": {
                Required:   true,
                /* ... */
            },
            /* ... */
The following example shows how the example_attribute attribute on the
exampleDataSource data source is set to be required with the framework.
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,
                /* ... */
            },
            /* ... */