TFtarget

July 21, 2023

logo

TFtarget is a powerful wrapper tool for the Terraform command, offering an intuitive way to interactively select specific resources for planning, applying, and destroying actions. With TFtarget, you can seamlessly improve the often cumbersome CLI user experience, particularly when making precise and targeted changes to your infrastructure. Say goodbye to complex resource selection and hello to a more efficient and user-friendly Terraform experience with TFtarget. Simplify your infrastructure management today with this indispensable tool.

Let's install TFtarget and and see how it works in our example:

Prerequisites:

  1. Install Terraform - here
  2. Install TFtarget:
  • If you have GO installed - run go install github.com/future-architect/tftarget@latest
  • Using Homebrew - run brew install future-architect/tap/tftarget
  • You can also download the latest compiled binaries and put it anywhere in your executable path - here

Now let's take a look on this tool in our example:

Terraform Plan:

  • I have a small terraform code to create 6 resources. Here is how terraform plan command output looks like:

terraformplan

  • Normally if i would need to target resources i need to use following command terraform plan -target=<resource_type.resource_name> which is not really convenient as you need to make sure no mistakes in resource names :

terraformtarget

  • Now let's use command tftarget plan :

tftargetplan We can now select resources we want to plan as it mentioned here - [Use arrows to move, space to select, to all, to none] Let's select only 3 resources out of 6, for this use arrow to navigate between resources and hit space to choose, when finished hit Enter: tftargetselect

Now we can see our output has changed, as only 3 resources out 6 has been planned:

plan1 plan2

Terraform Apply:

  • Now let's apply only this 3 resources using following command tftarget apply, same as before - we will choose resources to create and hit Enter:

apply1

  • After confirming our apply command as always, only selected resources will be created:

apply2

Terraform Destroy:

  • Let's destoy our EC2 instance and security group we created. We will only leave our VPC

I will use tftarget destroy command, as you can see output shows us only created resources which we can destroy: destroy

  • As expected only 2 out 3 resources were destroyed:

destroy1

Targeting resource inside the terraform module

  • I have a terraform code where i declared modules in main.tf file. Let's see if can target resources inside a modules as easily as before.

My main.tf file looks like that: modulesmain

  • While running tftarget plan i have the following output:

modulestftarget

We can easilly specify resources in submodules to target, while understanding in which module it is located.

Feel free to visit github page of this amazing tool here