Terraform Associate Certification: HashiCorp Certified
Variables Resource Attributes and Dependencies
Resource Targeting
In this guide, you will learn how to employ resource targeting with Terraform's 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 key Name
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 ${...}
.
resource "random_string" "server-suffix" {
length = 6
upper = false
special = false
}
resource "aws_instance" "web" {
ami = "ami-06178cf087598769c"
instance_type = "m5.large"
tags = {
Name = "web-${random_string.server-suffix.id}"
}
}
Changing the Random String Length
Suppose you need to update the random string length from 6 to 5. Running terraform 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:
$ terraform apply
.
Plan: 1 to add, 1 to change, 1 to destroy.
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value: yes
random_string.server-suffix: Destroying... [id=6r923x]
random_string.server-suffix: Destruction complete after 0s
random_string.server-suffix: Creating...
random_string.server-suffix: Creation complete after 0s [id=nglmop]
aws_instance.web: Modifying... [id=i-67428769e06ae2901]
aws_instance.web: Modifications complete after 0s [id=i-67428769e06ae2901]
Apply complete! Resources: 1 added, 1 changed, 1 destroyed.
Warning
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.
$ terraform apply -target random_string.server-suffix
.
Terraform will perform the following actions:
# random_string.server-suffix must be replaced
-/+ resource "random_string" "server-suffix" {
~ id = "bl12qd" -> (known after apply)
~ length = 6 -> 5 # forces replacement
}
.
Plan: 1 to add, 0 to change, 1 to destroy.
Warning: Resource targeting is in effect
.
random_string.server-suffix: Destroying... [id=6r923x]
random_string.server-suffix: Destruction complete after 0s
random_string.server-suffix: Creating...
random_string.server-suffix: Creation complete after 0s [id=nglmpo]
Warning: Applied changes may be incomplete
Apply complete! Resources: 1 added, 0 changed, 1 destroyed.
During this targeted apply, only the random string resource is recreated while the AWS instance resource remains unchanged.
Note
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.
That concludes our tutorial on using resource targeting with Terraform. By following these steps, you can control resource changes in your infrastructure more granularly while maintaining the desired configuration.
Watch Video
Watch video content