In this article, we'll dive deep into Terraform variables, a crucial concept for managing configurations efficiently. By the end, you'll understand how to create and use variables, making your Terraform scripts more modular and reusable.
Why Use Variables in Terraform?
Variables in Terraform allow you to define values that can be reused throughout your configuration files. This makes it easier to manage and update configurations without hardcoding values, leading to more flexible and maintainable code. Variables help to:
Simplify Configuration Management: By using variables, you can easily change configuration values without modifying the main script.
Enhance Reusability: Variables make your Terraform scripts reusable across different environments by allowing you to pass different values as needed.
Improve Readability: Variables make your code more readable by clearly defining the values used throughout your scripts.
Step-by-Step Guide
1. Creating a variables.tf
File
First, create a file named variables.tf
. This file will hold all your variable definitions. Here's an example of how to define some basic variables:
variable "filename" {
default = "/home/ubuntu/terraform-tutorials/terraform-variables/demo-var.txt"
}
variable "content" {
default = "This is coming from a variable which was updated"
}
In the above code:
filename
: Holds the path to a file.content
: Stores a string that we want to write into the file.
These variables can then be accessed in your main.tf
file using the var
object.
2. Using Variables in main.tf
Next, use the variables defined in variables.tf
in your main configuration file, main.tf
.
resource "local_file" "devops" {
filename = var.filename
content = var.content
}
In this example, we create a local file named "devops" using the filename and content defined in our variables.
Understanding Data Types in Terraform
Terraform supports various data types for variables. Let's explore some common ones:
3. Map Data Type
A map is a collection of key-value pairs.
variable "file_contents" {
type = map
default = {
"statement1" = "this is cool"
"statement2" = "this is cooler"
}
}
Here, file_contents
is a map with two key-value pairs. You can use these values in your configuration by referencing the keys.
4. List Data Type
A list is an ordered collection of values.
variable "server_names" {
type = list(string)
default = ["server1", "server2", "server3"]
}
You can access list items using their index. Lists are useful when you need to maintain the order of elements.
5. Set Data Type
A set is an unordered collection of unique values.
variable "unique_tags" {
type = set(string)
default = ["tag1", "tag2", "tag3"]
}
Sets are useful for storing unique items without duplicates. Unlike lists, the order of elements in a set is not preserved.
6. Object Data Type
An object is a collection of attributes, allowing you to group related attributes together.
variable "server_config" {
type = object({
name = string
size = string
})
default = {
name = "server1"
size = "large"
}
}
Objects are useful for grouping related attributes, making your variables more organized.
Task Breakdown
Task 1: Create a Local File Using Terraform
Here's how you can create a local file using variables in your Terraform script:
resource "local_file" "devops" {
filename = var.filename
content = var.content
}
This resource block creates a local file with the filename and content specified in the variables.
Task 2: Demonstrate Usage of List, Set, and Object Data Types
Let's create variables of different data types and use them in our configuration.
List Data Type Example
variable "server_names" {
type = list(string)
default = ["server1", "server2", "server3"]
}
resource "null_resource" "example" {
count = length(var.server_names)
provisioner "local-exec" {
command = "echo ${var.server_names[count.index]}"
}
}
This example prints each server name defined in the list.
Set Data Type Example
variable "unique_tags" {
type = set(string)
default = ["tag1", "tag2", "tag3"]
}
resource "null_resource" "example" {
count = length(var.unique_tags)
provisioner "local-exec" {
command = "echo ${element(var.unique_tags, count.index)}"
}
}
This example prints each unique tag defined in the set.
Object Data Type Example
variable "server_config" {
type = object({
name = string
size = string
})
default = {
name = "server1"
size = "large"
}
}
resource "null_resource" "example" {
provisioner "local-exec" {
command = "echo ${var.server_config.name} is ${var.server_config.size}"
}
}
This example prints the server name and size defined in the object.
Refreshing State with terraform refresh
The terraform refresh
command is used to reconcile the state Terraform knows about with the real-world infrastructure. This helps to reload and update the values of your variables based on the current state of your resources.
terraform refresh
Running this command will ensure that your state file is up-to-date with the actual resources in your infrastructure.
Conclusion
Understanding and using variables in Terraform is essential for writing clean, maintainable, and reusable code. By defining variables in a separate file and using them in your main configuration, you can manage your infrastructure more efficiently.
Happy learning!