Learn how to execute your Terraform configurations by providing a specific .tf file as input to the terraform apply command.
terraform -target (using modules/network):In Terraform, you manage infrastructure as code by defining resources in .tf files. While you can't apply a single .tf file directly, Terraform provides several methods to achieve focused deployments and manage your infrastructure effectively.
You can't directly apply a single .tf file in Terraform. Terraform treats all .tf files within a directory as a single configuration.
However, you can achieve a similar effect by using these methods:
terraform -target=: This option allows you to specify specific resources within your configuration to target. You can structure your code so that the resources defined in a particular .tf file share a common naming convention or are grouped under a specific module. Then, you can use -target to apply changes only to those resources.
For example, if you have a file named network.tf and want to apply only the resources defined there, you could use a command like:
terraform apply -target=module.networkThis assumes your network.tf file defines resources within a module named "network".
Separate Directories: Organize your Terraform code into separate directories based on their functionality or environment. Each directory would contain its own set of .tf files. Then, you can navigate to the specific directory and run terraform apply to apply only the configuration within that directory.
Input Variables: Define variables in a separate file (e.g., variables.tf) and use them within your .tf files. You can then provide values for these variables using command-line arguments (-var) or variable definition files (.tfvars). This allows you to control the behavior of your Terraform code without modifying the .tf files themselves.
Workspaces: Terraform workspaces allow you to manage multiple instances of your infrastructure with the same configuration. You can create different workspaces for different environments (e.g., development, staging, production) and apply changes to each workspace independently.
Remember that Terraform's strength lies in managing your entire infrastructure as code. While focusing on individual .tf files might seem convenient, it's generally better to structure your code in a modular and reusable way.
This code demonstrates how to apply Terraform code selectively using separate directories and the terraform -target command. It includes a project structure with dev and prod directories, each containing main.tf and variables.tf files for configuring resources in different environments. The code also includes a modules directory with a network module for creating VPC and subnet resources. The example shows how to apply code for specific environments by navigating to the corresponding directory and running terraform commands. It also demonstrates how to use terraform -target to apply only the network module from any directory. This allows for flexible and targeted infrastructure management using Terraform.
This example showcases the methods mentioned in the article to apply Terraform code selectively, focusing on separate directories and terraform -target.
terraform-selective-apply/
โโโ dev/
โ   โโโ main.tf
โ   โโโ variables.tf
โโโ prod/
โ   โโโ main.tf
โ   โโโ variables.tf
โโโ modules/
    โโโ network/
        โโโ variables.tf
        โโโ main.tf
module "network" {
  source = "../../modules/network"
  network_cidr = var.dev_network_cidr
}
resource "aws_instance" "dev_instance" {
  ami           = "ami-xxxxxxxxxxxxxxxxx"
  instance_type = "t2.micro"
  subnet_id     = module.network.public_subnets[0]
  # ... other configurations
}variable "dev_network_cidr" {
  default = "10.0.0.0/16"
}module "network" {
  source = "../../modules/network"
  network_cidr = var.prod_network_cidr
}
resource "aws_instance" "prod_instance" {
  ami           = "ami-yyyyyyyyyyyyyyyyy"
  instance_type = "t3.medium"
  subnet_id     = module.network.public_subnets[0]
  # ... other configurations
}variable "prod_network_cidr" {
  default = "10.1.0.0/16"
}To apply the "dev" environment:
cd dev
terraform init
terraform applyTo apply the "prod" environment:
cd prod
terraform init
terraform applyresource "aws_vpc" "main" {
  cidr_block = var.network_cidr
  # ... other configurations
}
resource "aws_subnet" "public" {
  count = 2
  # ... other configurations
}variable "network_cidr" {
  default = "10.0.0.0/16"
}To apply only the "network" module from any directory:
terraform apply -target=module.networkThis example demonstrates how to use separate directories and terraform -target to apply specific parts of your Terraform code. You can adapt these methods and combine them with input variables and workspaces to manage your infrastructure effectively.
-target Granularity:  The -target option can target individual resources, modules, or even specific instances of a resource created with count or for_each.While you can't apply a single .tf file directly in Terraform, you can achieve targeted changes through these methods:
| Method | Description | 
|---|---|
terraform -target= | 
Apply changes only to resources matching a specific name pattern or module. Requires consistent naming conventions in your code. | 
| Separate Directories | Organize code into directories by functionality or environment. Apply changes to a specific directory's configuration. | 
| Input Variables | Define variables in a separate file and control their values via command-line arguments or .tfvars files. Modify behavior without changing .tf files. | 
| Workspaces | Manage multiple infrastructure instances with the same configuration. Apply changes independently to different environments (e.g., development, staging, production). | 
Key Takeaway:  Focus on modular and reusable code structure instead of applying individual .tf files for a more manageable and robust Terraform setup.
In conclusion, while Terraform doesn't allow applying a single .tf file directly, it offers flexibility in managing and applying parts of your infrastructure code. Techniques like terraform -target, separate directories, input variables, and workspaces empower you to make targeted changes. Prioritize a modular and well-structured codebase for better organization, scalability, and easier management. Remember to understand resource dependencies, review planned changes, and implement robust testing and documentation practices. By leveraging these methods and maintaining best practices, you can effectively manage your infrastructure with Terraform while ensuring clarity and maintainability.
 How to work with JSON - Terraform - HashiCorp Discuss | Hello everyone, hope somebody can help me with my issue. I need some help on understanding how to work with a JSON full of variables that Iโd like to use in my terraform file in order to create my infrastructure. Is this even possible? from what I was able to see, it is possible but I was not able to find a good example that shows me how to do it.  The JSON that I want to use has simple varibles and arrays of arrays (meaning that is not just a simple JSON). Do I need to declare, in a variables.t...terraform apply <textfile> gives an unhelpful error: "zip: not a valid ... | Terraform Version Terraform v0.12.4 Terraform Configuration Files Just need an empty file named, for example, test. Debug Output https://gist.github.com/javaJake/88dc0939ccba72a4a091736cb1190fdc Ex...
 Command: apply | Terraform | HashiCorp Developer | ... Terraform input variables ( -var and -var-file ), etc. Apply Options. The following options change how the apply command executes and reports on the applyย ...
 Terraform Apply Command | Options, Examples and Best Practices ... | This state file contains all the metadata and the current state of the EC2 instance. From this point onwards, any further changes you make in your main.tf fileย ...
 Command: plan | Terraform | HashiCorp Developer | Use this option multiple times to include values from more than one file. There are several other ways to set values for input variables in the root module,ย ...
 Set up terraform (TF) and AWS CLI, build a simple EC2! | by ... | I was gifted  a new-to-me laptop, and to keep my TF and AWS skills up-to-date, I am setting up the Terraform and AWS CLIs.