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
- How to use terraform to Launch an AWS EC2 Instance
- Using terraform to launch Digitaocean kubernetes cluster
- Terraform AWS VPC with Public and Private subnets with NAT
- Create an RDS instance in terraform with a Mariadb Example
Table of Content
- Terraform target Single resource with terraform
- Terraform target multiple resources
- 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
Where:
module
– In case if you are using modules in your terraform project then you should add the prefixmodule
module-name
– name of my module in which my terraform resource resideaws_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:
<meta charset="utf-8">terraform apply -target=module.module-name-one.aws_resource.name <meta charset="utf-8">-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-<meta charset="utf-8">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.<meta charset="utf-8">{uat,prod}-<meta charset="utf-8">files-bucket -out=tf.plan
terraform apply tf.plan
Conclusion
In this guide, we learnt how to use terraform targets to make changes to selected resources