What is a Variable in Terraform?
In Terraform, variables are used to define inputs to your Terraform configuration. They allow you to parameterize your code and provide dynamic values to your infrastructure. Variables can be used to pass values between different parts of your Terraform code or to allow users to customize the behaviour of your infrastructure.
We can create 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
- Create a local file using Terraform Hint:
resource "local_file" "devops" {
filename = var.filename
content = var.content
}
Create a variable.tf file which create a filename variable to define the location and name of the file, and create a content variable to define the content of the file.
Create a terraform file to define the resource that calls the variables and assigns them to the file and content to be created. This is the file that is created for the main purpose of executing Terraform to get the output.
This initializes terraform in the folder. It installs all the plugins and providers required to run the Terraform file.
terraform init
This command shows what are the changes to be implemented through terraform.
terraform plan
Finally, the changes are made using this command.
terraform apply
Now, we can see a file is created with the content that we had given in the variable file.
Task-02: Data Types in Terraform
Use terraform to demonstrate usage of Map, List, Set and Object datatypes
Map
In Terraform, a map is a data structure that allows you to store and manage a collection of key-value pairs. Maps are often used to define input variables, output values, and resource configurations.
Create a variable of type map and pass two statements that will act as content for two text files.
variable "file_contents" { type = map default = { "statement1" = "this is cool" "statement2" = "this is cooler" } }
Create two files that will pick the content of the map variable.
Initialize the terraform.
We can check the syntax of our HCL code using terraform validate command. Then let's check the plan of the terraform.
Finally, apply the terraform for the code.
Now we can check that two files have been created with the contents that we had passed in the Map variable.
List
In Terraform, a list is a data structure that allows you to store and manage a collection of values. Lists are often used to define input variables, output values, and resource configurations.
Create a variable of type list to pass the list of files.
Replace the file name in the main.tf code to the list of file variables. This will assign the filename and location through the variable.
We can check the plan of the terraform to visualise what changes are going to be made.
Now, apply the terraform to make the changes.
We can see the files are intact with its content.
Object
In Terraform, an object is a complex data structure that allows you to store and manage a collection of key-value pairs, where the value can be of different data types, including other objects, lists, or maps. Objects are often used to define resource configurations that require complex or nested data structures.
Create a variable to store the object of ec2 instance that will contain some of the instance configurations.
Now, create an output file to view the output of a specific value as per our wish.
Check the plan of terraform.
Apply the terraform in the code.
We can see the output that we had requested in the main.tf file.
Set
In Terraform, a set is a data structure that allows you to store and manage a collection of unique values. Sets are often used to define input variables and to manage resource configurations that require unique sets of values.
Create a variable for the type set to pass the string of security groups.
Create a output file to check the security group.
Plan and apply the terraform for the HCL code.
We can see the security group in the above screenshot.
- Use
terraform refresh
To refresh the state, your configuration file reloads the variables.
Thanks for reading my article. Have a nice day.
You can follow me on LinkedIn for my daily updates:- linkedin.com/in/bandan-kumar-sahoo-131412203