0% found this document useful (0 votes)
41 views230 pages

Terraform KodeKloud

The document discusses the challenges faced in traditional IT environments, such as slow deployment, high costs, limited automation, and human error. It introduces Infrastructure as Code (IaC) as a solution to these challenges, highlighting various IaC tools and their functionalities. Additionally, it provides examples of Terraform scripts for managing cloud resources and emphasizes the benefits of using Terraform for infrastructure management.

Uploaded by

Dhanasekar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views230 pages

Terraform KodeKloud

The document discusses the challenges faced in traditional IT environments, such as slow deployment, high costs, limited automation, and human error. It introduces Infrastructure as Code (IaC) as a solution to these challenges, highlighting various IaC tools and their functionalities. Additionally, it provides examples of Terraform scripts for managing cloud resources and emphasizes the benefits of using Terraform for infrastructure management.

Uploaded by

Dhanasekar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 230

Traditional IT &

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

Human Error Inconsistency


Application team
Data Center
Wasted Resources
© 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

Human Error Inconsistency


Application team
Data Center
Wasted Resources
© 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

Backup Admins
Slow Delivery

Human Error Inconsistency


Application team
Data Center
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Shell Python Ruby
Perl Powershell

© 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)

INSTANCE=$(echo ${EC2_INSTANCE} | sed 's/*INSTANCE //'


| sed 's/ .*//')

# Wait for instance to be ready


while ! ec2-describe-instances $INSTANCE | grep -q
"running"
do
echo Waiting for $INSTANCE is to be ready...
done

# Check if instance is not provisioned and exit


if [ ! $(ec2-describe-instances $INSTANCE | grep -q
"running") ]; then
echo Instance $INSTANCE is stopped.
exit
fi

ec2-associate-address $IP_ADDRESS -i $INSTANCE

echo Instance $INSTANCE was created successfully!!!

© 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/ .*//')

# Wait for instance to be ready


while ! ec2-describe-instances $INSTANCE | grep -q
"running"
do
echo Waiting for $INSTANCE is to be ready...
done

# Check if instance is not provisioned and exit


if [ ! $(ec2-describe-instances $INSTANCE | grep -q
"running") ]; then
echo Instance $INSTANCE is stopped.
exit
fi

ec2-associate-address $IP_ADDRESS -i $INSTANCE

echo Instance $INSTANCE was created successfully!!!

© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Infrastructure as Code
ec2.yaml main.tf

- amazon.aws.ec2: resource "aws_instance" "webserver" {


key_name: mykey ami = "ami-0edab43b6fa892279"
instance_type: t2.micro instance_type = "t2.micro"
image: ami-123456 }
wait: yes
group: webserver
count: 3
vpc_subnet_id: subnet-29e63245
assign_public_ip: yes

© 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 Server Templating Provisioning 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

Maintains Standard Structure

Version Control

Idempotent

© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Server Templating Tools

Pre Installed Software and Dependencies

Virtual Machine or Docker Images

Immutable Infrastructure

© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Provisioning Tools

Deploy Immutable Infrastructure resources

Servers, Databases, Network Components etc.

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

Physical Machines BigIP

VMWare CloudFlare

AWS DNS

GCP Palo Alto

Azure Infoblox

© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Providers

Physical Machines BigIP DataDog

VMWare CloudFlare Grafana

AWS DNS Auth0

GCP Palo Alto Wavefront

Azure Infoblox Sumo Logic

© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Providers

Physical Machines BigIP DataDog InfluxDB

VMWare CloudFlare Grafana MongoDB

AWS DNS Auth0 MySQL

GCP Palo Alto Wavefront PostgreSQL

Azure Infoblox Sumo Logic VCS

© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
HashiCorp Configuration Language

main.tf

resource "aws_instance" "webserver" {


ami = "ami-0edab43b6fa892279"
instance_type = "t2.micro"
}

resource "aws_s3_bucket" "finance" {


bucket = "finanace-21092020"
tags = {
Description = "Finance and Payroll"
}
}
resource "aws_iam_user" "admin-user" {
name = "lucy"
tags = {
Description = "Team Leader"
}
}
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Declarative

main.tf Real World Infrastructure

resource "aws_instance" "webserver" {


ami = "ami-0edab43b6fa892279"
instance_type = "t2.micro"
}

resource "aws_s3_bucket" "finance" {


bucket = "finanace-21092020"
tags = {
Description = "Finance and Payroll"
}
}
resource "aws_iam_user" "admin-user" {
name = "lucy"
tags = {
Description = "Team Leader"
}
}
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Declarative

main.tf Real World Infrastructure

resource "aws_instance" "webserver" {


ami = "ami-0edab43b6fa892279"
instance_type = "t2.micro" Init
}

resource "aws_s3_bucket" "finance" { Plan


bucket = "finanace-21092020"
tags = {
Description = "Finance and Payroll"
} Apply
}
resource "aws_iam_user" "admin-user" {
name = "lucy"
tags = {
Description = "Team Leader"
}
}
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Resource

