S.
No Description Details Notes
The terraform init command initializes a working directory containing Terraform
configuration files. This is the first command that should be run after writing a new
1 terraform init
Terraform configuration or cloning an existing one from version control. It is safe to run
this command multiple times.
The terraform validate command validates the configuration files in a directory, referring Validate runs checks that verify whether a configuration is syntactically valid and internally
2 terraform validate only to the configuration and not accessing any remote services such as remote state, consistent, regardless of any provided variables or existing state. It is thus primarily useful for
provider APIs, etc. general verification of reusable modules, including correctness of attribute names and value types.
The validate command, on the other hand, is used to validate the configuration internally i.e., locally
on the host system. Its focus is on validating the Terraform configuration files for syntax and
The purpose of the plan command is to validate the changes in the configuration and internal consistencies.
3 Difference b/w validate vs plan
highlight the same by referencing remote state and cloud resources.
Thus, validate command does not depend on any state file or information regarding deployed
command
The terraform fmt command is used to rewrite Terraform configuration files to a
4 Terraform fmt canonical format and style. This command applies a subset of the Terraform language
style conventions, along with other minor adjustments for readability.
The plan command does three things:
Ensures the state is up to date by reading the current state of any already-existing remote
The Terraform plan command is available to bring visibility to your IaC deployments. The
infrastructure.
plan command reports on changes to infrastructure, but it does not apply any of the
5 Terraform plan Determines the deltas between the current configuration and the prior state data.
proposed changes. Instead, it creates a reviewable execution plan, which you can use to
Proposes a series of changes that will make the remote infrastructure match the current
confirm that the proposed changes are expected.
configuration.
If there aren’t any deltas, the output will report that no actions need to be taken
If the apply command is run without any options it will run a terraform plan first, ask the
6 Terraform apply
user to confirm the planned actions, and then execute those changes once confirmed.
The core Terraform workflow has three steps:
Write - Author infrastructure as code.
7 The Core Terraform Workflow
Plan - Preview changes before applying.
Apply - Provision reproducible infrastructure.
he terraform state file, by default, is named terraform.tfstate and is held in the same directory
Terraform logs information about the resources it has created in a state file. This enables where Terraform is run. It is created after running terraform apply. The actual content of this file is
8 terraform.tfstate Terraform to know which resources are under its control and when to update and a JSON formatted mapping of the resources defined in the configuration and those that exist in your
destroy them infrastructure.
It is not a good idea to store the state file in source control.
When working on Terraform projects in a team, this becomes a problem as multiple
You should store your state files remotely, not I general we save in central repo like S3 Bucket in AWS or Storage Account in Azure & It is not a
9 people will need to access the state file when we store state file on locally & If local M/C
on your local machine! good idea to store the state file in source control like GIT.
crashes the state file will be lost.
1.The code can be distributed across multiple files.
2.Declaring variable is easy
3.Performing dry run is very easy and very verbose
4.Terraform modules.
10 Terraform Advantages
5.Terraform workspaces
6.Terraform can be used for AWS, Azure, GCP etc (Code changes from cloud to cloud but
logic remains same)
7.Code readibality is easy.
Providers are a logical abstraction of an upstream API. They are responsible for
11 Provider AWS, Azure, GCP
understanding API interactions and exposing resources.
Each resource block describes one or more infrastructure objects, such as virtual
12 Resource
networks, compute instances, or higher-level components such as DNS records.
resource <res_Type> <res_Name>{
13 Syntex
}
Argument Reference
Attributes Reference
You can use provisioners to model specific actions on the local machine or on a remote
14 Provisioners
machine in order to prepare servers or other infrastructure objects for service.
If we create a Infrastructure by using If we are ok with changes then the same have to update in our code , we we dont want taht changes
By using Terraform Plan , we can check any changes as terraform Plan compares with
15 Terraform, after that some one changed we can run terraform apply , which will overide the manual changes as they are not in existing state
existing state file.
manually then how will we come to know ? file
1.Simple way was by commenting out the code releated to taht VPC and running the
terraform apply .
How to delete a unwanted VPC after creating
16 2. By using Command #terraform destroy -target aws_vpc.VPC Name (Deleating an
with using terraform
Individual resource) Ex: terraform destroy -target aws_vpc.my_vpc & we have to remove
in code otherwise terraform will create the same while apply
terraform state rm aws_vpc.vpc_name
If i had created the VPC main1 using Terraform and now i don't want to configure or
17
control that vpc any more with terraform we will remove that VPC from state file
Ex: terraform state rm aws_vpc.main1
terraform state mv old_name new_ name If we had created a resource with terraform and now i want rename it, this command
18 updates the name in state file , after that we have plan and apply. also we have change the
ex: terraform state mv my_vpc APP_ Server code afterwards
We will create backend.tf file and we will save the following script in backend.tf
terraform {
backend "s3" {
19 How to store state file aws S3 bucket bucket = "bucket_name"
key = "name.tfstate" >> Name for state file in bucket
region = "us-east-1"
}
20 terraform state list to see list of resources created
Configuration for the AWS Provider can be derived from several sources, which are
applied in the following order:
1.Parameters in the provider configuration
2.Environment variables
Authentication and Configuration
3.Shared credentials files
4.Shared configuration files
5.Container credentials
6.Instance profile credentials and region
21 aws configure To change or check Access Key & secreate key in your CLI
Step 1 : #aws configure If we have 3 different envernoment's like Development, Testing & Production , then instade of
step 2 :add access Key & secreate key here [default] we can give as [Development], [Testing], [Production] and save credentials and we can run
Note : These credentials will be saved under [default] header in aws credentials file in the same code for different enivernoments
If i dont want give my credientials directly in
22 aws folder ("C:\Users\.aws\credentials) as follows For [default] we give in code profile = [default],
terraform code
[default] For [Development] we have to give profile = [Development] and so on.....
aws_access_key_id = AKIAWW2LFO6T2VJPUJFC If we have saved the credentials in another location the we have to mention path also as
aws_secret_access_key = 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 shared_credentials_file = "path where credentials saved"
What is the difference between variable.tf and variable.tf file is used to declare variables, where as terraform.tfvars file is used to pass
23
terraform.tfvars on the values for variables decleared in variable file
we can change the terraform.tfvars file name,
terraform plan --var-file dev.tfvars
24 say dev.tfvars but we have use extra command
terraform apply --var-file dev.tfvars
as shown to run
If terraform knows by default which is is depend on another know as Implicit
25 Implicit and Explicit Dependencies If we specify the dependency the it is know as Explicit dependency
Dependency
27 depends_on [......] Creates the dependence while creating resources If we want to create a x resource only after creating resource y Ex: depends_on [y]
we Use the Amazon Web Services (AWS)
provider to interact with the many resources
26 supported by AWS. You must configure the
provider with the proper credentials before
you can use it.
In terraform we have two option in for creating sources
1. Resource : Used to create resource
28 What are Data Sources in terraform
2.Data Source: Used to Import resources which are unknow to terraform and deploy
necessary updates
What is the difference between Public & Private Whichever subnet having route towards internet is Public subnet & which doesn’t have route
Subnet towards internet is private subnet
Count It is a command used to execute the code repeatedly Say Count = 3, code below count will exicute 3 times
If we have multiple value we will give in list which is List has indexing , say azs = ["us-east-1a","us-east-1b","us-east-1c"],
donated by [ ] so us-east-1a has index '0', us-east-1b has index '1', us-east-1c has index '2'
element(list,index) Ex: list =var.azs & index = count.index
splat syntex var.list[*].id Ex: subnet_id = element(aws_subnet.public-subnets.*.id,count.index)
Length length > Counts the values in list Ex: azs = ["us-east-1a","us-east-1b","us-east-1c"] Ex: List is azs = ["us-east-1a","us-east-1b","us-east-1c"], length (var.azs)
The terraform taint command informs Terraform that a particular object has become degraded or
terraform taint damaged. Terraform represents this by marking the object as "tainted" in the Terraform state, and
Terraform will propose to replace it in the next plan you create.
map
lookup
EX: for Shell
if ["ENV" == "PROD"]
then
echo "Deploy 3 EC2 Instance"
else
condition It can be used as if condition in shell echo "Deploy 1 EC2 Instance"
fi
the same can be written in terraform using condition command as we cant write shell scrip in
terraform
count = [var.env = "PROD" ? 3: 1]
alias alias
SSh Connection
file provisioner
Terraform Provisioners
remote-exec
local-exec
null_resource
subnetcidrs
sed
remote_exec Runs at Ec2 instance directly, henc we have to provide connections for remote_exec
local_exec Run at terraform hence no separet connection details required.
Terraform Workspaces Same code but multiple deployments
Terraform Modules