Resources - Customizing Differences
Terraform tracks the state of provisioned resources in its state file, and compares the user-passed configuration against that state. When Terraform detects a discrepancy, it presents the user with the differences between the configuration and the state.
Sometimes determining the differences between state and configuration requires special handling, which can be managed with the CustomizeDiff
function.
CustomizeDiff
is passed a *schema.ResourceDiff
. This is a structure similar to schema.ResourceData
— it lacks most write functions (like Set
), but adds some functions for working with the difference, such as SetNew
, SetNewComputed
, and ForceNew
.
NOTE: CustomizeDiff
does not currently support computed/"known after apply" values from other resource attributes.
Any function can be provided for difference customization. For the majority of simple cases, we recommend that you first try to compose the behavior using the customdiff helper package, which allows for a more declarative configuration. However, for highly custom requirements, a custom-made function is usually easier and more maintainable than working around the helper's limitations.
In this example we use the helpers to ensure the size can only be increased to multiples of the original size, and that if it is ever decreased it forces a new resource. The customdiff.All
helper will run all the customization functions, collecting any errors as a multierror
. To have the functions short-circuit on error, please use customdiff.Sequence
.