๐Ÿถ
Terraform

Terraform Apply with .tf File: Input Guide

By Filip on 10/07/2024

Learn how to execute your Terraform configurations by providing a specific .tf file as input to the terraform apply command.

Terraform Apply with .tf File: Input Guide

Table of Contents

Introduction

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.

Step-by-Step Guide

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:

  1. 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.network

    This assumes your network.tf file defines resources within a module named "network".

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

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

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

Code Example

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.

Project Structure:

terraform-selective-apply/
โ”œโ”€โ”€ dev/
โ”‚   โ”œโ”€โ”€ main.tf
โ”‚   โ””โ”€โ”€ variables.tf
โ”œโ”€โ”€ prod/
โ”‚   โ”œโ”€โ”€ main.tf
โ”‚   โ””โ”€โ”€ variables.tf
โ””โ”€โ”€ modules/
    โ””โ”€โ”€ network/
        โ”œโ”€โ”€ variables.tf
        โ””โ”€โ”€ main.tf

1. Separate Directories (dev/ and prod/):

  • dev/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
}
  • dev/variables.tf:
variable "dev_network_cidr" {
  default = "10.0.0.0/16"
}
  • prod/main.tf:
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
}
  • prod/variables.tf:
variable "prod_network_cidr" {
  default = "10.1.0.0/16"
}

To apply the "dev" environment:

cd dev
terraform init
terraform apply

To apply the "prod" environment:

cd prod
terraform init
terraform apply

2. terraform -target (using modules/network):

  • modules/network/main.tf:
resource "aws_vpc" "main" {
  cidr_block = var.network_cidr
  # ... other configurations
}

resource "aws_subnet" "public" {
  count = 2
  # ... other configurations
}
  • modules/network/variables.tf:
variable "network_cidr" {
  default = "10.0.0.0/16"
}

To apply only the "network" module from any directory:

terraform apply -target=module.network

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

Additional Notes

  • Modularity is Key: While these methods offer flexibility, always strive for a well-structured and modular Terraform codebase. This makes your infrastructure easier to manage, understand, and scale.
  • -target Granularity: The -target option can target individual resources, modules, or even specific instances of a resource created with count or for_each.
  • Partial Apply Risks: Be cautious when partially applying changes. Ensure you understand the dependencies between resources to avoid unexpected behavior or errors.
  • State Management: Remember that each Terraform operation (even partial) updates the state file. Always review the planned changes before applying them.
  • Alternative Approaches: Tools like Terragrunt and Terraform Cloud/Enterprise offer advanced features for managing and deploying Terraform code in more complex scenarios.
  • Security Considerations: When using separate directories or workspaces, ensure proper access control and security measures are in place to protect your infrastructure.
  • Testing: Implement thorough testing strategies, including unit tests and integration tests, to validate your Terraform code and prevent issues in your infrastructure.
  • Documentation: Clearly document your Terraform code, including the purpose of each module, variable, and resource. This helps with collaboration and maintainability.

Summary

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.

Conclusion

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.

References

Were You Able to Follow the Instructions?

๐Ÿ˜Love it!
๐Ÿ˜ŠYes
๐Ÿ˜Meh-gical
๐Ÿ˜žNo
๐ŸคฎClickbait