Real World Infrastructure

© Copyright K odeK l oud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Terraform State

terraform.tfstate Real World Infrastructure

© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Terraform Import

terraform.tfstate Real World Infrastructure

© 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

© Copyright K odeK l oud


Installing Terraform
>_
$ wget https://releases.hashicorp.com/terraform/0.13.0/terraform_0.13.0_linux_amd64.zip
$ unzip terraform_0.13.0_linux_amd64.zip
$ mv terraform /usr/local/bin

$ 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

resource "aws_instance" "webserver" {


ami = "ami-0c2f25c1f66a1ff4d"
instance_type = "t2.micro"
}

© 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

resource "local_file" "pet" {​


{
filename = "/root/pets.txt"​
Arguments
content = "We love pets!"​
}

© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
aws-ec2.tf

resource "aws_instance" "webserver" {


ami = "ami-0c2f25c1f66a1ff4d"
instance_type = "t2.micro"
}

© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
aws-s3.tf

resource "aws_s3_bucket" "data" {


bucket = "webserver-bucket-org-2207"
acl = "private"
}

© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
local.tf

resource "local_file" "pet" {


filename = "/root/pets.txt"
content = "We love pets!"
}

Init Plan Apply

© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
local.tf

resource "local_file" "pet" {


filename = "/root/pets.txt"
content = "We love pets!"
}

>_
$ terraform init
Initializing the backend...

Initializing provider plugins...


- Finding latest version of hashicorp/local...
- Installing hashicorp/local v1.4.0...
- Installed hashicorp/local v1.4.0 (signed by HashiCorp)

The following providers do not have any version constraints in configuration,


so the latest version was installed.

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.

* hashicorp/local: version = "~> 1.4.0"

Terraform has been successfully initialized!


© 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.

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

An execution plan has been generated and is shown below.


Resource actions are indicated with the following symbols:
+ create

Terraform will perform the following actions:

# local_file.pet will be created


+ resource "local_file" "pet" {
+ content = "We love pets!"
+ directory_permission = "0777"
+ file_permission = "0777"
+ filename = "/root/pets.txt"
+ id = (known after apply)
}

Plan: 1 to add, 0 to change, 0 to destroy.

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

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
© Copyright KodeKloud"terraform apply" is subsequently run. Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
>_
$ terraform apply
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
+ create

Terraform will perform the following actions:

# local_file.pet will be created


+ resource "local_file" "pet" {
+ content = "We love pets!"
+ directory_permission = "0777"
+ file_permission = "0777"
+ filename = "/root/pets.txt"
+ id = (known after apply)
}

Plan: 1 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?


Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.

Enter a value: yes


local_file.new_file: Creating...
local_file.new_file: Creation complete after 0s
[id=521c5c732c78cb42cc9513ecc7c0638c4a115b55]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

$ 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

resource "local_file" "pet" {​


filename = "/root/pets.txt"​
content = "We love pets!"​
}

© Copyright K odeK l oud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
provider

resource_type

Argument-1 Argument-1 Argument-1 Argument-1 Argument-1 Argument-1

Arguments Argument-2 Argument-2 Argument-2 Argument-2 Argument-2 Argument-2

Argument-X Argument-X Argument-X Argument-X Argument-X Argument-X

© 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

filename = "/root/pets.txt" Terraform will perform the following actions:


content = "We love pets!"
# local_file.pet must be replaced
file_permission = "0700" -/+ 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)
}

Plan: 1 to add, 0 to change, 1 to destroy.

----------------------------------------------------------------------
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)
}

Plan: 1 to add, 0 to change, 1 to destroy.

Do you want to perform these actions?


Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.

Enter a value: yes

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]

Apply complete! Resources: 1 added, 0 changed, 1 destroyed.


© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
>_
$ terraform destroy
local_file.pet: Refreshing state...
[id=5f8fb950ac60f7f23ef968097cda0a1fd3c11bdf]

An execution plan has been generated and is shown below.


Resource actions are indicated with the following symbols:
- destroy

Terraform will perform the following actions:

# local_file.pet will be destroyed


- resource "local_file" "pet" {
- content = "My favorite pet is a gold fish" -> null
- directory_permission = "0777" -> null
- file_permission = "0700" -> null
- filename = "/root/pet.txt" -> null
- id = "5f8fb950ac60f7f23ef968097cda0a1fd3c11bdf" -
> null
}

Plan: 0 to add, 0 to change, 1 to destroy.

Do you really want to destroy all resources?


Terraform will destroy all your managed infrastructure, as shown above.
There is no undo. Only 'yes' will be accepted to confirm.

Enter a value: yes

local_file.pet: Destroying... [id=5f8fb950ac60f7f23ef968097cda0a1fd3c11bdf]


local_file.pet: Destruction complete after 0s

© Copyright KodeKloud Destroy complete!


