Day 63 - Mastering Terraform Variables
Terraform variables are essential for managing infrastructure-as-code efficiently. They allow flexibility, reusability, and modularity, especially when handling values such as instance names, configurations, file paths, or any other inputs. This blog explores variables in Terraform, their types, and practical applications, guiding you step-by-step through tasks to enhance your hands-on skills.
What Are Variables in Terraform?
Variables in Terraform act as placeholders for dynamic values in configuration files. Instead of hardcoding values, you define variables that can be used throughout your Terraform scripts. This simplifies updates and scales your infrastructure more effectively.
How to Use Terraform Variables
Define variables in a separate file (
variables.tf
):
Keep all your variable definitions in one place for better readability and manageability.Access variables using the
var
object:
In yourmain.tf
file or other resources, reference variables usingvar.<variable_name>
.
Implementation
Let’s dive into two key tasks that will demonstrate Terraform variables.
Task 1: Create a Local File Using Terraform
Step 1: Define Variables
Create a variables.tf
file and add the following:
variable "filename" {
default = "/home/ubuntu/terraform-tutorials/terraform-variables/demo-var.txt"
}
variable "content" {
default = "This is coming from a variable which was updated"
}
Here:
filename
specifies the file path where the file will be created.content
defines the content of the file.
Step 2: Use Variables in main.tf
Create a main.tf
file and add:
resource "local_file" "devops" {
filename = var.filename
content = var.content
}
This resource creates a local file using the variables defined earlier.
Step 3: Execute Terraform Commands
Run the following commands to deploy the configuration:
terraform init
terraform plan
terraform apply
After execution:
Verify the file at
/home/ubuntu/terraform-tutorials/terraform-variables/demo-var.txt
.The file should contain:
This is coming from a variable which was updated
Task 2: Demonstrate Usage of List, Set, and Object Data Types
Data Types Overview
List: An ordered collection of values. Example:
["item1", "item2"]
Set: A collection of unique values. Example:
["item1", "item2"]
Object: A complex data structure with attributes and values. Example:
{ key1 = "value1", key2 = "value2" }
Step 1: Define Variables
In variables.tf
, add:
variable "my_list" {
type = list(string)
default = ["apple", "banana", "cherry"]
}
variable "my_set" {
type = set(string)
default = ["terraform", "devops", "automation"]
}
variable "my_object" {
type = object({
name = string
age = number
})
default = {
name = "John Doe"
age = 30
}
}
Step 2: Use Variables in main.tf
Modify main.tf
to demonstrate the usage of these data types:
output "list_example" {
value = var.my_list
}
output "set_example" {
value = var.my_set
}
output "object_example" {
value = var.my_object
}
Step 3: Apply Configuration
Run the following commands to see outputs:
terraform plan
terraform apply
Check the output:
list_example = [
"apple",
"banana",
"cherry"
]
set_example = [
"terraform",
"devops",
"automation"
]
object_example = {
"name" = "John Doe"
"age" = 30
}
Step 4: Use terraform refresh
The terraform refresh
command updates the state file with the latest configuration and data.
Run:
terraform refresh
This ensures your state file reflects the latest changes from the configuration.
Key Takeaways
Why Use Terraform Variables?
Simplify management of dynamic inputs.
Reuse configurations across environments.
Maintain modular and scalable infrastructure.
Data Types in Terraform
List: Store ordered collections.
Set: Store unique, unordered collections.
Object: Structure complex configurations with named attributes.
Best Practices
Use
variables.tf
for centralizing variables.Use
terraform refresh
to synchronize state files.Leverage
terraform apply
andterraform plan
to preview and apply changes.
Screenshots for Validation
Make sure to take screenshots:
Before applying configurations: Output of
terraform plan
.After applying configurations: Terminal output and file contents.
State refresh: Output of
terraform refresh
.
Conclusion
Terraform variables unlock the potential for modular, flexible infrastructure code. By practicing the tasks above, you’ll gain hands-on experience in variable management and learn how to implement advanced data structures like Lists, Sets, and Objects. Keep experimenting and refining your Terraform skills!