Terraform Variables 🔥
Terraform Variables
Terraform allows you to define variables to parameterize your infrastructure code and make it more reusable and configurable. Variables in Terraform are quite important, as you need to hold values of names of instances, configs, etc.
To define a variable in Terraform, you can use the "variable" block in your configuration file or create a separate ".tfvars" file. Here's an example of defining a variable in a configuration file:
variable "region" {
description = "The AWS region"
type = string
default = "us-east-1"
}
In this example, we define a variable named "region" with a description, type, and default value. The variable is of type string and has a default value of "us-east-1". You can reference this variable later in your configuration by using the syntax "${var.region}".
We can create a variable.tf file which will hold all the variables. These variables can be accessed by the var object in main.tf
Task-01
Create a local file using Terraform
Create a variable.tf, and add details regarding file creation by providing the new file complete path.
variable "filename" {
default = "/Users/nahid/My_personal_work/Devops/DevopsWithSubham/DevOps_code/Terraform-practice/blog_practice/demo-var.txt"
}
variable "content" {
default = "Hey, This is Nahid. This is a testing of variable"
}
Create a main.tf file where we will access the variables that have defined before.
resource "local_file" "devops" {
filename = var.filename
content = var.content
}
Run the below command to apply terraform configuration and then the file is created by using a variable.
terraform init
terraform plan
terraform apply
Data Types in Terraform
Use terraform to demonstrate usage of List, Set and Object datatypes
In Terraform, variables and resource attributes can have different data types. Here are the commonly used data types in Terraform.
String: Represents a sequence of characters. Strings are enclosed in double quotes ("") or single quotes ('').
For example: "us-west-2".
Number: Represents numerical values. Numbers can be integers or floating-point values.
For example: 42, 3.14.
Bool: Represents boolean values, which can be either true or false.
List: Represents an ordered collection of values. Lists are enclosed in square brackets ([]). Each element in the list can be of any data type.
For example: ["value1", "value2", "value3"].
Map: Represents a collection of key-value pairs. Maps are enclosed in curly braces ({}), and each element in the map is defined as "key" = "value". Both the key and value can be of any data type.
For example: {"key1" = "value1", "key2" = "value2"}.
variable "file_contents" {
type = map
default = {
"statement1" = "this is cool"
"statement2" = "this is cooler"
}
}
Set: Represents an unordered collection of unique values. Sets are similar to lists but do not allow duplicate values. Sets are enclosed in curly braces ({}) and use the ["value1", "value2"] syntax.
variable "ingress_rules" {
type = set(object({
from_port = number
to_port = number
protocol = string
cidr_blocks = list(string)
}))
default = [
{
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
},
{
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
]
}
resource "aws_security_group" "example_security_group" {
ingress {
from_port = var.ingress_rules[0].from_port
to_port = var.ingress_rules[0].to_port
protocol = var.ingress_rules[0].protocol
cidr_blocks = var.ingress_rules[0].cidr_blocks
}
ingress {
from_port = var.ingress_rules[1].from_port
to_port = var.ingress_rules[1].to_port
protocol = var.ingress_rules[1].protocol
cidr_blocks = var.ingress_rules[1].cidr_blocks
}
vpc_id = aws_vpc.example_vpc.id
}
Object: Represents a complex data structure with multiple attributes. Objects are defined using the . syntax to access nested attributes.
variable "vpc_config" {
type = object({
cidr_block = string
instance_tenancy = string
})
default = {
cidr_block = "10.0.0.0/16"
instance_tenancy = "default"
}
}
resource "aws_vpc" "example_vpc" {
cidr_block = var.vpc_config.cidr_block
instance_tenancy = var.vpc_config.instance_tenancy
}
Thank you for reading this blog. If you found this blog helpful, please like, share, and follow me for more blog posts like this in the future.
— Happy Learning !!!