Check out Resources: 1 Terraform
our full course on destroyed.
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 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

Initializing provider plugins...


- Finding latest version of hashicorp/local...
- Installing hashicorp/local v2.0.0...
- Installed hashicorp/local v2.0.0 (signed by HashiCorp)

The following providers do not have any version constraints in


configuration,
so the latest version was installed.

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.

* hashicorp/local: version = "~> 2.0.0"

Terraform has been successfully initialized!

© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
>_
$ terraform init
Initializing the backend...

Initializing provider plugins...


- Finding latest version of hashicorp/local...
- Installing hashicorp/local v2.0.0...
- Installed hashicorp/local v2.0.0 (signed by HashiCorp)

The following providers do not have any version constraints in


configuration,
so the latest version was installed.

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.

* hashicorp/local: version = "~> 2.0.0"

Terraform has been successfully initialized!

© 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.

* hashicorp/local: version = "~> 2.0.0"

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

The following providers do not have any version


configuration,
so the latest version was installed.

To prevent automatic upgrades to new major versi


contain breaking
changes, we recommend adding version constraints
required_providers block
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Configuration
Directory
>_
[terraform-local-file]$ ls /root/terraform-local-file
local.tf

local.tf cat.tf

resource "local_file" "pet" { resource "local_file" "cat" {


filename = "/root/pets.txt" filename = "/root/cat.txt"
content = "We love pets!" content = "My favorite pet is Mr. Whiskers"
}
}

© 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

resource "local_file" "cat" { outputs.tf Contains outputs from resources


filename = "/root/cat.txt"
content = "My favorite pet is Mr. Whiskers" provider.tf Contains Provider definition

}
© 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

resource "local_file" "pet" {


filename = "/root/pets.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/pets.txt"
content = "We love pets!"
}

resource "random_pet" "my-pet" {


prefix = "Mrs"
separator = "."
length = "1"
}

© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
>_

$ terraform init
Initializing the backend...

Initializing provider plugins...


- Using previously-installed hashicorp/local v2.0.0
- Finding latest version of hashicorp/random...
- Installing hashicorp/random v2.3.0...
- Installed hashicorp/random v2.3.0 (signed by HashiCorp)

The following providers do not have any version constraints in


configuration,
so the latest version was installed.

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.

* hashicorp/local: version = "~> 2.0.0"


* hashicorp/random: version = "~> 2.3.0"

Terraform has been successfully initialized!

© 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.

local_file.pet: Refreshing state...


[id=d1a31467f206d6ea8ab1cad382bc106bf46df69e]

.
.
# random_pet.my-pet will be created
+ resource "random_pet" "my-pet" {
+ id = (known after apply)
+ length = 1
+ prefix = "Mrs"
+ separator = "."
}

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
>_
$ terraform apply
local_file.new_file: Refreshing state...
[id=d1a31467f206d6ea8ab1cad382bc106bf46df69e]

An execution plan has been generated and is shown below.


Resource actions are indicated with the following symbols:
+ create

Terraform will perform the following actions:

# random_pet.my-pet will be created


+ resource "random_pet" "my-pet" {
Mrs.hen
+ id = (known after apply)
+ length = 1
+ prefix = "Mrs"
+ separator = "."
}

Plan: 1 to add, 0 to change, 0 to destroy.

random_pet.my-pet: Creating...
random_pet.my-pet: Creation complete after 0s [id=Mrs.hen]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.


© 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
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

resource "local_file" "pet" { variable "filename" {


filename = "/root/pets.txt" default = "/root/pets.txt"
content = "We love pets!" }
variable "content" {
} default = "We love pets!"
}
resource "random_pet" "my-pet" { variable "prefix" {
prefix = "Mrs" default = "Mrs"
separator = "." }
length = "1" variable "separator" {
} default = "."
}
variable "length" {
default = "1"
}

© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
main.tf variables.tf

resource "local_file" "pet" { variable "filename" {


filename = var.filename
"/root/pets.txt" default = "/root/pets.txt"
var.content
content = "We love pets!" }
variable "content" {
} default = "We love pets!"
}
resource "random_pet" "my-pet" { variable "prefix" {
prefix = var.prefix
"Mrs" default = "Mrs"
separator = var.separator
"." }
length = var.length
"1" variable "separator" {
} default = "."
}
variable "length" {
default = "1"
}

© 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)
}

# random_pet.my-pet will be created


+ resource "random_pet" "my-pet" {
+ id = (known after apply)
+ length = 1
+ prefix = "Mrs"
+ separator = "."
}

Plan: 2 to add, 0 to change, 0 to destroy.


.
.
random_pet.my-pet: Creating...
random_pet.my-pet: Creation complete after 0s [id=Mrs.ram]
local_file.pet: Creating...
local_file.pet: Creation complete after 0s
[id=f392b4bcf5db76684f719bf72061627a9a177de1]
© Copyright K odeK l oud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
main.tf variables.tf

