TERRAFORMS
IAC--> Infrastructure as a code
automate your infrastructure provisioning you can use
terraform
CF -- aws
ARM --> azure
Create by hashicorp
Using this terraform u can automate infrastructure creation using
aws/azure/gcp
terraform is generic
IAC has 2 approaches
imperative: Like if u want to create instance or do any
changes to existing instance whatever might be u create
custom file in that custom file u will use step by step
procedure or sequential procedure
here order matter lot
create vp,subnet,route table
ex: aws CLI and bash scripts
declarative: there will be config file whatever resources you want
you can create it.
create servers, vpc everything mention in this config file and not in
order any order it can create infrastructre for you
ex: terraform, cloud formation
Terraform uses declarative approach which is using developing or
modifying the infrastructure very safely
Developed in 2014 but gained popularity from 2018
Terrform written in Golang + HCL (hashicorp language)
Othertools developed by hashicorp are vault, consul
terrform is like open source
It has 2 editions
enterprise free and enterprise paid edition
why we want to use terraform u can use cloud formation? Reason to use
terraform is:
1. Speed
you can create infrastructire very fast compared to CF/ARM/DM
2. Consistency:
when u are trying to create infrastructure manually
if u are creating VPC and while creating u need to mention ur CIDR
block if not it will not allow
same way in terraform when u are trying to create
it will give some error and after certain validation only it will
create
3. Scalability: using same config file u can create infra for
test env/uat env/dev env/prod env
by doing some minor tweaks
4. Different IAC tools:
Terraform can be used with different IAC tools
we can integrate with ansible and other tools , what ever config files
u created can be used in CF/ARM template
5. Open source and proprietary
6. Reduce the Time
execution time is less the number of lines of code is very less
approximately 2 mins execution time
7. Simple language to understand: terraform is written in GOlang and
Hashicorp
json u need to keenly write in to syntax if u miss some,or
paranthesis it takes some time to debug
Terrform is very easy to write which we will see soon
say u want to create 4 servers, under resources in CF u need to create
different block for all i1,i2,i3,i4 but here u can use count parameter
it creates 4 instances if u give count=4
terraform.io
official site
install terraform on windows
go to windows amd64 one u can use
extract it
terraform executable file
go to c drive
create folder known as terraform
copy the executable to terraform folder
terraform --version
its not there
go to cmd
copy address
and u need to create env variable
Run
sysdm.cpl
it will open setings --> system properties--> advanced
click on path env variable
New
close the command prompt reopen it again
terraform --version
it got installed
u can do that even on linux machine also
open ec2 machine of ubuntu
search for installing terraform on ubuntu machine
copy the curl command
and other commands and paste it
In cmd
terraform
terraform init --> if ur config files written in aws it will donwload
all the related plugins of aws via internet , it will initialize the
environment
provider "aws" {
region="us-east-2"
validate--> It check for syntax errors when ur writeconfig file
plan --> Kind of blueprint, what ever u want to create using config
file this command shows ypu what ever it will nbe created
apply --> creates ur infrastructure
fmt --> reformat your configuration according to standards of
terraform
say you written access key or security key without any alignment
it will format and put in order
graph --> whatever steps u mentioned in config to create it will try
to show in graohical format
show --> it will show the current state of ur saved plan
These are frequently used commands, though we have many I showed u
only frequently used commands
output --> To printv a output
workdspace/environment both are same
which workspace u want to create
like dev/uat/test/prod
by default we have default work space
terraform workspace list
terraform workspace new dev
terraform workspace select default
import --> ifu created any service manually u can import into
terraform using import command
Create one ec2 instance
also create one iam user or any other user security key and access key
provider "aws" {
region="us-east-2"
access_key=""
secret_key=""
}
we have vast documentatuion in google
just browse in google
create ec2 instance in aws using teraform
create newFolder
save the file as test.tf
navigate to that path in cmd
terraform init
terraform validate
terraform plan
terraform apply
terraform destroy
.state file has in json format
all script u created in terraform will be in json format
how to create VPC in aws using terraform
registry.terraform.io official site
copy and paste and edit as per your requirement
use the synopsis and create all vpc requirement