Variables in Terraform are quite important, as you need to hold values of names of instance, configs , etc.
Terraform variables are a powerful feature that allows you to define reusable and configurable values within your Terraform configuration. They are used to decouple your infrastructure configuration from specific values, making it easier to maintain, share, and reuse your configuration.
We can create a variables.tf file which will hold all the variables.
variable "filename" {
default = "/home/ubuntu/terrform-tutorials/terraform-variables/demo-var.txt"
}
variable "content" {
default = "This is coming from a variable which was updated"
}
These variables can be accessed by var object in main.tf
Task-01
- Create a local file using Terraform
Hint:
resource "local_file" "devops" {
filename = var.filename
content = var.content
}
- Create a
variable.tf
file
Create a main.tf
file
After defining your variable.tf
and main.tf
, you can run the following Terraform commands:
terraform init
terraform plan
terraform apply
Verify the file is created
Data Types in Terraform
Map: Represents a collection of key-value pairs.
List: Represents ordered sequences of values.
Set: Represents an unordered collection of unique values.
Object: Represents a complex data structure with multiple attributes.
String: Represents sequences of characters.
Number: Represents numeric values (integers or floating-point numbers).
Bool: Represents boolean values (true or false).
Tuple: Represents an ordered collection of values with fixed types and a fixed number of elements.
Map
A map is a collection of key-value pairs where each key and value can be of any data type.
Example of a map:
variable "file_contents" {
type = map
default = {
"statement1" = "this is cool"
"statement2" = "this is cooler"
}
}
Create a variable.tf
file
Create a main.tf
file
Now run the command terraform init
to initialize the Terraform.
Run terraform plan
command to create an execution plan.
Run terraform apply
Verify
Task-02
Use terraform to demonstrate usage of List, Set and Object datatypes
Put proper screenshots of the outputs
Use terraform refresh
To refresh the state by your configuration file, reloads the variables
List
In Terraform, a list is a data type that represents an ordered sequence of values. Lists are useful when you need to work with a collection of elements where the order matters. You can define a list variable, use it in resource configurations, and access individual elements by their index.
Lists are represented by enclosing values within square brackets.
Example of a list:
variable "example_list" {
type = list(string)
default = ["item1", "item2", "item3"]
}
Object
In Terraform, an object is a collection of named attributes, each of which has a specific value. It's a flexible way to organize and store structured data within Terraform configurations.
Objects are declared using curly braces {}
followed by a comma-separated list of attribute-value pairs.
variable "aws_ec2_object" {
type = object({
name = string
instance = number
keys = list(string)
ami = string
})
default = {
name = "Terraform"
instance = 1
keys = ["Terraformkeypair.pem"]
ami = "ami-0c7217cdde317cfec"
}
}
Create a variables.tf
file
Create a main.tf
file
output "aws_ec2_object" {
value = var.aws_ec2_object
}
Run terraform init
Run terraform plan
Run terraform apply
Set
In Terraform, a set ia s data type that represents an unordered collection of unique values. Sets are useful when you want to ensure that a group of elements contains only distinct values. Sets do not preserve the order of elements, and each element must be unique within the set.
Create a variables.tf
file
variable "sercurity_groups" {
type = set(string)
default = ["sg-135791112", "sg-246810124"]
}
Create a main.tf
file
Use terraform refresh
to refresh the state by your configuration file, and reload the variables.
The terraform refresh
command is typically used to update the state file before running a terraform plan
or terraform apply
command. This ensures that Terraform is aware of any changes that have been made to your infrastructure outside of Terraform.