Terraform
Attribute Fields
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.
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 | Required field on attribute | |
Optional | Optional field on attribute | |
Computed | Computed field on attribute | |
ForceNew | RequiresReplace on PlanModifiers field 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 | Default field on attribute using one of the predefined Defaults or implementing one of the schema package interfaces | |
DefaultFunc | Default field on attribute using one of the predefined Defaults or implementing one of the schema package interfaces | |
Description | Description field on attribute | |
InputDefault | N/A - no longer valid | |
StateFunc | Requires implementation of bespoke logic before storing state, for instance in resource Create method | |
Elem | ElementType on ListAttribute, MapAttribute or SetAttribute. Refer to Blocks if schema.Resource is present in Elem . | |
MaxItems | Use listValidator.SizeAtMost, mapvalidator.SizeAtMost or setvalidator.SizeAtMost on Validators field on list, map or set attribute | |
MinItems | Use listValidator.SizeAtLeast, mapvalidator.SizeAtLeast or setvalidator.SizeAtLeast on Validators field 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 | DeprecationMessage field on attribute | |
ValidateFunc | Predefined Validators, Custom Validators, or Custom Types validation logic | |
ValidateDiagFunc | Predefined Validators, Custom Validators, or Custom Types validation logic | |
Sensitive | Sensitive field on attribute |
This page explains how to migrate the required, optional, computed, and sensitive attribute fields from SDKv2 to the Framework.
SDKv2
In SDKv2, Required
, Optional
, Computed
, and Sensitive
are boolean fields on the attribute's schema.
func resourceExample() *schema.Resource {
return &schema.Resource{
/* ... */
Schema: map[string]*schema.Schema{
"attribute_example": {
Required: bool
Optional: bool
Computed: bool
Sensitive: bool
/* ... */
Framework
In the Framework, you set the same fields on the schema.Attribute
implementation, with the same behavior.
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
/* ... */
Example
SDKv2
The following example shows how the example_attribute
attribute on the exampleDataSource
data source is set to
be required with SDKv2.
func exampleDataSource() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"example_attribute": {
Required: true,
/* ... */
},
/* ... */
Framework
The following example shows how the example_attribute
attribute on the exampleDataSource
data source is set
to be required 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,
/* ... */
},
/* ... */