resource "local_file" "pet" { variable "filename" {


filename = var.filename
"/root/pets.txt" default = "/root/pets.txt"
var.content
content = "We love pets!" }
variable "content" {
} default = "My
"We favorite
love pets!"
pet is Mrs. Whiskers"
}
resource "random_pet" "my-pet" { variable "prefix" {
prefix = var.prefix
"Mrs" default = "Mrs"
separator = var.separator
"." }
length = var.length
"1" variable "separator" {
} default = "."
}
variable "length" {
default = "1"
"2"
}

© 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:

-/+ resource "local_file" "pet" {


~ content = “We love pets!" -> "My favorite pet is Mrs. Whiskers!" #
forces replacement
directory_permission = "0777"
file_permission = "0777"
filename = "/root/pet.txt"
~ id = "bc9cabef1d8b0071d3c4ae9959a9c328f35fe697" -> (known after
apply)
}

# random_pet.my-pet must be replaced


-/+ resource "random_pet" "my-pet" {
~ id = "Mrs.Hen" -> (known after apply)
~ length = 1 -> 2 # forces replacement
prefix = "Mrs"
separator = "."
}

Plan: 2 to add, 0 to change, 2 to destroy.


random_pet.my-pet: Destroying... [id=Mrs.hen]
random_pet.my-pet: Destruction complete after 0s
local_file.pet: Destroying... [id=bc9cabef1d8b0071d3c4ae9959a9c328f35fe697]
local_file.pet: Destruction complete after 0s
random_pet.my-pet: Creating...
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
local_file.pet: Creating...
main.tf variables.tf
resource "aws_instance" "webserver" { variable "ami" {
ami = var.ami
var.ami default = "ami-0edab43b6fa892279"
instance_type = var.instance_type
var.instance_type }
} variable "instance_type" {
default = "t2.micro"
}

© 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 "filename" { string "/root/pets.txt"


default = "/root/pets.txt"
type = string number 1
description = "the path of local file"
bool true/false

} any Default Value


variable "content" {
default = "I love pets!"
variables.tf
type = string
description = “the content of the file"
variable "length" {
} default = 2
variable "prefix" { type = number
default = "Mrs" description = "length of the pet name"
type = string }
description = "the prefix to be set"

} 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

any Default Value

list ["cat", "dog"]

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

variable "prefix" { resource "random_pet" "my-pet" {


default = ["Mr", "Mrs", "Sir"] prefix = var.prefix[0]
type = list 0 1 2 }
}

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

variable file-content { resource local_file my-pet {


type = map filename = "/root/pets.txt"
default = { content = var.file-content["statement2"]
"statement1" = "We love pets!" }
"statement2" = “We love animals!"
}
}

Key Value

statement1 We love pets!

statement2 We love animals!

© 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

variable "prefix" { variable "prefix" {


default = ["Mr", "Mrs", "Sir"] default = ["Mr", "Mrs", "Sir"]
type = list(string) type = list(number)
} }

variables.tf >_
$ terraform plan
variable "prefix" { Error: Invalid default value for variable

default = [1, 2, 3] on variables.tf line 3, in variable "prefix":


type = list(number) 3: default = ["Mr", "Mrs", "Sir"]
}
This default value is not compatible with the
variable's type constraint: a number is required.
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Map of a Type

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

resource "local_file" "pet" { variable "filename" {


filename = var.filename
"/root/pets.txt" default = "/root/pets.txt"
var.content
content = "We love pets!" }
variable "content" {
} default = "We love pets!"
}
resource "random_pet" "my-pet" { variable "prefix" {
prefix = var.prefix
"Mrs" default = "Mrs"
separator = var.separator
"." }
length = var.length
"1" variable "separator" {
} default = "."
}
variable "length" {
default = 2
}

© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
main.tf variables.tf

resource "local_file" "pet" { variable "filename" {


filename = var.filename
"/root/pets.txt"
var.content
content = "We love pets!" }
variable "content" {
}
}
resource "random_pet" "my-pet" { variable "prefix" {
prefix = var.prefix
"Mrs"
separator = var.separator
"." }
length = var.length
"1" variable "separator" {
}
}
variable "length" {

© 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

>_

$ terraform apply -var "filename=/root/pets.txt" -var "content=We love


Pets!" -var "prefix=Mrs" -var "separator=." -var "length=2"

© 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 apply -var-file variables.tfvars

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" ?

>_

$ terraform apply -var "filename=/root/best-pet.txt" ?

© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Variable Definition Precedence

Order Option >_


1 Environment Variables $ export TF_VAR_filename="/root/cats.txt" 1?
2 terraform.tfvars

3 *.auto.tfvars (alphabetical order) terraform.tfvars


filename = "/root/pets.txt" 2?
4 -var or –var-file (command-line flags)

variable.auto.tfvars
filename = "/root/mypet.txt" 3?

>_

$ terraform apply -var "filename=/root/best-pet.txt" 4?

© 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"
}

resource "random_pet" "my-pet" {


prefix = var.prefix Mr.Bull
separator = var.separator
length = var.length
}

>_
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]

Apply complete! Resources: 2 added, 0 changed, 0 destroyed.


© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
FILENAME CONTENT PREFIX SEPERATOR

main.tf
LENGTH
resource "local_file" "pet" {
filename = var.filename
content = "My favorite pet is Mr.Cat"
}

resource "random_pet" "my-pet" {


prefix = var.prefix Mr.Bull
separator = var.separator
length = var.length
}

>_
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]

Apply complete! Resources: 2 added, 0 changed, 0 destroyed.


© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
FILENAME CONTENT PREFIX SEPERATOR

main.tf
resource "local_file" "pet" {
filename = var.filename
${random_pet.my-pet.id}"
content = "My favorite pet is Mr.Cat"
}

resource "random_pet" "my-pet" {


prefix = var.prefix Mr.Bull
separator = var.separator
length = var.length
}

>_
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]

Apply complete! Resources: 2 added, 0 changed, 0 destroyed.


© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
l_file" "pet" {
var.filename

"My favorite pet is ${random_pet.my-pet.id}"


Mr.Cat"
Mr.Bull"

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]

Apply complete! Resources: 1 added, 0 changed, 1 destroyed.

© 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"
}

resource "random_pet" "my-pet" {


prefix = var.prefix
separator = var.separator
length = var.length
}

© 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
]

resource "random_pet" "my-pet" {


prefix = var.prefix
separator = var.separator
length = var.length
}

© 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...

Initializing provider plugins...


main.tf - Finding latest version of hashicorp/local...
- Installing hashicorp/local v1.4.0...
- Installed hashicorp/local v1.4.0 (signed by HashiCorp)
resource "local_file" "pet" {
The following providers do not have any version constraints
filename = var.filename in configuration,
content = var.content so the latest version was installed.
} Init
To prevent automatic upgrades to new major versions that
may contain breaking
variables.tf changes, we recommend adding version constraints in a
required_providers block
in your configuration, with the constraint strings
variable "filename" { suggested below.
default = "/root/pets.txt"
* hashicorp/local: version = "~> 1.4.0"
}
variable "content" { Terraform has been successfully initialized!

default = "I love pets!"


}

© 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:

resource "local_file" "pet" { # local_file.pet will be created


filename = var.filename + resource "local_file" "pet" {
+ content = "I love pets!"
content = var.content + directory_permission = "0777"
} Apply
Plan
Init + file_permission = "0777"
+ filename = "/root/pets.txt"
variables.tf + id = (known after apply)
}
variable "filename" {
Plan: 1 to add, 0 to change, 0 to destroy.
default = "/root/pets.txt"
} Do you want to perform these actions?
variable "content" { Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
default = "I love pets!"
} Enter a value: yes

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
>_ >_

[terraform-local-file]$ cat /root/pets [terraform-local-file]$ terraform apply


local_file.pet: Refreshing state...
I love pets!
[id=7e4db4fbfdbb108bdd04692602bae3e9bd1e1b68]

Apply complete! Resources: 0 added, 0 changed, 0 destro

Apply
Plan
Init

© Copyright K odeK l oud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
>_ >_

[terraform-local-file]$ ls [terraform-local-file]$ cat terraform.tfstate


