Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I would like to run terraform only for a specific resource

Tags:

terraform

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?

like image 393
negabaro Avatar asked Oct 16 '17 02:10

negabaro


People also ask

How do you ignore a resource in terraform?

When you want Terraform to ignore changes between subsequent apply commands you can use the lifecycle ignore_changes meta-argument. The ignore_changes argument means that Terraform will set the value when the resource is first deployed and then forever ignore any changes to it.

Can we run terraform apply without plan?

When you run terraform apply without passing a saved plan file, Terraform automatically creates a new execution plan as if you had run terraform plan , prompts you to approve that plan, and takes the indicated actions.

What is a target in terraform?

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.


2 Answers

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

like image 124
Julio Daniel Reyes Avatar answered Oct 07 '22 00:10

Julio Daniel Reyes


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
like image 19
Saurabh Avatar answered Oct 07 '22 00:10

Saurabh