Terraform Notes
Terraform Notes
Terraform Notes
=========================================
Installation Terraform
# terraform -h
-----------------------------------------------------------------------------------
-----------------------------------------
# vi first.tf
output hello {
value = "Hello World"
}
# terraform plan
-----------------------------------------------------------------------------------
-----------------------------------------
# vi first.tf
{
"output" : {
"Hello" : {
"value" : "Hello Ganesh"
}
}
}
# terraform plan
-----------------------------------------------------------------------------------
-----------------------------------------
# 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}"
}
-----------------------------------------------------------------------------------
-----------------------------------------
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}"
}
-----------------------------------------------------------------------------------
-----------------------------------------
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