Terraform KodeKloud
Terraform KodeKloud
Challenges
Field Engineers
Procurement
System/ NW
Infrastructure Team
Administrators
Solution Architect
Business Business Analyst Storage Admins
/ Technical Lead
Backup Admins
Application team
Data Center
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Field Engineers
Procurement
System/ NW
Infrastructure Team
Administrators
Solution Architect
Business Business Analyst Storage Admins
/ Technical Lead
Slow Deployment
Expensive
Backup Admins
Limited Automation
System/ NW
Infrastructure Team
Administrators
Solution Architect
Business Business Analyst Storage Admins
/ Technical Lead
Slow Deployment
Expensive
Backup Admins
Limited Automation
System/ NW
Infrastructure Team
Administrators
Solution Architect
Business Business Analyst Storage Admins
/ Technical Lead
Backup Admins
Slow Delivery
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Infrastructure as Code
© Copyright K odeK l oud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
© Copyright KodeKloud
Infrastructure as
Code
Infrastructure as Code
ec2.sh
#!/bin/bash
IP_ADDRESS="10.2.2.1"
EC2_INSTANCE=$(ec2-run-instances --instance-type
t2.micro ami-0edab43b6fa892279)
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Infrastructure as Code
ec2.sh main.tf
#!/bin/bash
resource "aws_instance" "webserver" {
IP_ADDRESS="10.2.2.1"
ami = "ami-0edab43b6fa892279"
EC2_INSTANCE=$(ec2-run-instances --instance-type instance_type = "t2.micro"
t2.micro ami-0edab43b6fa892279) }
INSTANCE=$(echo ${EC2_INSTANCE} | sed 's/*INSTANCE //'
| sed 's/ .*//')
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Infrastructure as Code
ec2.yaml main.tf
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Types of IAC Tools
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Types of IAC Tools
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Types of IAC Tools
Configuration Management
Designed to Install and Manage Software
Version Control
Idempotent
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Server Templating Tools
Immutable Infrastructure
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Provisioning Tools
Multiple Providers
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
© Copyright KodeKloud
Terraform
Why Terraform?
Physical Machines
VMWare
AWS
GCP
Azure
© Copyright K odeK l oud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Providers
VMWare CloudFlare
AWS DNS
Azure Infoblox
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Providers
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Providers
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
HashiCorp Configuration Language
main.tf
© Copyright K odeK l oud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Terraform State
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Terraform Import
© Copyright K odeK l oud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Terraform Cloud and Terraform Enterprise
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
$ terraform version
Terraform v0.13.0
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
HCL – Declarative Language
aws.tf
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Resource
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Resource
© Copyright K odeK l oud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
HCL Basics
>_
$ mkdir /root/terraform-local-file
$ cd /root/terraform-local-file
local.tf
resource "local_file"
<block> <parameters> { "pet" {
filename = "/root/pets.txt"
key1 = value1
key2 = value2
content = "We love pets!"
}}
© Copyright K odeK l oud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
local=provider
file=resource
Block Resource Resource
Name Type Name
FILENAME CONTENT
local.tf
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
aws-ec2.tf
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
aws-s3.tf
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
local.tf
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
local.tf
>_
$ terraform init
Initializing the backend...
To prevent automatic upgrades to new major versions that may contain breaking
changes, we recommend adding version constraints in a required_providers block
in your configuration, with the constraint strings suggested below.
------------------------------------------------------------------------
------------------------------------------------------------------------
$ cat /root/pets.txt
© Copyright KodeKloud We love pets! Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
>_
$ terraform show
# local_file.pet:
resource "local_file" “pet" {
content = "We love pets!"
directory_permission = "0777"
file_permission = "0777"
filename = "/root/pets.txt"
id = "cba595b7d9f94ba1107a46f3f731912d95fb3d2c"
}
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
local=provider
file=resource
Resource
Type
local.tf
© Copyright K odeK l oud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
provider
resource_type
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
https://registry.terraform.io/providers/hashicorp/local/latest/docs
provider Local provider
resource_type local_file
filename (required)
Content (optional)
Arguments file_permission (optional)
directory_permission (optional)
sensitive_content (optional)
content_base64 (optional)
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
HANDS-ON LABS
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Update and Destroy
Infrastructure
>_
$ terraform plan
local_file.pet: Refreshing state...
[id=5f8fb950ac60f7f23ef968097cda0a1fd3c11bdf]
----------------------------------------------------------------------
local.tf An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
resource "local_file" "pet" { -/+ destroy and then create replacement
----------------------------------------------------------------------
Note: You didn't specify an "-out" parameter to save this plan, so
Terraform
can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
>_ >_
$ ls -ltr /root/pets.txt
$ terraform apply
-rwx------ 1 root root 30 Aug 17 23:20 pet.txt
# local_file.pet must be replaced
-/+ resource "local_file" "pet" {
content = "We love pets!"
directory_permission = "0777"
~ file_permission = "0777" -> "0700" # forces replacement
filename = "/root/pet.txt"
~ id =
"5f8fb950ac60f7f23ef968097cda0a1fd3c11bdf" -> (known after apply)
}
local_file.pet: Destroying...
[id=5f8fb950ac60f7f23ef968097cda0a1fd3c11bdf]
local_file.pet: Destruction complete after 0s
local_file.pet: Creating...
local_file.pet: Creation complete after 0s
[id=5f8fb950ac60f7f23ef968097cda0a1fd3c11bdf]
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Terraform Basics
Using Terraform
Providers
>_
$ terraform init
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Official
Verified
Community
© Copyright KodeKloud
registry.terraform.i
Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
o
>_ >_
$ terraform init $ ls /root/terraform-local-file/.terraform
Initializing the backend... plugins
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
>_
$ terraform init
Initializing the backend...
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
To prevent automatic upgrades to new maj
contain breaking
changes, we recommend adding version con
required_providers block
in your configuration, with the constrai
below.
Organizational
Type
Namespace
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
To prevent automatic upgrades to new maj
contain breaking
changes, we recommend adding version con
required_providers block
in your configuration, with the constrai
below.
* registry.terraform.io/ hashicorp/loc
Hostname Organizational
Ty
Namespace
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Initializing provider plugins...
- Finding latest version of hashicorp/local...
- Installing hashicorp/local v2.0.0...
- Installed hashicorp/local v2.0.0 (signed by Ha
local.tf cat.tf
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
local.tf cat.tf
main.tf
File Name Purpose
resource "local_file" "pet" {
Main configuration file containing resource
filename = "/root/pets.txt" main.tf
definition
content = "We love pets!"
} variables.tf Contains variable declarations
}
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
HANDS-ON LABS
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Multiple Providers
main.tf
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
main.tf
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
>_
$ terraform init
Initializing the backend...
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
>_
$ terraform plan
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but
will not be
persisted to local or remote state storage.
.
.
# random_pet.my-pet will be created
+ resource "random_pet" "my-pet" {
+ id = (known after apply)
+ length = 1
+ prefix = "Mrs"
+ separator = "."
}
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
>_
$ terraform apply
local_file.new_file: Refreshing state...
[id=d1a31467f206d6ea8ab1cad382bc106bf46df69e]
random_pet.my-pet: Creating...
random_pet.my-pet: Creation complete after 0s [id=Mrs.hen]
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Define Input
Variables
main.tf
Argument Value
resource "local_file" "pet" {
filename = "/root/pets.txt" filename "/root/pets.txt"
content = "We love pets!"
content "We love pets!"
}
prefix "Mrs"
resource "random_pet" "my-pet" {
prefix = "Mrs" separator "."
separator = "."
length "1"
length = "1"
}
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
main.tf variables.tf
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
main.tf variables.tf
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
>_
$ terraform apply
# local_file.pet will be created
+ resource "local_file" "pet" {
+ content = “We love pets!"
+ directory_permission = "0777"
+ file_permission = "0777"
+ filename = "/root/pet.txt"
+ id = (known after apply)
}
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
>_
$ terraform apply
Terraform will perform the following actions:
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Understanding the
Variable Block
variables.tf
variable "filename" {
default = "/root/pets.txt"
}
variable "content" {
default = "I love pets!"
}
variable "prefix" {
default = "Mrs"
}
variable "separator" {
default = "."
}
variable "length" {
default = "1"
}
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
variables.tf
variable "filename" {
default = "/root/pets.txt"
type = string
description = "the path of local file"
}
variable "content" {
default = "I love pets!"
type = string
description = “the content of the file"
}
variable "prefix" {
default = "Mrs"
type = string
description = "the prefix to be set"
}
variable "separator" {
default = "."
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
variables.tf Type Example
} variable "password_change" {
variable "separator" { default = true
default = "." type = bool
© Copyright KodeKloud } course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Check out our full
Type Example
string "/root/pets.txt"
number 1
bool true/false
pet1 = cat
map
pet2 = dog
Complex Data
object
Structure
Complex Data
tuple
Structure
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
List
variables.tf maint.tf
Index Value
0 Mr
1 Mrs
2 Sir
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Map
variables.tf maint.tf
Key Value
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
List of a Type
variables.tf variables.tf
variables.tf >_
$ terraform plan
variable "prefix" { Error: Invalid default value for variable
variables.tf variables.tf
variable "cats" { variable "pet_count" {
default = { default = {
"color" = "brown" "dogs" = 3
"name" = "bella" "cats" = 1
} "goldfish" = 2
type = map(string) }
} type = map(number)
}
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Set
variables.tf variables.tf
variable "prefix" { variable "prefix" {
default = ["Mr", "Mrs", "Sir"] default = ["Mr", "Mrs", "Sir", "Sir"]
type = set(string) type = set(string)
} }
variables.tf variables.tf
variable "fruit" { variable "fruit" {
default = ["apple", "banana"] default = ["apple", "banana", "banana"]
type = set(string) type = set(string)
} }
variables.tf variables.tf
variable "age" { variable "age" {
default = [10, 12, 15] default = [10, 12, 15, 10]
type = set(number) type = set(number)
} KodeKloud
© Copyright } full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Check out our
Objects
Key Example Type
variables.tf
name bella string
variable "bella" {
color brown string type = object({
name = string
age 7 number color = string
age = number
food ["fish", "chicken", "turkey"] list
food = list(string)
favorite_pet true bool
favorite_pet = bool
})
default = {
name = "bella"
color = "brown"
age = 7
food = ["fish", "chicken", "turkey"]
favorite_pet = true
}
}
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Tuples
variables.tf
variable kitty {
type = tuple([string, number, bool])
default = ["cat", 7, true]
variables.tf >_
$ terraform plan
variable kitty { Error: Invalid default value for variable
type = tuple([string, number, bool])
default = ["cat", 7, true, "dog"] on variables.tf line 3, in variable "kitty":
3: default = ["cat", 7, true, "dog"]
} This default value is not compatible with the
variable's type constraint:
tuple required.
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
HANDS-ON LABS
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Using Variables in
Terraform
main.tf variables.tf
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
main.tf variables.tf
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Interactive Mode
>_
$ terraform apply
var.content
Enter a value: We love Pets!
var.filename
Enter a value: /root/pets.txt
var.length
Enter a value: 2
var.prefix
Enter a value: Mrs.
var.separator
Enter a value: .
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Command Line Flags
>_
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Environment Variables
>_
$ export TF_VAR_filename="/root/pets.txt"
$ export TF_VAR_content="We love pets!"
$ export TF_VAR_prefix="Mrs"
$ export TF_VAR_separator="."
$ export TF_VAR_length="2"
$ terraform apply
© Copyright K odeK l oud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Variable Definition Files
terraform.tfvars
filename = "/root/pets.txt"
content = "We love pets!"
prefix = "Mrs"
separator = "."
length = "2"
>_
terraform.tfvars | terraform.tfvars.json
Automatically Loaded
*.auto.tfvars | *.auto.tfvars.json
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Variable Definition Precedence
main.tf >_
resource local_file pet {
$ export TF_VAR_filename="/root/cats.txt" ?
filename = var.filename
}
terraform.tfvars
variables.tf
filename = "/root/pets.txt" ?
variable filename {
type = string
} variable.auto.tfvars
filename = "/root/mypet.txt" ?
>_
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Variable Definition Precedence
variable.auto.tfvars
filename = "/root/mypet.txt" 3?
>_
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
HANDS-ON LABS
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Resource Attribute
Reference
FILENAME CONTENT PREFIX SEPERATOR
main.tf
LENGTH
resource "local_file" "pet" {
filename = var.filename
content = "My favorite pet is Mr.Cat"
}
>_
random_pet.my-pet: Creating...
local_file.pet: Creating...
random_pet.my-pet: Creation complete after 0s [id=Mr.bull]
local_file.pet: Creation complete after 0s
[id=059090e865809f9b6debfda7aebf48fdce2220a6]
main.tf
LENGTH
resource "local_file" "pet" {
filename = var.filename
content = "My favorite pet is Mr.Cat"
}
>_
random_pet.my-pet: Creating...
local_file.pet: Creating...
random_pet.my-pet: Creation complete after 0s [id=Mr.bull]
local_file.pet: Creation complete after 0s
[id=059090e865809f9b6debfda7aebf48fdce2220a6]
main.tf
resource "local_file" "pet" {
filename = var.filename
${random_pet.my-pet.id}"
content = "My favorite pet is Mr.Cat"
}
>_
random_pet.my-pet: Creating...
local_file.pet: Creating...
random_pet.my-pet: Creation complete after 0s [id=Mr.bull]
local_file.pet: Creation complete after 0s
[id=059090e865809f9b6debfda7aebf48fdce2220a6]
om_pet" "my-pet" {
r.prefix
var.separator
r.length
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
>_
$ terraform apply
.
.
.
# local_file.pet must be replaced
-/+ resource "local_file" "pet" {
~ content = "My favorite pet is Mrs.Cat!" ->
"My favorite pet is Mr.bull" # forces replacement
directory_permission = "0777"
file_permission = "0777"
filename = "/roots/pets.txt"
~ id =
"98af5244e23508cffd4a0c3c46546821c4ccbbd0" -> (known after
apply)
}
.
.
local_file.pet: Destroying...
[id=98af5244e23508cffd4a0c3c46546821c4ccbbd0]
local_file.pet: Destruction complete after 0s
local_file.pet: Creating...
local_file.pet: Creation complete after 0s
[id=e56101d304de7cf1b1001102923c6bdeaa60c523]
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
HANDS-ON LABS
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Resource
Dependencies
Implicit Dependency
main.tf
resource "local_file" "pet" {
filename = var.filename 2 1
${random_pet.my-pet.id}"
content = "My favorite pet is Mr.Cat"
}
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Explicit Dependency
main.tf
resource "local_file" "pet" {
filename = var.filename 2 1
content = "My favorite pet is Mr.Cat"
depends_on = [
random_pet.my-pet
]
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
HANDS-ON LABS
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Output Variables
main.tf variables.tf
resource "local_file" "pet" { variable "filename" {
filename = var.filename default = "/root/pets.txt"
${random_pet.my-pet.id}"
content = "My favorite pet is Mr.Cat" }
} variable "content" {
default = "I love pets!"
resource "random_pet" "my-pet" { }
prefix = var.prefix variable "prefix" {
separator = var.separator default = "Mrs"
length = var.length }
} variable "separator" {
default = "."
output pet-name { }
value = random_pet.my-pet.id variable "length" {
description = "Record the value of pet ID generated by the default = "1"
random_pet resource" }
}
output "<variable_name>" {
value = "<variable_value>"
<arguments>
}
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
>_
$ terraform apply
.
.
Outputs:
pet-name = Mrs.gibbon
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
>_
$ terraform output
pet-name = Mrs.gibbon
>_
$ terraform output pet-name
Mrs.gibbon
Output Variable
SHELL SCRIPTS
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
HANDS-ON LABS
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Introduction to
Terraform State
>_ >_
$ cd terraform-local-file
$ ls terraform-local-file [terraform-local-file]$ terraform init
main.tf variables.tf
Initializing the backend...
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
>_ >_
[terraform-local-file]$ terraform plan
$ ls terraform-local-file
Refreshing Terraform state in-memory prior to plan...
main.tf variables.tf
The refreshed state will be used to calculate this plan,
persisted to local or remote state storage.
main.tf
--------------------------------------------------------
resource "local_file" "pet" {
filename = var.filename An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbol
content = var.content + create
} Plan
Init
Terraform will perform the following actions:
variables.tf
# local_file.pet will be created
variable "filename" { + resource "local_file" "pet" {
+ content = "I love pets!"
default = "/root/pets.txt" + directory_permission = "0777"
} + file_permission = "0777"
variable "content" { + filename = "/root/pets.txt"
+ id = (known after apply)
default = "I love pets!" }
}
Plan: 1 to add, 0 to change, 0 to destroy.
--------------------------------------------------------
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Note: You didn't specify an "-out" parameter to save thi
>_ >_
[terraform-local-file]$ terraform apply
$ ls terraform-local-file
An execution plan has been generated and is shown below.
main.tf variables.tf
Resource actions are indicated with the following symbol
+ create
main.tf Terraform will perform the following actions:
local_file.pet: Creating...
local_file.pet: Creation complete after 0s
© Copyright KodeKloud [id=7e4db4fbfdbb108bdd04692602bae3e9bd1e1b68]
Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
>_ >_
Apply
Plan
Init
© Copyright K odeK l oud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
>_ >_
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Purpose of State
Real World Infrastructure terraform.tfstate
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Tracking Metadata
pet
main.tf
resource "local_file" "pet" {
filename = "/root/pet.txt"
content = "My favorite pet is ${random_pet.my-pet.id}!"
}
resource "random_pet" "my-pet" {
length = 1 my-pet
}
resource "local_file" "cat" {
filename = "/root/cat.txt"
content = "I like cats too!"
}
cat
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Tracking Metadata
>_
$ terraform apply pet
.
.
.
Plan: 3 to add, 0 to change, 0 to destroy.
local_file.cat: Creating...
random_pet.my-pet: Creating...
local_file.cat: Creation complete after 0s
[id=fe448888891fc40342313bc44a1f1a8986520c89]
random_pet.my-pet: Creation complete after 0s [id=yak] cat
local_file.pet: Creating...
local_file.pet: Creation complete after 0s
[id=28b373c6c1fa3fce132a518eadd0175c98f37f20]
pet
main.tf
resource "local_file" "pet" {
filename = "/root/pet.txt"
content = "My favorite pet is ${random_pet.my-pet.id}!"
}
resource "random_pet" "my-pet" {
length = 1 my-pet
}
resource "local_file" "cat" {
filename = "/root/cat.txt"
content = "I like cats too!"
}
cat
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Tracking Metadata
pet
main.tf
my-pet
cat
© Copyright K odeK l oud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Tracking Metadata
pet
main.tf
resource "local_file" "cat" {
filename = "/root/cat.txt"
content = "I like cats too!"
}
my-pet
>_
$ cat terraform.tfstate
{
"mode": "managed",
"type": "local_file",
"name": "pet",
"instances": [ cat
{
"schema_version": 0,
"attributes": {
"content": "My favorite pet is yak!",
. },
"private": "bnVsbA==",
"dependencies": [
"random_pet.my-pet"
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
]
Tracking Metadata
pet
main.tf
resource "local_file" "cat" {
filename = "/root/cat.txt"
content = "I like cats too!"
}
my-pet
>_
$ terraform apply
Plan: 0 to add, 0 to change, 2 to destroy.
local_file.pet: Destroying...
[id=28b373c6c1fa3fce132a518eadd0175c98f37f20]
local_file.pet: Destruction complete after 0s
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
random_pet.my-pet: Destroying... [id=yak]
Performance
terraform.tfstate
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Performance
terraform.tfstate >_
{ $ terraform plan --refresh=false
"version": 4, An execution plan has been generated and is shown
"terraform_version": "0.13.0", below.
"serial": 4, Resource actions are indicated with the following
"lineage": "e35dde72-a943-de50-3c8b-1df8986e5a31", symbols:
"outputs": {}, -/+ destroy and then create replacement
"resources": [
{ Terraform will perform the following actions:
"mode": "managed",
"type": "local_file", # local_file.cat must be replaced
"name": "pet", -/+ resource "local_file" "pet" {
"instances": [ ~ content = "I like cats too!" ->
{ "Dogs are awesome!" # forces replacement
"schema_version": 0, directory_permission = "0777"
"attributes": { file_permission = "0777"
"content": "We love pets!", filename = "/root/pets.txt"
"content_base64": null, ~ id =
"directory_permission": "0777", "cba595b7d9f94ba1107a46f3f731912d95fb3d2c" -> (known
... after apply)
}
terraform.tfstate >_
{ $ ls
"version": 4,
"terraform_version": "0.13.0", main.tf variables.tf terraform.tfstate
"serial": 4,
"lineage": "e35dde72-a943-de50-3c8b-1df8986e5a31",
"outputs": {},
"resources": [
{
"mode": "managed",
"type": "local_file",
"name": "pet",
"instances": [
{
"schema_version": 0, terraform.tfstate
"attributes": {
"content": "We love pets!",
"content_base64": null,
"directory_permission": "0777",
...
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
AWS S3 HashiCorp Consul
Collaboration Google Cloud Storage Terraform Cloud
terraform.tfstate
{
"version": 4,
"terraform_version": "0.13.0",
"serial": 4, terraform.tfstate
"lineage": "e35dde72-a943-de50-3c8b-1df8986e5a31",
"outputs": {},
"resources": [
{
"mode": "managed",
"type": "local_file",
"name": "pet",
"instances": [
{
"schema_version": 0,
"attributes": {
"content": "We love pets!",
"content_base64": null,
"directory_permission": "0777",
...
© Copyright K odeK l oud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
HANDS-ON LABS
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Terraform State
Considerations
Sensitive Data
terraform.tfstate
{
"mode": "managed",
"type": "aws_instance",
"name": "dev-ec2",
"provider": "provider[\"registry.terraform.io/hashicorp/aws\"]",
"instances": [
{
"schema_version": 1,
"attributes": {
"ami": "ami-0a634ae95e11c6f91",
.
.
.
"primary_network_interface_id": "eni-0ccd57b1597e633e0",
"private_dns": "ip-172-31-7-21.us-west-2.compute.internal",
"private_ip": "172.31.7.21",
"public_dns": "ec2-54-71-34-19.us-west-2.compute.amazonaws.com",
"public_ip": "54.71.34.19",
"root_block_device": [
{
"delete_on_termination": true,
"device_name": "/dev/sda1",
"encrypted": false,
© Copyright KodeKloud "iops": 100, Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Terraform State Considerations
Remote State Backends Version
Control
terraform.tfstate main.tf
{ resource "local_file" "pet" {
"mode": "managed",
filename = "/root/pet.txt"
"type": "aws_instance",
"name": "dev-ec2", content = "My favorite pet is Mr.Whiskers!"
"provider": "provider[\"registry.terraform.io/hashicorp/aws\"]", }
"instances": [ resource "random_pet" "my-pet" {
{
"schema_version": 1,
length = 1
"attributes": { }
"ami": "ami-0a634ae95e11c6f91", resource "local_file" "cat" {
. filename = "/root/cat.txt"
.
. content = "I like cats too!"
"primary_network_interface_id": "eni-0ccd57b1597e633e0", }
"private_dns": "ip-172-31-7-21.us-west-2.compute.internal",
"private_ip": "172.31.7.21",
"public_dns": "ec2-54-71-34-19.us-west-2.compute.amazonaws.com",
"public_ip": "54.71.34.19",
"root_block_device": [
{
"delete_on_termination": true,
"device_name": "/dev/sda1",
"encrypted": false,
"iops": 100,
"kms_key_id": "",
© Copyright K odeK"volume_id":
l oud "vol-070720a3636979c22", Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
No Manual Edits
terraform.tfstate
{
"mode": "managed",
"type": "aws_instance",
"name": "dev-ec2",
"provider": "provider[\"registry.terraform.io/hashicorp/aws\"]",
"instances": [
{
"schema_version": 1,
"attributes": {
"ami": "ami-0a634ae95e11c6f91",
.
.
.
"primary_network_interface_id": "eni-0ccd57b1597e633e0",
"private_dns": "ip-172-31-7-21.us-west-2.compute.internal",
"private_ip": "172.31.7.21",
"public_dns": "ec2-54-71-34-19.us-west-2.compute.amazonaws.com",
"public_ip": "54.71.34.19",
"root_block_device": [
{
"delete_on_termination": true,
"device_name": "/dev/sda1",
"encrypted": false,
© Copyright KodeKloud "iops": 100, Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Terraform Commands
terraform validate
main.tf >_
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
terraform fmt
main.tf >_
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
terraform fmt
main.tf >_
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
terraform show
>_ >_
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
terraform providers
main.tf >_
provider[registry.terraform.io/hashicorp/local]
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
terraform output
main.tf >_
}
output pet-name {
value = random_pet.cat.id
sensitive = false
description = "Print the name of the pet"
© Copyright K odeK l oud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
terraform refresh
main.tf >_
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
terraform graph
main.tf >_
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
HANDS-ON LABS
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Mutable vs
Immutable
Infrastructure
terraform validate
main.tf >_
$ terraform apply
resource "local_file" "pet" {
filename = "/root/pets.txt" # local_file.pet must be replaced
content = "We love pets!" -/+ resource "local_file" "pet" {
content = "We love pets!"
file_permission = "0700"
directory_permission = "0777"
}
~ file_permission = "0777" -> "0700" # forces
replacement
filename = "/root/pet.txt"
~ id =
"5f8fb950ac60f7f23ef968097cda0a1fd3c11bdf" -> (known after
apply)
}
local_file.pet: Destroying...
[id=5f8fb950ac60f7f23ef968097cda0a1fd3c11bdf]
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
local_file.pet: Destruction complete after 0s
v1.19
v1.18
upgrade-nginx.sh
v1.17
© Copyright K odeK l oud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Mutable Infrastructure
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Configuration Drift
v1.19 v1.19
v1.18 v1.18 v1.18
v1.17 v1.17 v1.17
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
v1.17 v1.17 v1.17
1 2 3
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
4
v1.17 v1.17 v1.17 v1.18
1 2 3
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
4 5
v1.17 v1.17 v1.18 v1.18
2 3
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
4 5 6
v1.17 v1.18 v1.18 v1.18
© Copyright K odeK l oud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Immutable Infrastructure
4 5 6
v1.18 v1.18 v1.18
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Immutable Infrastructure
4 5
v1.17 v1.18 v1.18
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Immutable Infrastructure
main.tf >_
$ terraform apply
resource "local_file" "pet" {
filename = "/root/pets.txt" # local_file.pet must be replaced
content = "We love pets!" -/+ resource "local_file" "pet" {
content = "We love pets!"
file_permission = "0700"
directory_permission = "0777"
}
~ file_permission = "0777" -> "0700" # forces
replacement
filename = "/root/pet.txt"
~ id =
"5f8fb950ac60f7f23ef968097cda0a1fd3c11bdf" -> (known after
apply)
}
local_file.pet:
local_file.pet: Destroying...
Destroying...
[id=5f8fb950ac60f7f23ef968097cda0a1fd3c11bdf]
[id=5f8fb950ac60f7f23ef968097cda0a1fd3c11bdf]
local_file.pet:
local_file.pet: Destruction
Destruction complete
complete after
after 0s
0s
local_file.pet: Creating...
local_file.pet: Creating...
local_file.pet:
local_file.pet: Creation
Creation complete
complete after
after 0s
0s
[id=5f8fb950ac60f7f23ef968097cda0a1fd3c11bdf]
[id=5f8fb950ac60f7f23ef968097cda0a1fd3c11bdf]
© Copyright K odeK l oud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
© Copyright KodeKloud
Lifecycle Rules
main.tf >_
$ terraform apply
resource "local_file" "pet" {
filename = "/root/pets.txt" # local_file.pet must be replaced
content = "We love pets!" -/+ resource "local_file" "pet" {
content = "We love pets!"
file_permission = "0700"
directory_permission = "0777"
}
~ file_permission = "0777" -> "0700" # forces
replacement
filename = "/root/pet.txt"
~ id =
"5f8fb950ac60f7f23ef968097cda0a1fd3c11bdf" -> (known after
apply)
}
local_file.pet:
local_file.pet: Destroying...
Destroying...
[id=5f8fb950ac60f7f23ef968097cda0a1fd3c11bdf]
[id=5f8fb950ac60f7f23ef968097cda0a1fd3c11bdf]
local_file.pet:
local_file.pet: Destruction
Destruction complete
complete after
after 0s
0s
local_file.pet: Creating...
local_file.pet: Creating...
local_file.pet:
local_file.pet: Creation
Creation complete
complete after
after 0s
0s
[id=5f8fb950ac60f7f23ef968097cda0a1fd3c11bdf]
[id=5f8fb950ac60f7f23ef968097cda0a1fd3c11bdf]
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
create_before_destroy
main.tf >_
$ terraform apply
resource "local_file" "pet" {
filename = "/root/pets.txt" # local_file.pet must be replaced
-/+ resource "local_file" "pet" {
content = "We love pets!"
content = "We love pets!"
file_permission = "0700" directory_permission = "0777"
~ file_permission = "0777" -> "0755" # forces repl
lifecycle { filename = "/root/pet.txt"
create_before_destroy = true ~ id =
} "5f8fb950ac60f7f23ef968097cda0a1fd3c11bdf" -> (known after ap
}
...
local_file.pet: Creating...
local_file.pet: Creation complete after 0s
[id=5f8fb950ac60f7f23ef968097cda0a1fd3c11bdf]
local_file.pet: Destroying...
[id=5f8fb950ac60f7f23ef968097cda0a1fd3c11bdf]
local_file.pet: Destruction complete after 0s
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
prevent_destroy
main.tf >_
$ terraform apply
resource "local_file" "pet" {
filename = "/root/pets.txt" local_file.my-pet: Refreshing state...
content = "We love pets!" [id=cba595b7d9f94ba1107a46f3f731912d95fb3d2c]
file_permission = "0700"
Error: Instance cannot be destroyed
lifecycle {
create_before_destroy
prevent_destroy = true= true on main.tf line 1:
} 1: resource "local_file" "my-pet" {
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
ignore_changes
main.tf >_
resource "aws_instance" "webserver" { $ terraform apply
ami = "ami-0edab43b6fa892279"
instance_type = "t2.micro" ...
Terraform will perform the following actions:
tags = {
Name = “ProjectA-Webserver"
# aws_instance.webserver will be created
}
+ resource "aws_instance" "webserver" {
}
+ ami = "ami-0edab43b6fa892279"
+ get_password_data = false
+ host_id = (known after apply)
+ id = (known after apply)
+ instance_state = (known after apply)
+ instance_type = "t2.micro“
+ tags {
+ "Name" = "ProjectA-WebServer"
}
.
aws_instance.webserver: Creation complete after 33s [id=i-
05cd83b221911acd5]
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
ignore_changes
main.tf >_
resource "aws_instance" "webserver" { $ terraform apply
ami = "ami-0edab43b6fa892279" aws_instance.webserver: Refreshing state... [id=i-
instance_type = "t2.micro" 05cd83b221911acd5]
tags = {
Name = “ProjectA-Webserver" An execution plan has been generated and is shown below.
} Resource actions are indicated with the following symbols:
} ~ update in-place
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
ignore_changes
main.tf >_
resource "aws_instance" "webserver" { $ terraform apply
ami = "ami-0edab43b6fa892279"
aws_instance.webserver: Refreshing state... [id=i-
instance_type = "t2.micro"
05cd83b221911acd5]
tags = {
Name = “ProjectA-Webserver"
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
}
lifecycle {
ignore_changes = [
tags
]
}
}
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
ignore_changes
main.tf >_
resource "aws_instance" "webserver" { $ terraform apply
ami = "ami-0edab43b6fa892279"
aws_instance.webserver: Refreshing state... [id=i-
instance_type = "t2.micro"
05cd83b221911acd5]
tags = {
Name = “ProjectA-Webserver"
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
}
lifecycle {
ignore_changes == all
ignore_changes [
tags,ami
]
}
}
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Order Option
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
HANDS-ON LABS
© Copyright K odeK l oud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
© Copyright KodeKloud
Data Sources
Real World Infrastructure terraform.tfstate
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
main.tf
>_
$ cat /root/dog.txt
Dogs are awesome!
dogs.txt pets.txt
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Data Sources main.tf
dogs.txt pets.txt
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
terraform.tfstate
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
HANDS-ON LABS
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
© Copyright KodeKloud
Meta Arguments
main.tf variables.tf
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Shell Scripts
create_files.sh >_
touch /root/pet${i}
done
Iteration filename
1 /root/pet1
2 /root/pet2
3 /root/pet3
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Meta Arguments
depends_on lifecycle
main.tf main.tf
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
© Copyright KodeKloud
Count
count
main.tf variables.tf
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
count
main.tf variables.tf
local_file.pet[2]: Creating...
pet[1] local_file.pet[0]: Creating...
pet[0] pet[2]
local_file.pet[1]: Creating...
local_file.pet[0]: Creation complete after 0s
[id=7e4db4fbfdbb108bdd04692602bae3e9bd1e1b68]
local_file.pet[2]: Creation complete after 0s
[id=7e4db4fbfdbb108bdd04692602bae3e9bd1e1b68]
local_file.pet[1]: Creation complete after 0s
[id=7e4db4fbfdbb108bdd04692602bae3e9bd1e1b68]
main.tf variables.tf
>_
pet[0] pet[1] pet[2]
$ ls /rootapply
$ terraform
pet.txt
pets.txtTruncated]
[Output
.dogs.txt
cats.txt
local_file.pet[2]: Creating...
local_file.pet[0]: Creating...
local_file.pet[1]: Creating...
© Copyright KodeKloud local_file.pet[0]: Creation
Check out our full course on Terraform complete
for the Absolute after
Beginners: 0s
https://kode.wiki/3PoScZd
Length Function
main.tf variables.tf
}
pet[0] pet[1] pet[2]
>_
$ ls /rootapply
$ terraform
pet.txt
pets.txtTruncated]
[Output
.dogs.txt
© Copyright KodeKloud cats.txt
Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
local_file.pet[2]: Creating...
Length Function
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
LengthFunction
main.tf variables.tf
}
pet[0] pet[1] pet[2]
>_
$ ls /rootapply
$ terraform
pet.txt
pets.txtTruncated]
[Output
.dogs.txt
© Copyright KodeKloud cats.txt
Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
local_file.pet[2]: Creating...
>_ >_
$ terraform apply $ ls /root
. pet.txt
. dogs.txt
Terraform will perform the following actions: cats.txt
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
variables.tf
variable "filename" {
default
default == "/root/pets.txt"
[
main.tf
"/root/pets.txt",
"/root/dogs.txt",
resource "local_file" "pet" { "/root/cats.txt"
filename = var.filename [count.index] ]
count = length(var.filename)
3 }
}
>_
$ terraform plan
...
# local_file.pet[0] must be replaced
-/+ resource "local_file" "pet" {
directory_permission = "0777"
file_permission = "0777"
pet[0] pet[1] pet[2] ~ filename = "/root/pets.txt" -> "/root/dogs.txt" #
forces replacement
}
# local_file.pet[1] must be replaced
-/+ resource "local_file" "pet" {
directory_permission = "0777"
file_permission = "0777"
~ filename = "/root/dogs.txt" -> "/root/cats.txt" #
forces replacement
}
# local_file.pet[2] will be destroyed
Replace
© Copyright KodeKloud Replace Destroy - resource "local_file" "pet" {
Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
- directory_permission = "0777" -> null
main.tf >_
$ terraform output
resource "local_file" "pet" { Outputs:
filename = var.filename [count.index] pets = [
{
count = length(var.filename)
3 "directory_permission" = "0777"
} "file_permission" = "0777"
"filename" = "/root/pets.txt"
"id" = "da39a3ee5e6b4b0d3255bfef95601890afd80709"
output "pets" { },
value = local_file.pet {
} "directory_permission" = "0777"
"file_permission" = "0777"
"filename" = "/root/dogs.txt"
pet[0] pet[1] pet[2] "id" = "da39a3ee5e6b4b0d3255bfef95601890afd80709"
},
{
"directory_permission" = "0777"
"file_permission" = "0777"
"filename" = "/root/cats.txt"
"id" = "da39a3ee5e6b4b0d3255bfef95601890afd80709"
},
]
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
variables.tf
pet[0] pet[1] pet[2] variable "filename" {
default
default == "/root/pets.txt"
[
"/root/pets.txt",
"/root/dogs.txt",
"/root/cats.txt"
]
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
variables.tf
pet[0] pet[1] variable "filename" {
default
default == "/root/pets.txt"
[
"/root/pets.txt",
"/root/dogs.txt",
"/root/cats.txt"
]
dogs.txt cats.txt }
© Copyrightpet[2]
KodeKloud Does not Exist Check out our full course onDestroy
Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
HANDS-ON LABS
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
© Copyright KodeKloud
for_each
for_each
main.tf variables.tf
variable "filename" {
resource "local_file" "pet" { type=list(string)
var.filename [count.index]
filename = each.value default
default == "/root/pets.txt"
[
for_each
count ==length(var.filename)
3var.filename "/root/pets.txt",
"/root/dogs.txt",
} "/root/cats.txt"
]
>_
pet[0] pet[1] pet[2]
$ terraform plan
Error: Invalid for_each argument
main.tf variables.tf
variable "filename" {
resource "local_file" "pet" { type=list(string)
type=set(string)
var.filename [count.index]
filename = each.value default
default == "/root/pets.txt"
[
for_each
count ==length(var.filename)
3var.filename "/root/pets.txt",
"/root/dogs.txt",
} "/root/cats.txt"
]
>_
pet[0] pet[1] pet[2]
$ terraform plan
Terraform
Error: Invalid
will perform
for_eachthe
argument
following actions:
# local_file.pet["/root/cats.txt"] will be created
+
onresource
main.tf "local_file"
line 2, in resource
"pet" {"local_file" "pet":
2: + directory_permission
for_each = var.filename = "0777"
+ file_permission = "0777"
The given
+ filename
"for_each" argument=value "/root/cats.txt"
is unsuitable: the "for_each"
argument
} must be a map, or set of strings, and you have provided a value
...
of type
<output
listtrimmed>
of string.
© Copyright KodeKloud Plan: 3 to
Check out ouradd, 0 to on
full course change, 0 to
Terraform destroy.
for the Absolute Beginners: https://kode.wiki/3PoScZd
for_each
main.tf variables.tf
variable "filename" {
resource "local_file" "pet" { type=list(string)
type=set(string)
var.filename [count.index]
filename = each.value default
default == "/root/pets.txt"
[
for_each
count ==length(var.filename)
var.filename
3toset(var.filename) "/root/pets.txt",
"/root/dogs.txt",
} "/root/cats.txt"
]
>_
pet[0] pet[1] pet[2]
$ terraform plan
Terraform
Error: Invalid
will perform
for_eachthe
argument
following actions:
# local_file.pet["/root/cats.txt"] will be created
+
onresource
main.tf "local_file"
line 2, in resource
"pet" {"local_file" "pet":
2: + directory_permission
for_each = var.filename = "0777"
+ file_permission = "0777"
The given
+ filename
"for_each" argument=value "/root/cats.txt"
is unsuitable: the "for_each"
argument
} must be a map, or set of strings, and you have provided a value
...
of type
<output
listtrimmed>
of string.
© Copyright KodeKloud Plan: 3 to
Check out ouradd, 0 to on
full course change, 0 to
Terraform destroy.
for the Absolute Beginners: https://kode.wiki/3PoScZd
for_each
main.tf variables.tf
variable "filename" {
resource "local_file" "pet" { type=list(string)
var.filename [count.index]
filename = each.value default
default == "/root/pets.txt"
[
for_each
count ==length(var.filename)
var.filename
3toset(var.filename) "/root/pets.txt",
"/root/dogs.txt",
} "/root/cats.txt"
output "pets" { ]
value = local_file.pet
}
}
>_
pet[0] pet[1] pet[2]
$ terraform plan
Terraform will perform
Error: Invalid for_eachthe following actions:
argument
# local_file.pet["/root/pets.txt"]
local_file.pet["/root/cats.txt"] will be destroyed created
+
onresource
- main.tf "local_file" "pet" {"local_file" "pet":
line 2, in resource
2: +
- directory_permission
for_each = var.filename = "0777" -> null
+
- file_permission = "0777" -> null
+
- filename
The given "for_each" argument=value "/root/pets.txt"
is unsuitable:
"/root/cats.txt" -> null
the "for_each"
}}
argument must be a map, or set of strings, and you have provided a value
... <output
of type listtrimmed>
of string.
© Copyright KodeKloud Plan: 03 to
Check out ouradd, 0 to on
full course change, 10 to
Terraform destroy.
for the Absolute Beginners: https://kode.wiki/3PoScZd
for_each
main.tf >_
$ terraform output
resource "local_file" "pet" { pets = {
var.filename [count.index]
filename = each.value "/root/cats.txt" = {
"directory_permission" = "0777"
for_each
count ==length(var.filename)
var.filename
3toset(var.filename) "file_permission" = "0777"
"filename" = "/root/cats.txt"
} "id" = "da39a3ee5e6b4b0d3255bfef95601890afd80709"
}
output "pets" {
value = local_file.pet "/root/dogs.txt" = {
} "directory_permission" = "0777"
"file_permission" = "0777"
"filename" = "/root/dogs.txt"
pet[0] pet[1] pet[2] "id" = "da39a3ee5e6b4b0d3255bfef95601890afd80709"
}
}
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
count for_each
>_ >_
$ terraform output $ terraform output
pets = [ pets = {
{ "/root/cats.txt" = {
"directory_permission" = "0777" "directory_permission" = "0777"
"file_permission" = "0777" "file_permission" = "0777"
"filename" = "/root/pets.txt" "filename" = "/root/cats.txt"
"id" = "da39a3ee5e6b4b0d3255bfef95601890afd80709" "id" = "da39a3ee5e6b4b0d3255bfef95601890afd80709"
}, }
{
"directory_permission" = "0777"
"file_permission" = "0777" "/root/dogs.txt" = {
"filename" = "/root/dogs.txt" "directory_permission" = "0777"
"id" = "da39a3ee5e6b4b0d3255bfef95601890afd80709" "file_permission" = "0777"
}, "filename" = "/root/dogs.txt"
{ "id" = "da39a3ee5e6b4b0d3255bfef95601890afd80709"
"directory_permission" = "0777" }
"file_permission" = "0777" }
"filename" = "/root/cats.txt"
"id" = "da39a3ee5e6b4b0d3255bfef95601890afd80709"
},
]
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
HANDS-ON LABS
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
© Copyright KodeKloud
Version Constraints
main.tf >_
resource "local_file" "pet" { $ terraform init
filename = "/root/pet.txt" Initializing the backend...
content = "We love pets!" Initializing provider plugins...
} - Finding latest version of hashicorp/local...
- Installing hashicorp/local v1.4.0...
- Installed hashicorp/local v1.4.0 (signed by HashiCorp)
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
main.tf
resource "local_file" "pet" {
filename = "/root/pet.txt"
content = "We love pets!"
}
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
main.tf
resource "local_file" "pet" {
filename = "/root/pet.txt"
content = "We love pets!"
}
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
main.tf
resource "local_file" "pet" {
filename = "/root/pet.txt"
content = "We love pets!"
}
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
main.tf
terraform {
required_providers {
local = {
source = "hashicorp/local"
version = "1.4.0"
}
}
}
resource "local_file" "pet" {
filename = "/root/pet.txt"
content = "We love pets!"
}
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
main.tf >_
terraform { $ terraform init
required_providers { Initializing the backend...
local = { Initializing provider plugins...
source = "hashicorp/local" - Finding hashicorp/local versions matching "1.4.0"...
- Installing hashicorp/local v1.4.0...
version = "1.4.0" - Installed hashicorp/local v1.4.0 (signed by HashiCorp)
}
Terraform has been successfully initialized!
}
} You may now begin working with Terraform. Try running
"terraform plan" to see
any changes that are required for your infrastructure. All
resource "local_file" "pet" { Terraform commands
filename = "/root/pet.txt" should now work.
content = "We love pets!" If you ever set or change modules or backend configuration for
} Terraform,
rerun this command to reinitialize your working directory. If
you forget, other
commands will detect it and remind you to do so if necessary.
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
main.tf >_
terraform { $ terraform init
required_providers { Initializing the backend...
local = {
Initializing provider plugins...
source = "hashicorp/local" - Finding hashicorp/local versions matching "> 1.2.0, <
version
version == "1.4.0"
">
"<
"!=1.2.0,
1.4.0"
1.1.0"
2.0.0"< 2.0.0, != 1.4.0" 2.0.0, != 1.4.0"...
} - Installing hashicorp/local v1.3.0...
} - Installed hashicorp/local v1.3.0 (signed by
HashiCorp)
}
Terraform has been successfully initialized!
resource "local_file" "pet" {
filename = "/root/pet.txt"
content = "We love pets!"
}
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
main.tf >_
terraform { $ terraform init
required_providers { Initializing the backend...
local = {
Initializing provider plugins...
source = "hashicorp/local"
- Finding hashicorp/local versions matching "~>
version
version === "1.4.0"
version ">
"!=1.2.0,
"<
"~> 2.0.0"< 2.0.0, != 1.4.0"
1.4.0"
1.1.0"
1.2"
1.2.0" 1.2.0"...
} - Installing hashicorp/local v1.2.2...
} - Installed hashicorp/local v1.2.2 (signed by
HashiCorp)
}
Terraform has been successfully initialized!
resource "local_file" "pet" {
filename = "/root/pet.txt"
content = "We love pets!"
}
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
© Copyright KodeKloud