How to use terraform targets to run specific resource

Pinterest LinkedIn Tumblr

Terraform allows you to target specific resources when you plan, apply, or destroy your infrastructure. You can use Terraform’s -target option to target specific resources, modules, or collections of resources.

This command Instructs Terraform to focus its initialization, planning, application or destruction efforts only on resource instances which match the given address and on any objects that those instances depend on.

Terraform target is useful when you have terraform file which contains lots of resources but you only do not want to apply the complete terraform configuration but instead, you just want to run one specific or some specific resource out of your Terraform configuration.

To run specific resources in terraform you should use -target=resource which will help you to target specific resources instead of applying complete terraform configuration.

Related Content

Table of Content

  1. Terraform target Single resource with terraform
  2. Terraform target multiple resources
  3. Example with a terraform s3 bucket module

1. Terraform target single resource

To target a single resource in terraform, use this:

terraform apply -target=module.module-name.aws_resource.name 


  • module – In case if you are using modules in your terraform project then you should add the prefix module
  • module-name – name of my module in which my terraform resource reside
  • aws_resource – This is the aws resource that you are targetting – like an ec2 instance
  • name – this is the name of the aws resource that we are targetting

If you do not have a module in your project, it is also possible to target the resource directly. You can remove the module keyword.

This is how you would do it:

terraform apply -target=aws_resource.name

In this case, you only have a terraform file like main.tf since you do not have any module inside your terraform project so you can directly pass the reference of the resource along with its name into the -target parameter.

2. Terraform target multiple resources

In a case where you want to run more than one resource, you can still use terraform -target=resource and specific multiple -target.

This is the format:

terraform apply -target=module.module-name-one.aws_resource.name -target=module.module-name-two.aws_resource.name

The above contains two modules named – module-name-one, module-name-two and their respective resources

3. Example with a terraform s3 bucket module

Initialize ony buckets matching uat/prod-files-bucket

terraform init -target=module.{uat,prod}-files-bucket

Get and validate buckets matching same pattern

terraform get
terraform validate -target=module.{uat,prod}-files-bucket

Plan and apply the changes

terraform plan -target=module.{uat,prod}-files-bucket -out=tf.plan
terraform apply tf.plan


In this guide, we learnt how to use terraform targets to make changes to selected resources

I am a Devops Engineer, but I would describe myself as a Tech Enthusiast who is a fan of Open Source, Linux, Automations, Cloud and Virtualization. I love learning and exploring new things so I blog in my free time about Devops related stuff, Linux, Automations and Open Source software. I can also code in Python and Golang.

Write A Comment