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.
Here we use Terraform Variables to create a local file using Terraform Block types like provider, resource, variable, output, etc.
- Example of Provider:
provider "aws" {
region = "us-east-1"
}
- Example of Resource:
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
}
- Example of Variable:
variable "filename" {
default = ""
}
- Example of Output:
output "instance_ip_addr" {
value = aws_instance.example.public_ip
}
Task-1:Create a local file using Terraform
Step 1: Create a main.tf file and write the code for creating a local file using Terraform.
resource "local_file" "devops" { filename = var.filename content = var.content }
- Step 2: Create a variables.tf file and write the code for creating a variable.
variable "filename" {
default = "/home/ubuntu/Documents/terraform/day2/demo.txt"
}
variable "content" {
default = "This is a sample file.
}
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
we can see a file is created with the content that we had given in the variable file
Task-02: Data Types in Terraform
Data Types in Terraform.
What are the Data Types in Terraform?
Terraform supports several data types:
String: Holds text or characters.
Number: Stores numeric values (integers or floats).
Bool: Represents boolean values (true or false).
List: An ordered collection of values.
Map: Holds key-value pairs.
Object (Complex Type): Complex data type for structured data.
What is Map in Terraform?
In Terraform, a map is a data structure that stores key-value pairs. It allows you to
organize
andstore data
efficiently.Example of Map in Terraform:
variable "example_map" {
type = map(string)
default = {
key1 = "value1"
key2 = "value2"
key3 = "value3"
}
}
provider "aws"{
region = "ap-south-1"
}
resource "local_file" "example3"{
filename = "/home/rohit/Documents/terraform/day3/devops.txt"
content = var.file_contents["statement1"]
}
resource "local_file" "example4"{
filename = "/home/rohit/Documents/terraform/day3/devops+cloud.txt"
content = var.file_contents["statement2"]
}
- Step 2: Let's create a variables.tf file and write the code for creating a map in Terraform.
variable "filename"{ default = "/home/ubuntu/Documents/terraform/demo/demo.txt" } varible "content"{ default = "Hello World ..." } variable "file_contents"{ type = map default = { "statement1" = "This is a sample file." "statement2" = "This is a sample file for Devops and Cloud." } }
- Now run the command
terraform init
to initialize the Terraform.
terraform init
- Now run the command
terraform apply
to create a map in Terraform.
terraform apply
Now verify the map using the command cat
Task-03:
What is a list in Terraform?
In Terraform, a list is a data type that represents an
ordered collection
ofvalues
. Lists maintain the order of elements and can contain values of the same or different data types.They are declared using
square brackets
and accessed by index. Lists are versatile for storing and working withmultiple values within a single variable.
variable "example_list" {
type = list
default = ["item1", "item2", "item3"]
}
- Let's create a
variables.tf
file and write the code for creating a list in Terraform.
variable "file_contents"{
type = map
default = {
"statement1" = "This is a sample file."
"statement2" = "This is a sample file for Devops and Cloud."
}
}
variable "file_list_type"{
type = list
default = [
"/home/Akshay/Documents/terraform/day3/devops.txt",
"/home/Akshay/Documents/terraform/day3/devops+cloud.txt"
]
}
- At last, run the command
terraform apply
to create a list in Terraform.
What is an Object in Terraform?
In Terraform, an
"object"
is a complex adata structure
that organizes related data using key-value pairs. It allows you to representstructured data
with various attributes or properties, each with its owndata type
.Objects are useful for
organizing
andmanaging hierarchical
or structured information withinTerraform configurations
.Step 1: let's create a
main.tf
file and write the code for creating an object in Terraform.
provider "aws" {
region = "ap-south-1"
}
output "aws_ec2_object" {
value = var.aws_ec2_object
}
- Let's create a
variables.tf
file and write the code for creating an object in Terraform.
variable "aws_ec2_object" {
type = object({
name = string
instance = number
keys = list(string)
ami = string
})
default = {
name = "MyEC2"
instance = 1
keys = ["apache.pem"]
ami = "ami-0f5ee92e2d63afc18"
}
}
In Terraform, a "set" typically refers to creating a distinct collection of resources or modules based on conditions or input data. This is achieved using the
count
orfor_each
arguments to dynamically manage instances within Terraform configurations.While Terraform doesn't have a specific "set" data type, it allows you to achieve similar functionality by selectively creating or managing resources. Keep in mind that Terraform capabilities may have evolved since my last update, so consult the latest documentation for any changes.
Step 1: let's create a
main.tf
file and write the code for creating a set in Terraform.
provider "aws" {
region = "ap-south-1"
}
output "sercurity_groups" {
value = var.sercurity_groups
}
After running the terraform init
command run terraform plan
to create a Set in Terraform.
- Use
terraform refresh
to refresh the state by your configuration file, and reload the variables.
- Use
terraform refresh