Schema Behaviors
Schema fields that can have an effect at plan or apply time are collectively
referred to as "Behavioral fields", or an element's behaviors. These fields are
often combined in several ways to create different behaviors, depending on the
need of the element in question, typically customized to match the behavior of a
cloud service API. For example, at time of writing, AWS Launch Configurations
cannot be updated through the AWS API. As a result, all of the schema elements in
the corresponding Terraform Provider resource aws_launch_configuration
are
marked as ForceNew: true
. This behavior instructs Terraform to first destroy
and then recreate the resource if any of the attributes change in the
configuration, as opposed to trying to update the existing resource.
Primitive Behaviors
Note: The primitive behavior fields cannot be set to false
. You can opt out of a behavior by omitting it.
Optional
Data structure: bool
Values: true
Restrictions:
- Cannot be used if
Required
istrue
- Must be set if
Required
is omitted and element is notComputed
Indicates that this element is optional to include in the configuration. Note
that Optional
does not itself establish a default value. See Default
below.
Schema example:
Configuration example:
Required
Data structure: bool
Values: true
Restrictions:
- Cannot be used if
Optional
istrue
- Cannot be used if
Computed
istrue
- Must be set if
Optional
is omitted and element is notComputed
Indicates that this element must be provided in the configuration. Omitting this attribute from configuration, or later removing it, will result in a plan-time error.
Schema example:
Configuration example:
Default
Data structure: interface
Value: any value of an elements Type
for primitive types, or the type
defined by Elem
for complex types.
Restrictions:
- Cannot be used if
Required
istrue
- Cannot be used with
DefaultFunc
If Default
is specified, Terraform will use that value when this item is not set in
the configuration.
Schema example:
Configuration example (specified):
Configuration example (omitted):
Computed
Data structure: bool
Value: true
Restrictions:
- Cannot be used when
Required
istrue
- Cannot be used when
Default
isspecified
- Cannot be used with
DefaultFunc
Computed
is often used to represent values that are not user configurable or
can not be known at time of terraform plan
or apply
, such as date of
creation or a service specific UUID. Computed
can be combined with other
attributes to achieve specific behaviors, and can be used as output for
interpolation into other resources
Schema example:
Configuration example:
ForceNew
Data structure: bool
Value: true
ForceNew
indicates that any change in this field requires the resource to be
destroyed and recreated.
Schema example:
Configuration example:
Function Behaviors
DiffSuppressFunc
Data structure: SchemaDiffSuppressFunc
When provided DiffSuppressFunc
will be used by Terraform to calculate the diff
of this field. Common use cases are capitalization differences in string names,
or logical equivalences in JSON values.
Schema example:
Configuration example:
Here we assume the service API accepts capitalizations of the base_image
name
and converts it to a lowercase string. The API then returns the lower case value
in its responses.
DefaultFunc
Data structure: SchemaDefaultFunc
Restrictions:
- Cannot be used if
Default
is specified
When provided DefaultFunc
will be used to compute a dynamic default for this element.
The return value of this function should be "stable", such that it is uncommon
to return different values in subsequent plans without any other changes being
made, to avoid unnecessary diffs in terraform plan
.
DefaultFunc
is most commonly used in Provider schemas to allow elements to have
defaults that are read from the environment.
Schema example:
In this example, Terraform will attempt to read region
from the environment if
it is omitted from configuration. If it’s not found in the environment, a
default value of us-west
is given.
Configuration example (provided):
Configuration example (default func with PROVIDER_REGION
set to us-east
in
the environment):
Configuration example (default func with PROVIDER_REGION
unset in the
environment):
StateFunc
Data structure: SchemaStateFunc
StateFunc
is a function used to convert the value of this element to a string to be stored in the state.
Schema example:
In this example, the StateFunc
converts a string value to all lower case.
Configuration example (provided):
Value in statefile:
ValidateFunc
Deprecated: Use ValidateDiagFunc
instead.
Data structure: SchemaValidateFunc
Restrictions:
- Only works with primitive types
ValidateFunc
is a function used to validate the value of a primitive type. Common use cases include ensuring an integer falls within a range or a string value is present in a list of valid options. The function returns two slices; the first for warnings, the second for errors which can be used to catch multiple invalid cases. Terraform will only halt execution if an error is returned. Returning warnings will warn the user but the data provided is considered valid.
Terraform includes a number of validators for use in plugins in the validation package. A full list can be found here: https://pkg.go.dev/github.com/hashicorp/terraform-plugin-sdk/helper/validation
Schema example:
In this example, the ValidateFunc
ensures the integer provided is a value between 0 and 10.
Configuration example:
ValidateDiagFunc
Data structure: SchemaValidateDiagFunc
Restrictions:
- Only works with primitive types
ValidateDiagFunc
is a function used to validate the value of a primitive type. Common use cases include ensuring an integer falls within a range or a string value is present in a list of valid options. The function returns a collection of Diagnostics. Developers should append and build the list of diagnostics up until a fatal error is reached, at which point they should return the Diagnostics. Terraform will only halt execution if an error is returned. Warnings will display a warning message to the practitioner, but continue execution.
The SDK includes some basic validators in the helper/validation
package.
Schema example:
In this example, the ValidateDiagFunc
ensures the string is abc
.
Configuration example: