Learn how to use Terraform's targeted resource execution to efficiently manage and update individual resources within your infrastructure.
In Terraform, you can focus your actions on specific resources instead of applying changes to your entire infrastructure. This is particularly useful when you need to modify or troubleshoot a single resource or a small set of resources without affecting the rest of your deployment. This article explains how to use the -target option with terraform plan and terraform apply to work with specific resources in your Terraform code.
To run Terraform actions on specific resources, you can use the -target option with terraform plan and terraform apply.
Identify the resource: Determine the exact address of the resource you want to target. This address follows the format:
resource_type.resource_name. For example, aws_instance.example.
Use the -target option: When running terraform plan or terraform apply, include the -target option followed by the resource address. For instance:
terraform plan -target=aws_instance.exampleThis command will show the execution plan for only the targeted resource.
Apply the changes (optional): If you're satisfied with the plan, apply the changes using terraform apply with the same -target option:
terraform apply -target=aws_instance.exampleImportant Notes:
-target options.module.my_module.aws_instance.example.-target can lead to configuration drift if you're not careful. Ensure you understand the dependencies between your resources.This text describes how to use the -target flag in Terraform to make changes to specific resources within a configuration. It explains how to identify the resource address, plan changes with -target, and apply those changes. It also covers how to target multiple resources and resources within modules. Finally, it advises caution when using -target and suggests alternative tools for managing complex environments.
Let's say you have a Terraform configuration that defines an AWS EC2 instance and an S3 bucket:
# main.tf
resource "aws_instance" "example" {
ami = "ami-0c55b159c9e67027c"
instance_type = "t2.micro"
}
resource "aws_s3_bucket" "example" {
bucket = "my-tf-test-bucket"
acl = "private"
tags = {
Name = "My static website"
}
}Scenario: You want to modify the tags for the S3 bucket without affecting the EC2 instance.
Steps:
Identify the resource: The resource address for the S3 bucket is aws_s3_bucket.example.
Plan changes with -target:
terraform plan -target=aws_s3_bucket.exampleThis command will only show the planned changes for the aws_s3_bucket.example resource.
Apply changes with -target (optional):
terraform apply -target=aws_s3_bucket.exampleThis command will apply the changes only to the targeted S3 bucket.
Multiple Targets:
To target both the EC2 instance and the S3 bucket, you can use multiple -target options:
terraform plan -target=aws_instance.example -target=aws_s3_bucket.exampleModules:
If the S3 bucket was defined within a module named "storage", the target address would include the module path:
terraform plan -target=module.storage.aws_s3_bucket.exampleRemember: While -target is useful for specific scenarios, always exercise caution and consider its potential impact on your infrastructure. Using tools like Terragrunt or Terraform workspaces can provide more robust solutions for managing complex environments and isolating changes.
*) in resource addresses to target multiple resources. For example, aws_instance.* would target all resources of type aws_instance. Use wildcards with extreme caution as it can lead to unintended consequences.module.storage.* would target all resources within the "storage" module.-target is incredibly useful for debugging issues with specific resources. You can use it to run terraform plan and terraform apply repeatedly on a single resource to pinpoint problems.-target is the safe and recommended way to focus Terraform actions.-target, Terraform's drift detection might not accurately reflect the state of your entire infrastructure. It's crucial to run a full terraform plan periodically to ensure consistency.-target can indicate potential issues with your infrastructure management. Consider if techniques like modularization, workspaces, or smaller, focused Terraform projects would be more suitable for your workflow.This table summarizes how to use the -target option in Terraform to manage individual resources:
| Step | Description | Example |
|---|---|---|
| 1. Identify Resource Address | Determine the resource's unique identifier in the format: resource_type.resource_name. |
aws_instance.example |
| 2. Plan Changes | Use terraform plan -target to preview changes for the specific resource. |
terraform plan -target=aws_instance.example |
| 3. Apply Changes (Optional) | Use terraform apply -target to execute the planned changes on the targeted resource. |
terraform apply -target=aws_instance.example |
Key Points:
-target options to manage several resources at once.-target. Understand resource dependencies.The -target option in Terraform provides a way to focus actions on specific resources, which is helpful for making changes to parts of your infrastructure without affecting the rest. By specifying the resource address, you can plan and apply changes to individual resources or groups of resources. However, it's crucial to use -target cautiously, as it can lead to configuration drift if dependencies between resources are overlooked. For managing complex infrastructures and isolating changes, consider using tools like Terragrunt or Terraform workspaces as they offer more robust solutions. Regularly running a full terraform plan is also recommended to ensure the consistency and accuracy of your infrastructure's state.
Target resources | Terraform | HashiCorp Developer | Occasionally you may want to apply only part of a plan, such as when Terraform's state has become out of sync with your resources due to a network failure, a ...
How to run specific terraform resource, module, target? | Jhooq | Aug 15, 2021 ... But imagine you have terraform file which contains lots of resources but you only do not want to apply the complete terraform configuration but ...
I would like to run terraform only for a specific resource | by Harold ... | Running Terraform for a specific resource can be useful when you only want to apply changes to a particular part of your infrastructure…
How to target resources in Terraform | by suriya3456 | Medium | What is a target in Terraform?