plan and apply commands. This tutorial uses an example that includes two resource blocks: one for generating a random string and another for provisioning an AWS instance. In this updated example, both resource blocks have been slightly modified for enhanced functionality.
Enhanced AWS Instance Tagging
The AWS instance resource, named “web,” now has a tag with the keyName that appends the salt value generated by the random string resource. This is accomplished using Terraform’s interpolation syntax by referencing random_string.server-suffix.id within ${...}.
Changing the Random String Length
Suppose you need to update the random string length from 6 to 5. Runningterraform apply without targeting will lead to the recreation of the random string resource with the new length. Consequently, the AWS instance’s tag will be updated due to its dependency on the random string value.
Below is a sample of the console output after applying the change:
Using
terraform apply without proper targeting will update both the random string and the AWS instance tag. Make sure this is the desired behavior before proceeding.Resource Targeting to Isolate Changes
What if you want to update only the random string resource and keep the AWS instance configuration intact? To do this, you should first revert both resources to their original state, where the random string had a 6-character value. Then, use the-target flag with terraform apply to focus solely on the random string resource.
By specifying the resource address using the syntax resourceType.resourceName, the following command targets only the random string resource “server-suffix”. Note that Terraform may warn you that the changes could be incomplete.
Resource targeting should be used sparingly—only for corrective actions or urgent modifications. A full plan execution is recommended for comprehensive changes to avoid incomplete configurations.