Terrarform Variables

 You only need to define `variables.tf` to declare the input variables in your Terraform configuration. `terraform.tfvars` is optional but often used to provide values for those variables.


Here's a breakdown of their roles:


1. `variables.tf`:

   - Purpose: This file is used to declare input variables in your Terraform configuration. It specifies the names, types, and descriptions of the variables you want to use.

   - Contents: In `variables.tf`, you define variables like this:


     

     variable "aws_region" {

       description = "AWS region"

       type        = string

       default  = "us-east-1" # Default value for aws_region

     }

   - Usage: You use these declared variables throughout your Terraform configuration files (e.g., `main.tf`) to parameterize your resources.


2. `terraform.tfvars`:

   - Purpose: This file is used to provide concrete values for the input variables declared in `variables.tf`. It allows you to set values for those variables without modifying your Terraform configuration code.

   - Contents: In `terraform.tfvars`, you specify variable values like this:

     aws_region      = "us-west-2"

     instance_type   = "t2.micro"

   - Usage: Terraform automatically loads values from `terraform.tfvars` (if it exists in the same directory as your configuration files) and assigns those values to the corresponding variables declared in `variables.tf`.


In this example, the aws_region variable is defined with a default value of "us-east-1". If you don't specify a value for aws_region in terraform.tfvars, Terraform will use "us-east-1" as the default value when creating or updating resources.

When you use a variable with a default value in your Terraform configuration, you can still override the default value by providing a value in terraform.tfvars or through command-line flags. The value provided in terraform.tfvars takes precedence over the default value.



In summary, you only need to define `variables.tf` to declare the input variables in your Terraform configuration. However, using `terraform.tfvars` is a common practice because it allows you to customize your configuration without modifying the code itself. If you don't provide values in `terraform.tfvars`, you can still set variable values using command-line flags when running `terraform apply`.

Comments

Popular posts from this blog

Cloudbees FlowServer - Debug

PIP Errors and Fix

Terraform Basics