DEV Community

1suleyman
1suleyman

Posted on

🎯 What Is Resource Targeting in Terraform? (And Why You Shouldn’t Always Apply Everything at Once)

Hey everyone 👋

If you're getting into Terraform or managing cloud resources with infrastructure as code, you might’ve hit a point where you said:

“Wait… I just want to apply this one thing — not the whole stack!”

That’s where resource targeting comes in. I used to think terraform apply meant all-or-nothing. But there’s actually a built-in way to focus your changes down to a single resource — without touching everything else.

Let me break it down the way I wish someone had explained it to me 👇


🧸 Think of It Like Fixing One Lightbulb, Not Rewiring the Whole House

Imagine your infrastructure is like a house. You’ve got lights, plumbing, a heating system — the works.
Now the hallway light goes out. You don’t want the electrician to rip out all the wiring — just fix that light.

That’s exactly what resource targeting in Terraform lets you do. Instead of applying your entire configuration, you can target just one resource.


⚙️ What Terraform Normally Does (By Default)

When you run:

terraform plan
terraform apply
Enter fullscreen mode Exit fullscreen mode

Terraform looks at everything in your project folder — every .tf file — and merges it into a single plan.
By default, it plans to create/update/destroy all the resources it detects.

That’s fine most of the time. But sometimes, you want to be more surgical.


🎯 Enter the -target Flag

Let’s say your project contains:

resource "aws_iam_user" "admin_user" { ... }
resource "aws_security_group" "web_sg" { ... }
resource "local_file" "foo" {
  content  = "hello"
  filename = "foo.txt"
}
Enter fullscreen mode Exit fullscreen mode

You want to create just the local file.
Not the IAM user.
Not the security group.
Just foo.txt.

Here’s the magic:

terraform apply -target=local_file.foo
Enter fullscreen mode Exit fullscreen mode

✅ This will only apply changes to that one resource. The rest won’t be touched.


🛠️ Syntax Options

🧪 Operation 🧾 Example
Plan terraform plan -target=aws_security_group.web_sg
Apply terraform apply -target=local_file.foo
Destroy terraform destroy -target=aws_iam_user.admin_user

Alternate shell-safe syntax:

terraform apply --target="local_file.foo"
Enter fullscreen mode Exit fullscreen mode

💡 Why Would You Use This?

Let’s say you’re working in a team.
You’ve got a main.tf file with 10+ resources.
But only one of them — say, a security group — needs an urgent change.

Maybe port 80 needs to be opened for a hotfix.
You don’t want to risk breaking the other 9 resources that are still in development.

So instead, you run:

terraform apply -target=aws_security_group.web_sg
Enter fullscreen mode Exit fullscreen mode

Boom — just the security group gets updated. Crisis averted.


⚠️ Resource Targeting Isn’t Always a Best Practice

Yes, it’s powerful. But here’s the catch:

Overusing -target can lead to infrastructure drift or partial updates that break dependencies.

Terraform’s job is to understand your entire dependency graph and make changes safely.
When you bypass that with -target, you’re telling it:

“Ignore what you know — just do this.”

Use it only when you really need it, like:

  • Recovering from a failed deployment
  • Deploying a hotfix to a single resource
  • Fixing a corrupted state file
  • Testing a single module in isolation

🧠 My Takeaway

Resource targeting with -target is like a scalpel — great for surgery, bad for chopping vegetables every day.

It’s helped me in tricky moments (especially in team projects or multi-resource environments), but I always double-check what I'm targeting and why.


📚 Want to Try It Yourself?

Here’s a quick practice setup:

# resource-target.tf
resource "local_file" "foo" {
  content  = "hello world"
  filename = "foo.txt"
}

resource "null_resource" "test" {
  provisioner "local-exec" {
    command = "echo 'Hello from null resource'"
  }
}
Enter fullscreen mode Exit fullscreen mode

Run:

terraform init
terraform apply -target=local_file.foo
Enter fullscreen mode Exit fullscreen mode

Only the foo.txt file will be created.


🧩 Final Thoughts

Resource targeting is a great tool — but like all tools, it’s best used wisely.

✅ Great for:

  • Isolated testing
  • Urgent fixes
  • Broken state recovery

❌ Not ideal for:

  • Daily workflows
  • Full deployments
  • Anything with deep dependencies

If you’ve used -target in your Terraform journey — or if you’ve run into problems because of it — I’d love to hear your experience! Drop a comment or message me on LinkedIn. Let’s keep building smarter infrastructure together 💬🚀

Top comments (0)