svg
Post Image
By Daniel Tanque20 de Novembro, 2023In Sem categoria

Terraform: Variables

In Terraform, variables provide a way to parameterize your configurations, allowing you to reuse and customize values across different parts of your infrastructure code. Variables are a fundamental concept in Terraform that enhances the flexibility and maintainability of your configurations.

Variables help avoid hard code and gives more dynamic capability to the infrastructure. In this article you will learn more about them and how can you take use of them to optimize you terraform administration.

Structure

To define a variable you declare the following:

variable "filename"{
default = "/root/pets.txt"
}

And you can call on main.tf with:

var.filename

More into it

You can also define the type of the variable if you want to ensure a specific data type (it’s optional), for that here is an example:

variable "filename"{
default = "/root/pets.txt"
type = string
description = "the path to the local file"
}
variable "prefix" {
default = [ "Mr", "Mrs", "Sir"]
type = list
}

And you can call on main.tf as:

prefix = var.prefix[0]

Another option is to define as map:

variable “file_content” {
type = map
default = {
statement1 = “Test 1”
statement2 = “Test 2”
}
}

And you call it as:

content = var.file_content[“statement2”]

You can also have sets (which is a list with non-duplicates), an object or a tuple which is a list but the arguments have different types in it.

Terminal

You can also declare on terminal:

terraform apply – var “filename = /root/pets.txt”

Output Variables

You can use them to store values of an expression. For example:

output “pet-name”{
value = random_pet.my_pet.id
description = “Save this value”
}

svgTerraform Basics: Resources
svg
svgTerraform: Commands

Leave a reply