Default
After validation and before applying configuration changes, Terraform generates a plan that describes the expected values and behaviors of those changes. Resources can then tailor the plan to set default values on computed resource attributes that are null in the configuration.
A Default can only be added to a resource schema attribute.
When is a Default set?
A Default is set during the planning process, immediately prior to the framework marking computed attributes that are null in the configuration as unknown in the plan.
Attribute Default
You can supply the attribute type Default
field with a default for that attribute. For example:
If defined, a default is applied to the current attribute providing that the attribute is null in the configuration. If any nested attributes define a default, then those are applied afterwards. Any default that returns an error will prevent Terraform from applying further defaults of that attribute as well as any nested attribute defaults.
Common Use Case Attribute Defaults
The framework implements static value defaults in the typed packages under resource/schema/
:
Custom Default Implementations
To create an attribute default, you must implement the one of the resource/schema/defaults
package interfaces. For example:
Optionally, you may also want to create a helper function to instantiate the default. For example: