Update Resources
In-place update is part of the basic Terraform lifecycle for managing resources. During the terraform apply
command, Terraform calls the provider ApplyResourceChange
RPC, in which the framework calls the resource.Resource
interface Update
method. The request contains Terraform prior state, configuration, and plan data. The response contains updated state data. The data is defined by the schema of the resource.
To ensure that Terraform plans replacement of a resource, rather than perform an in-place update, use the resource.RequiresReplace()
attribute plan modifier in the schema or implement resource plan modification.
Other resource lifecycle implementations include:
- Create resources by receiving Terraform configuration and plan data, performing creation logic, and saving Terraform state data.
- Read resources by receiving Terraform prior state data, performing read logic, and saving refreshed Terraform state data.
- Delete resources by receiving Terraform prior state data and performing deletion logic.
Define Update Method
Implement the Update
method by:
- Accessing the data from the
resource.UpdateRequest
type. Most use cases should access the plan data in theresource.UpdateRequest.Plan
field. - Performing logic or external calls to modify the resource.
- Writing state data into the
resource.UpdateResponse.State
field.
If the logic needs to return warning or error diagnostics, they can added into the resource.UpdateResponse.Diagnostics
field.
In this example, the Update
function makes a HTTP call and returns successfully if the status code was 200 OK:
Caveats
Note these caveats when implementing the Update
method:
- An error is returned if the response state is not set when
Update
is called by the framework. If the resource does not support modification and should always be recreated on configuration value updates, theUpdate
logic can be left empty and ensure all configurable schema attributes implement theresource.RequiresReplace()
attribute plan modifier. - An error is returned if the response state contains unknown values. Set all attributes to either null or known values in the response.
- An error is returned if the response state has the
RemoveResource()
method called. This method is not valid during update. Return an error if the resource is no longer exists. - An error is returned unless every null or known value in the request plan is saved exactly as-is into the response state. Only unknown plan values can be modified.
Recommendations
Note these recommendations when implementing the Update
method:
- Get request data from the Terraform plan data over configuration data as the schema or resource may include plan modification logic which sets plan values.
- Only successfully modified parts of the resource should be return updated data in the state response.
- Use the
resource.UseStateForUnknown()
attribute plan modifier forComputed
attributes that are known to not change during resource updates. This will enhance the Terraform plan to not show=> (known after apply)
differences.
Additional Use Cases
This section highlights implementation details for specific use cases.
Detect Specific Attribute Changes
Certain update APIs require that only value changes are sent in the update request or require individual update API calls. Compare the attribute plan value to the attribute prior state value to determine individual attribute changes.
In this example, the entire plan and prior state are fetched then the attribute values are compared:
In this example, the attribute is individually fetched from the plan and prior state then the values are compared: