How to run specific terraform resource, module, target?


Terraform file always grows as you span your infrastructure and add multiple servers to your cloud-hosted infrastructure.

But imagine 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.

So 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.

-target=resource not only helps you to run or apply a single resource but with -target=resource you also run more than one resource.

Table of Content

  1. Terraform apply single resource using -target=resource
  2. Terraform apply multiple resources using -target=resource


1. Terraform apply single resource using -target=resource

Here is terraform apply for command for running/applying single resource -

1terraform apply -target=module.module-1.aws_instance.ec2_example 

Let breakdown the command furthermore to understand it -

  1. module - In case if you are using modules in your terraform project then you should add the prefix module
  2. moduel-1 - It is the name of my module in which my terraform resource reside.(Refer to the diagram below for more clarity)
  3. aws_instance - This is the instance type that I am targetting to run or apply
  4. ec2_example - It is the name of my instance which I am targetting to run or apply


1.1 If you do not have a module inside your terraform project

In case if you do not have any module inside your terraform project then you simply need to remove the keyword module.

Here is an example command for it -

1terraform apply -target=aws_instance.ec2_example 

As you do not have any module inside your terraform project so in such cases you will only have main.tf and you can directly pass the reference of the resource along with its name into the -target parameter.



2. Terraform apply multiple resource using -target=resource

In the step-1 we have seen how to run specific terraform resources but What if you want to run more than one resource?.

Well, you can still use terraform -target=resource and specific multiple -target.

Here is an example command for specifying multiple resources -

1terraform apply -target=module.module-1.aws_instance.ec2_module_1 -target=module.module-2.aws_instance.ec2_module_2  

The above contains two modules named - module-1, module-2 and their respective resources.

Here are couple of pictures which will help you to better understand the scenarios with multiple modules and resources




Read More -

  1. Install terraform on Ubuntu 20.04, CentOS 8, MacOS, Windows 10, Fedora 33, Red hat 8 and Solaris 11
  2. How to setup Virtual machine on Google Cloud Platform using terraform
  3. Create EC2 Instance on AWS using terraform
  4. How to use Terraform Input Variables
  5. What is variable.tf and terraform.tfvars?
  6. How to use Terraform locals?
  7. How to use Terraform output values?
  8. Understanding terraform count, for_each and for loop?
  9. Cloud-nuke : How to nuke AWS resources and save additional AWS infrastructure cost?
  10. How to use Terraform Dynamic blocks?
  11. How to use Terraform resource meta arguments?
  12. How to use Terraform Data sources?
  13. What is terraform provisioner?
  14. Terraform how to do SSH in AWS EC2 instance?
  15. How Terraform modules works?
  16. How to run specific terraform resource?
  17. How to use Workspaces in Terraform?
  18. Securing AWS secrets using HashiCorp Vault with Terraform?

Anchor-17-feb-2021