136

It takes a long time to run terraform and wait. So I would like to run it to exclude rds that takes the longest time to excute or I would like to run only ec2 resource. Is there a way to do such things in terraform?

cahen
  • 15,807
  • 13
  • 47
  • 78
negabaro
  • 3,127
  • 7
  • 21
  • 33
  • 1
    Regarding the question in the title (which is not addressed by the accepted answer), there is this feature request in the Terraform issue tracker: https://github.com/hashicorp/terraform/issues/2253 One suggestion on the issue is to: 'take the output of "terraform plan list" , grep out all resource I wanna keep, and then create a list of -target parameters from the rest with a shell script.' – Matt R Aug 06 '19 at 14:24
  • Putting them in separate modules is the most straightforward way of accomplishing this in the long term. You most likely will want to be able to create/destroy ec2 instances w/o destroying all of the data its generally a good idea to separate things this way – Brandon Mar 25 '22 at 22:19

6 Answers6

246

You can use -target=resource like this:

terraform plan -target=module.mymodule.aws_instance.myinstance
terraform apply -target=module.mymodule.aws_instance.myinstance

or

terraform plan -target=aws_instance.myinstance
terraform apply -target=aws_instance.myinstance

Disclaimer: Before downvoting the answer, please note that they actually asked to either "exclude" or "run only ec2 resource". And after all this time the exclude feature request is still open in the terraform repo.

pdoherty926
  • 9,895
  • 4
  • 37
  • 68
Julio Daniel Reyes
  • 5,489
  • 1
  • 19
  • 23
  • 11
    But this is not exclusion. This inclusion. If you need exclude only 1 resource but include the other 99 resources....then this is tedious – Vino Jun 03 '19 at 00:17
  • 8
    As mentioned - this is the opposite of what the questioner asked. If you want to exclude a specific resource, one way is to remove it from the state with 'terraform rm state resource.key' – James O'Brien Aug 07 '19 at 18:37
  • 9
    Before downvoting the answer, please note that he actually asked to either "exclude" or "run only ec2 resource". And after all that time the exclude feature request is still open in the terraform repo https://github.com/hashicorp/terraform/issues/2253. – Julio Daniel Reyes Sep 05 '19 at 15:14
  • It would be a better idea to save the plan, then applying the saved plan instead of checking for plan all the time. – matesio Jun 24 '20 at 13:21
  • 1
    Not sure why, but in my configuration the switch is: --target=aws_instance.myinstance (note: two dashes, one dash fails) – jlo-gmail Apr 02 '21 at 14:09
  • @JulioDanielReyes Your answer helps me lot. And is there any possibility set execution order(s) terraform plan -var-file=tfvars\dev.tfvars -target=module.resource_group -target=module.aks-network -target=module.aks-subnet -target=module.routetable -target=module.aks-subnet-association -target=module.kubernetes-cluster -target=module.node-pool – Ramakrishnan M Aug 05 '22 at 14:58
38

Adding to Julio's answer, you could target multiple resources in the following manner:

terraform init
terraform plan -target=resource_type1.resource_name1 -target=resource_type2.resource_name1
terraform apply -target=resource_type1.resource_name1 -target=resource_type2.resource_name1
Saurabh
  • 5,176
  • 4
  • 32
  • 46
6

I would like to run it to exclude rds that takes the longest time

Terraform currently does not support exclusion of resources (aka. inverse targeting).

Issue #2253: "feature request: inverse targeting / exclude"

(Thanks for the link Julio Daniel Reyes.)

user1338062
  • 11,939
  • 3
  • 73
  • 67
1

This may not be a definitive solution, but this tool could be useful.

The tool "tftarget" is designed to selectively execute "terraform xxx -target={...}".

The selection screen includes options such as "select all," so you should be able to achieve an "inverse target" effect by first selecting all resources and then deselecting the unnecessary ones.

https://github.com/future-architect/tftarget

enter image description here

guest
  • 11
  • 2
0

Most of the answers not working for my terraform version v=1.0.7. This syntax works for me:

terraform plan -target aws_resource.resource_name
terraform apply -target aws_resource.resource_name
Salvador Vigo
  • 397
  • 4
  • 16
-1

For those who use variables

terraform plan -var-file dev.tfvars -target=module.mymodule.aws_instance.myinstance
terraform apply -var-file dev.tfvars -target=module.mymodule.aws_instance.myinstance