{
main.tf variables.tf terraform.tfstate
"version": 4,
"terraform_version": "0.13.0",
"serial": 1,
"lineage": "e35dde72-a943-de50-3c8b-1df8986e5a31"
"outputs": {},
"resources": [
{
"mode": "managed",
"type": "local_file",
Apply
Plan
Init "name": "pet",
"provider":
"provider[\"registry.terraform.io/hashicorp/local\"
"instances": [
{
"schema_version": 0,
"attributes": {
"content": "I love pets!",
"content_base64": null,
"directory_permission": "0777",
"file_permission": "0777",
"filename": "/root/pets.txt",
"id":
"7e4db4fbfdbb108bdd04692602bae3e9bd1e1b68",
"sensitive_content": null
© Copyright KodeKloud Check out our full course on},
Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
"private": "bnVsbA=="
>_
variables.tf
[terraform-local-file]$ cat terraform.tfstate
variable "filename" { {
default = "/root/pets.txt" "version": 4,
} "terraform_version": "0.13.0",
"serial": 1,
variable "content" {
"lineage": "e35dde72-a943-de50-3c8b-1df8986e5a31"
default = "We
"I love
love pets!"
pets!" "outputs": {},
} "resources": [
{
"mode": "managed",
"type": "local_file",
>_ Apply
Plan
Init "name": "pet",
"provider":
$ terraform plan "provider[\"registry.terraform.io/hashicorp/local\"
Refreshing Terraform state in-memory "instances": [
prior to plan... {
The refreshed state will be used to "schema_version": 0,
calculate this plan, but will not be "attributes": {
persisted to local or remote state "content": "I love pets!",
storage. "content_base64": null,
"directory_permission": "0777",
local_file.pet: Refreshing state... "file_permission": "0777",
[id=7e4db4fbfdbb108bdd04692602bae3e9bd1e "filename": "/root/pets.txt",
1b68] "id":
. "7e4db4fbfdbb108bdd04692602bae3e9bd1e1b68",
. "sensitive_content": null
. Copyright KodeKloud
© Check out our full course on},
Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
[Output Truncated ] "private": "bnVsbA=="
>_
variables.tf
[terraform-local-file]$ cat terraform.tfstate
variable "filename" { {
default = "/root/pets.txt" "version": 4,
} "terraform_version": "0.13.0",
"serial": 1,
variable "content" {
"lineage": "e35dde72-a943-de50-3c8b-1df8986e5a31"
default = "We
"I love
love pets!"
pets!" "outputs": {},
} "resources": [
{
"mode": "managed",
"type": "local_file",
>_ Apply
Plan
Init "name": "pet",
"provider":
$ terraform apply "provider[\"registry.terraform.io/hashicorp/local\"
local_file.pet: Refreshing state... "instances": [
[id=7e4db4fbfdbb108bdd04692602bae3e9bd1e1b68] {
"schema_version": 0,
Terraform will perform the following actions:
"attributes": {
# local_file.pet must be replaced "content": "We "I lovelovepets!",
pets!“,
-/+ resource "local_file" "pet" { "content_base64": null,
~ content = "I love pets!" - "directory_permission": "0777",
> "We love pets!" # forces replacement "file_permission": "0777",
directory_permission = "0777" "filename": "/root/pets.txt",
file_permission = "0777"
"id":
filename = "/root/pets.txt"
~ id = "7e4db4fbfdbb108bdd04692602bae3e9bc4d1c14",
"7e4db4fbfdbb108bdd04692602bae3e9bd1e1b68" -> "sensitive_content": null
(known
© after
Copyright apply)
KodeKloud Check out our full course on},
Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
} "private": "bnVsbA=="
Real World Infrastructure terraform.tfstate

© 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

id=aabbcc id=eeddff id=gghhhii

© 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.

Do you want to perform these actions?


Terraform will perform the actions described above. my-pet
Only 'yes' will be accepted to approve.

Enter a value: yes

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]

Apply complete! Resources: 3 added, 0 changed, 0


destroyed.
© 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

pet
main.tf

my-pet

resource "local_file" "cat" {


filename = "/root/cat.txt"
content = "I like cats too!"
}

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.

Do you want to perform these actions?


Terraform will perform the actions described above.
Only 'yes' will be accepted to approve. cat

Enter a value: yes

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)
}

Plan: 1 to add, 0 to change, 1 to destroy.


© Copyright K odeK l oud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
------------------------------------------------------
Collaboration

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 >_

resource "local_file" "pet" { $ terraform validate


filename = "/root/pets.txt" Success! The configuration is valid.
content = "We love pets!"
file_permissions = "0700"
} $ terraform validate

Error: Unsupported argument

on main.tf line 4, in resource "local_file" "pet":


4: file_permissions = "0777"

An argument named "file_permissions" is not expected


here. Did you mean "file_permission"?

© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
terraform fmt

main.tf >_

resource "local_file" "pet" { $ terraform fmt


filename = "/root/pets.txt"
main.tf
content = "We love pets!"
file_permission = "0700"
}

© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
terraform fmt

main.tf >_

resource "local_file" "pet" { $ terraform fmt


filename = "/root/pets.txt"
main.tf
content = "We love pets!"
file_permission = "0700"
}

© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
terraform show

>_ >_

$ terraform show $ terraform show -json


# local_file.pet: {"format_version":"0.1","terraform_version":"0.13.0
resource "local_file" "pet" { ","values":{"root_module":{"resources":[{"address":
content = "We love pets!" "local_file.pet","mode":"managed","type":"local_fil
directory_permission = "0777" e","name":"pet","provider_name":"registry.terraform
file_permission = "0777" .io/hashicorp/local","schema_version":0,"values":{"
filename = "/root/pets.txt" content":"We love
id = pets!","content_base64":null,"directory_permission"
"cba595b7d9f94ba1107a46f3f731912d95fb3d2c" :"0777","file_permission":"0777","filename":"/root/
} pets.txt","id":"cba595b7d9f94ba1107a46f3f731912d95f
b3d2c","sensitive_content":null}}]}}}

© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
terraform providers
main.tf >_

resource "local_file" "pet" { $ terraform providers


filename = "/root/pets.txt" Providers required by configuration:
content = "We love pets!" .
file_permission = "0700" └── provider[registry.terraform.io/hashicorp/local]
}
Providers required by state:

provider[registry.terraform.io/hashicorp/local]

$ terraform providers mirror /root/terraform/new_local_file


- Mirroring hashicorp/local...
- Selected v1.4.0 with no constraints
- Downloading package for windows_amd64...
- Package authenticated: signed by HashiCorp

© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
terraform output
main.tf >_

resource "local_file" "pet" { $ terraform output


filename = "/root/pets.txt"
content = "We love pets!" content = We love pets!
file_permission = "0777" pet-name = huge-owl
}
resource "random_pet" "cat" {
length = "2"
separator = "-"
} $ terraform output pet-name
output content {
value = local_file.pet.content pet-name = huge-owl
sensitive = false
description = "Print the content of the file"

}
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 >_

resource "local_file" "pet" { $ terraform refresh


filename = "/root/pets.txt"
random_pet.cat: Refreshing state... [id=huge-owl]
content = "We love pets!"
local_file.pet: Refreshing state...
file_permission = "0777"
[id=cba595b7d9f94ba1107a46f3f731912d95fb3d2c]
}
resource "random_pet" "cat" {
length = "2"
separator = "-"
} $ 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.cat: Refreshing state... [id=huge-owl]


local_file.pet: Refreshing state...
[id=cba595b7d9f94ba1107a46f3f731912d95fb3d2c]
------------------------------------------------------

No changes. Infrastructure is up-to-date.

© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
terraform graph
main.tf >_

resource "local_file" "pet" { $ terraform graph


filename = "/root/pets.txt" digraph {
content = "My favorite pet is ${random_pet.m compound = "true"
y-pet.id}" newrank = "true"
} subgraph "root" {
resource "random_pet" "my-pet" { "[root] local_file.pet (expand)" [label =
prefix = "Mr" "local_file.pet", shape = "box"]
separator = "." "[root]
length = "1" provider[\"registry.terraform.io/hashicorp/local\"]" [label =
} "provider[\"registry.terraform.io/hashicorp/local\"]", shape =
"diamond"]
"[root]
provider[\"registry.terraform.io/hashicorp/random\"]" [label =
"provider[\"registry.terraform.io/hashicorp/random\"]", shape =
"diamond"]
"[root] random_pet.my-pet (expand)" [label =
"random_pet.my-pet", shape = "box"]
"[root] local_file.pet (expand)" -> "[root]
provider[\"registry.terraform.io/hashicorp/local\"]"
"[root] local_file.pet (expand)" -> "[root]
random_pet.my-pet (expand)"
"[root] meta.count-boundary (EachMode fixup)" -
> "[root] local_file.pet (expand)"
"[root]
© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
provider[\"registry.terraform.io/hashicorp/local\"] (close)" ->
terraform graph
main.tf >_

resource "local_file" "pet" { $ apt update


filename = "/root/pets.txt"
$ apt install graphviz -y
content = "My favorite pet is ${random_pet.m
y-pet.id}" $ terraform graph | dot -Tsvg > graph.svg
}
resource "random_pet" "my-pet" {
prefix = "Mr"
separator = "."
length = "1"
}

© 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)
}

Plan: 1 to add, 0 to change, 1 to destroy.

Do you want to perform these actions?


Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.

Enter a value: yes

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

v1.19 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
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)
}

Plan: 1 to add, 0 to change, 1 to destroy.

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]

Apply complete! Resources: 1 added, 0 changed, 1 destroyed.

© 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)
}

Plan: 1 to add, 0 to change, 1 to destroy.

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]

Apply complete! Resources: 1 added, 0 changed, 1 destroyed.

© 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
}

} Plan: 1 to add, 0 to change, 1 to destroy.

...

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

Apply complete! Resources: 1 added, 0 changed, 1 destroyed.

© 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" {

Resource local_file.my-pet has


} lifecycle.prevent_destroy set, but the plan calls
for this resource to be destroyed. To avoid this error
and continue with the plan, either disable
lifecycle.prevent_destroy or reduce the scope of the
plan using the -target flag.

© 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]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

© 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

Terraform will perform the following actions:

# aws_instance.webserver will be updated in-place


~ resource "aws_instance" "webserver" {
.
.
~ tags = {
~ "Name" = "ProjectB-WebServer" -> "ProjectA-WebServer"
}
.
.
Apply complete! Resources: 0 added, 1 changed, 0 destroyed.

© 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

Create the resource first and


1 create_before_destroy
then destroy older

2 prevent_destroy Prevents destroy of a resource

Ignore Changes to Resource


3 ignore_changes
Attributes (specific/all)

© 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

resource "local_file" "pet" {


filename = "/root/pets.txt"
content = "We love pets!"
}

>_
$ cat /root/dog.txt
Dogs are awesome!

Real World Infrastructure terraform.tfstate

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

resource "local_file" "pet" {


filename = "/root/pets.txt"
content = data.local_file.dog.content
"We love pets!"
}

>_ data "local_file" "dog" {


filename = "/root/dog.txt"
$ cat /root/dog.txt
}
Dogs are awesome!

Real World Infrastructure terraform.tfstate

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

Resource Data Source

Keyword: resource Keyword: data

Creates, Updates, Destroys


Only Reads Infrastructure
Infrastructure

Also called Managed Resources Also called Data Resources

© 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

resource "local_file" "pet" { variable "filename" {


filename = var.filename default = "/root/pets.txt"
content = var.content }
variable "content" {
} default = "I love pets!"
}

© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
Shell Scripts
create_files.sh >_

#!/bin/bash $ ls -ltr /root/


-rw-r--r-- 1 root root 0 Sep 9 02:04 pet2
for i in {1..3} -rw-r--r-- 1 root root 0 Sep 9 02:04 pet1
do -rw-r--r-- 1 root root 0 Sep 9 02:04 pet3

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

resource "local_file" "pet" {


filename = var.filename
content = var.content resource "local_file" "pet" {
depends_on = [ filename = "/root/pets.txt"
random_pet.my-pet content = "We love pets!"
] file_permission = "0700
} lifecycle {
resource "random_pet" "my-pet" { create_before_destroy = true
prefix = var.prefix }
separator = var.separator }
length = var.length
}

© 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

resource "local_file" "pet" { variable "filename" {


filename = var.filename default = "/root/pets.txt"
}
count = 3
} >_
$ terraform plan
[Output Truncated]
Terraform will perform the following actions:
...
# local_file.pet[2] will be created
+ resource "local_file" "pet" {
+ directory_permission = "0777"
+ file_permission = "0777"
+ filename = "/root/pets.txt"
+ id = (known after apply)
}

Plan: 3 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
count

main.tf variables.tf

resource "local_file" "pet" { variable "filename" {


filename = var.filename default = "/root/pets.txt"
}
count = 3
} >_
$ terraform
$ ls /rootapply
pet.txt
[Output Truncated]
.

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]

© Copyright KodeKloud Check outcomplete!


Apply our full course on Terraform for the3Absolute
Resources: added, Beginners: https://kode.wiki/3PoScZd
0 changed, 0
count

main.tf variables.tf

resource "local_file" "pet" { variable "filename" {


filename = var.filename [count.index] default
default == "/root/pets.txt"
[
count = 3 "/root/pets.txt",
"/root/dogs.txt",
} "/root/cats.txt"
]

>_
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

resource "local_file" "pet" { variable "filename" {


filename = var.filename [count.index] default
default == "/root/pets.txt"
[
count = length(var.filename)
3 "/root/pets.txt",
"/root/dogs.txt",
} "/root/cats.txt",
"/root/cows.txt",
"/root/ducks.txt"
]

}
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

variable function value

fruits = [ "apple", "banana", "orange"] length(fruits) 3

cars = [ “honda”, “bmw”, “nissan”, “kia”] length(cars) 4

colors = [ “red”, “purple”] length(colors) 2

© Copyright KodeKloud Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
LengthFunction

main.tf variables.tf

resource "local_file" "pet" { variable "filename" {


filename = var.filename [count.index] default
default == "/root/pets.txt"
[
count = length(var.filename)
3 "/root/pets.txt",
"/root/dogs.txt",
} "/root/cats.txt",
"/root/cows.txt",
"/root/ducks.txt"
]

}
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

# local_file.pet[0] will be created


+ resource "local_file" "pet" {
+ directory_permission = "0777"
+ file_permission = "0777"
+ filename = "/root/pets.txt"
+ id = (known after apply)
}

# local_file.pet[1] will be created


+ resource "local_file" "pet" {
+ directory_permission = "0777"
+ file_permission = "0777"
+ filename = "/root/dogs.txt"
+ id = (known after apply)
}

# local_file.pet[2] will be created


+ resource "local_file" "pet" {
+ directory_permission = "0777"
+ file_permission = "0777"
+ filename = "/root/cats.txt"
+ idKodeKloud
© Copyright = (known after apply) Check out our full course on Terraform for the Absolute Beginners: https://kode.wiki/3PoScZd
main.tf variables.tf

resource "local_file" "pet" { variable "filename" {


filename = var.filename [count.index] default
default == "/root/pets.txt"
[
count = length(var.filename)
3 "/root/pets.txt",
"/root/dogs.txt",
} "/root/cats.txt"
]

pet[0] pet[1] pet[2]

© 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"
]

pets.txt dogs.txt 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 }

Resource Resource Updates Action

pet[0] /root/pets.txt" -> "/root/dogs.txt" Destroy and Replace

pet[1] "/root/dogs.txt" -> "/root/cats.txt" Destroy and Replace

© 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

on main.tf line 2, in resource "local_file" "pet":


2: for_each = var.filename

The given "for_each" argument value is unsuitable: the "for_each"


argument must be a map, or set of strings, and you have provided a value
of type list of string.
© Copyright KodeKloud Check out our full course on Terraform 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)
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)

The following providers do not have any version constraints


in configuration, so the latest version was installed.

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.

* hashicorp/local: version = "~> 1.4.0"

Terraform has been successfully initialized!

© 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

You might also like