ForceNew
In Terraform, sometimes a resource must be replaced when the value of an attribute changes. In SDKv2, this is
accomplished via the ForceNew
field. In the Framework, you implement the same behavior via a RequiresReplace
plan
modifier. Refer to
Plan Modification - Attribute Plan Modification
in the Framework documentation for details.
This page explains how to migrate this behavior from SDKv2 to the Framework.
SDKv2
In SDKv2, setting the ForceNew
field on an attribute's schema.Schema
triggers a replace (i.e., a destroy-create
cycle) whenever the attribute's value is changed.
Framework
In the Framework, you implement the same behavior by using the resource.RequiresReplace
plan modifier on your
attribute's schema.Attribute
implementation.
Migration Notes
Remember the following differences between SDKv2 and the Framework when completing the migration.
- In both SDKv2 and Framework,
ForceNew
andRequiresReplace
, respectively, only trigger a replace if the attribute is not computed. In the Framework, if an attribute which is computed requires that the resource be replaced when it is changed, implement a plan modifier that triggers the replacement. Refer to RequiresReplacePlanModifier for an example, but bear in mind that each implementation requires different logic and you may need to detect whether the plan has already been modified.
Example
SDKv2
The following example shows the implementation of the ForceNew
field of the
exampleResource
resource's example_attribute
attribute with SDKv2.
Framework
The following shows the same section of provider code after the migration.
This code forces the replacement of a exampleResource
resource when the value of the example_attribute
attribute is changed.
The example does this using the PlanModifiers
field within the exampleResource
attribute's schema.