Reputation: 3975
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?
Upvotes: 187
Views: 220875
Reputation: 2415
I'm using Windows Powershell.
I use Double Hyphen in prefix of target (--target).
terraform apply --target=aws_s3_object.resource_name
Upvotes: 1
Reputation: 1
if you want to apply changes to a specific resource or to resources within a specific module without affecting the rest of the managed resources, you can use -target terraform apply -target
For example, running
terraform apply -target=aws_instance.env0-instance will apply changes only to the resource named env0-instance of the type aws_instance
Upvotes: 0
Reputation: 41
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
Upvotes: 4
Reputation: 6960
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
If you output the plan then you only need to specify the parameters during the plan phase:
terraform init
terraform plan \
-target=resource_type1.resource_name1 \
-target=resource_type2.resource_name1 \
-out plan
terraform apply plan
Upvotes: 53
Reputation: 457
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
Upvotes: 2
Reputation: 6365
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.
Upvotes: 330
Reputation: 97
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
Upvotes: 1
Reputation: 12755
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.)
Upvotes: 7