Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 5

==============================================================Terraform============

=========================================

Installation Terraform

# sudo yum install -y yum-utils


# sudo yum-config-manager --add-repo
https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
# sudo yum -y install terraform

# terraform -h

-----------------------------------------------------------------------------------
-----------------------------------------

Example 1 - Visual Studio Code

# vi first.tf

output hello {
value = "Hello World"
}

# terraform plan
-----------------------------------------------------------------------------------
-----------------------------------------

Example 2 - json format

# vi first.tf

{
"output" : {
"Hello" : {
"value" : "Hello Ganesh"
}
}
}

# terraform plan
-----------------------------------------------------------------------------------
-----------------------------------------

Example 3 - mutiple block in single terraform file

# vi example3.tf

output "firstblock" {
value = "First Block"
}

output "secondblock" {
value = "Second Block"
}

# terraform plan
-----------------------------------------------------------------------------------
-----------------------------------------
Example 4 - Mutiple terraform file in same directory

# vi first.tf

output first {
value = "First"
}

# vi second.tf

output second {
value = "Second"
}

# vi third.tf

output third {
value = "Third"
}

# terraform plan

-----------------------------------------------------------------------------------
-----------------------------------------
Example 5 - Variables

# vi variable.tf

variable username {}

# vi call-variable.tf

output PrintVariable {
value = "Print Variable ${var.username}"
}

# terraform plan
-----------------------------------------------------------------------------------
-----------------------------------------
Example 6 - pass variable from command

# vi variable.tf

variable username {}

# vi call-variable.tf

output PrintVariable {
value = "Print Variable ${var.username}"
}

# terraform plan -var username="Ganesh"

-----------------------------------------------------------------------------------
-----------------------------------------
Example 7 - multiple variable pass from command

# vi variable.tf
variable firstname {}

variable secondname {}

# vi call-variable.tf

output PrintVariable {
value = "Print Variable ${var.firstname}, and second name is ${var.secondname}"
}

# terraform plan -var firstname="Ganesh" -var secondname="Bagde"

-----------------------------------------------------------------------------------
-----------------------------------------
Example 8 - set default variable

# vi variable.tf

variable first{
default = "World"
}

variable second{
default = "Ganesh Bagde"
}

# vi call-variable.tf

output PrintVariable {
value = "Hello ${var.first}, And My name is ${var.second}"
}

# terraform plan

-----------------------------------------------------------------------------------
-----------------------------------------
Example 9 - Variable Types - number, string, list

variable stringVariable{
type = string
default = "Ganesh Bagde"
}

variable numVariable{
type = number
default = 38
}

variable listusers {
type = list
default = ["Ganesh", "Saanvi", "Pinky"]
}

# vi call-variable.tf
output PrintVariable {
value = "My name is ${var.stringVariable}, and my age is ${var.numVariable},
Only list user ${var.listusers[0]}"
}
# terraform plan

-----------------------------------------------------------------------------------
-----------------------------------------
Example 10 - Function call (join, upper, lower, title)

variable stringVariable{
type = list
default = ["Ganesh", "saanvi"]
}

# vi call-variable.tf

output PrintVariable {
value = "My name is ${join(",",[var.stringVariable[0]])}"
}

# terraform plan

-----------------------------------------------------------------------------------
-----------------------------------------
Example 11 - map Variable with lookup function

variable mapVariable{
type = map
default = {
Ganesh = 38
saanvi = 8
}
}

# vi call-variable.tf

output PrintVariable {
value = "My name is ${lookup(var.mapVariable, "Ganesh")}"
}

# terraform plan

-----------------------------------------------------------------------------------
-----------------------------------------
Example 12 - Dynamic Call map Variable with lookup function

variable mapVariable{
type = map
default = {
Ganesh = 38
saanvi = 8
}
}

# vi call-variable.tf
output PrintVariable {
value = "My name is ${var.mapVariable}" and my age is ${lookup(var.mapVariable)}
}

# terraform plan

You might also like