Day 63 — Terraform Variables
As we are diving deep into terraform day by day. Let’s explore variables in terraform. Variables in Terraform are quite important, as you need to hold values of names of instance, configs etc.
What are Variables?
In Terraform, variables are a way to parameterize your infrastructure code. They allow you to define values that can be passed into your Terraform configurations when you apply them, making your configurations more flexible and reusable. Variables are especially useful when you want to make your infrastructure code adaptable to different environments or configurations without having to modify the code itself.
Here’s how variables work in Terraform:
- Variable Declaration: You start by declaring variables in your Terraform configuration using the
variable
block. For example:
variable "example_var" {
description = "An example variable"
type = string
default = "default_value"
}
In this example, we’ve defined a variable named example_var
with a default value of "default_value" and a type of string. You can also provide a description for the variable to explain its purpose.
2. Using Variables: You can use the declared variables in your resource blocks, data blocks, or other parts of your configuration by referencing them with the var
prefix. For example:
resource "aws_instance" "example_instance" {
ami = var.example_var
instance_type = "t2.micro"
}
In this case, we’re using the example_var
variable as the ami
attribute for an AWS EC2 instance.
3. Variable Values: When you apply your Terraform configuration, you can provide values for the variables using various methods such as command-line arguments, variable files, or environment variables. For example, using the command line:
terraform apply -var="example_var=new_value"
This overrides the default value of example_var
with "new_value."
4) Variable Types: Terraform supports various variable types, including string, number, boolean, list, map, and more. You specify the variable type in the type
attribute when declaring the variable.
5) Variable Defaults: You can set default values for variables, as shown in the variable declaration example. If a value is not provided when applying the configuration, Terraform will use the default value.
Using variables in Terraform allows you to create more dynamic and reusable configurations. They are particularly valuable when you need to customize infrastructure for different environments (e.g., development, staging, production) or when sharing configurations across teams.
We can create a variable.tf file which will hold all the variables.
variable "filename" {
default = "/home/ubuntu/terraform/terraform-variable/var-demo.txt"
}
variable "content" {
default = "This is demo terraform variable file"
}
Tasks:
Task 1) Create a local file using Terraform
Create a variable.tf file
The filename variable is set to /home/ubuntu/terraform/terraform-variable/var-demo.txt by default, which is the path and filename where the local file will be created.
The content variable has the default value of “This is demo terraform variable file”, which is the text that will be put into the local file.
Create a main.tf file
resource "local_file" "devops" {
filename = var.filename
content = var.content
}
The “devops” local_file resource, which is created by Terraform code block, write a file with the name and content given by the filename and content variables to disc.
Initializes a new or existing working directory for Terraform
terraform init
Produces an execution plan outlining the steps Terraform will take to achieve the desired state defined in the configuration file.
terraform plan
When you run Terraform apply, Terraform will generate a file in the local directory named var-demo.txt with the content supplied in the content variable.
terraform apply
Check file is created in a specified folder using the ls command.
Data Types in Terraform
Map
variable "file_contents" {
type = map
default = {
"statement1" = "this is cool"
"statement2" = "this is cooler"
}
}
Task 2) Use terraform to demonstrate usage of List, Set and Object datatypes
Task-02
Use terraform to demonstrate usage of List, Set, and Object datatypes
Put proper screenshots of the outputs
Map:
A map is a collection of values where each value is identified by a string label
variable.tf
variable "content_map" {
type = map
default = {
"content1" = "this is cool content1"
"content2" = "this is cooler content2"
}
}
A map variable named content_map is defined in the example, with a default value of two key-value pairs. Strings serve as both the keys and the values.
This variable can be used in your Terraform configuration to reference the values associated with each key defined in the map. For example, you might use it to configure a resource that requires content,
resource "local_file" "devops" {
filename = "/home/ubuntu/terraform/terraform-variable/type-map/demo.txt"
content = var.content_map["content1"]
}
In this example, we set the content parameter of the local_file resource using the content_map variable. We use dot notation and the syntax var.content_map[“content1”] to refer to the value associated with the key “content1.”
Let’s do terraform init.
terraform plan
terraform apply