How To Use Terraform
How To Use Terraform
How To Use Terraform
https://registry.terraform.io/browse/providers
Terraform Architecture?
Core—config.tf state
We save the configuration file extension in .tf and state file is created to save all
the configuration. These two files are mingle know as core.
Providers – aws , azure , GCP .. 100+
Provider “aws” {
Region = “us-east-1”
Access_key = ”PUT-YOUR-ACCESS-KEY-HEAR”
Secret_key = “PUT-SECRET-KEY-HEAR”
}
Terraform commands:-
https://www.youtube.com/redirect?
event=video_description&redir_token=QUFFLUhqbnBsdlp1ZkFzTC1xMDRDYkJZS
WdoODdYcGx4QXxBQ3Jtc0ttYzRuMUZHNngxUXNMR2NlSFppRk90RlhaMUpaRGIt
NXBYUGlqYlhfOWcyd3N2bHl4UEZ3WEtLeGZhZzl2V2xnbGpQZ19jVTJjeDhQYWQ5a
29XbllOSG8tcnNUeERvaFF3bC1ZSFNqSklma2ptZ2g0NA&q=https%3A%2F
%2Fwww.terraform.io%2Fdocs%2Fcli%2Fcommands%2Fapply.html
to create a instance
create a empty directory and create ec2.tf file in the mention below
Resource “aws_instance” “myec2” {
Ami = “ami-084xxxxxxxxx”
Instance_type = “t2.micro”
}
Again create provider.tf file
Provider “aws” {
Region = “us-east-1”
Access_key = ”PUT-YOUR-ACCESS-KEY-HEAR”
Secret_key = “PUT-SECRET-KEY-HEAR”
}
Now execute using cmd
#terraform init
This cmd will download the package
To check the code in the file .tf
#terraform plan
Now apply the code.
#terraform apply
It will create a instance in aws in localmachine one more folder is created is
terraform.tfstate is file in future we can modify in the file only it will reflect.
To view the history of instance details.
#terraform show
Variables:-
Repeated Static value easily handle
Replace the hardcore value
Ex:-
Resource “aws_security_group” “demo_var” {
Name = “demo_var”
Ingress {
Description = “TLS from vpc”
From_port =443
To_port = 443
Proctol = “tcp”
Cidr_block =[10.20.30.40/32]
}
}
How to create variable :-
Variable.tf
Variable “client_ip” {
Default = “10.60.60.60/32”
}
Now change in the conf file in Cidr_block =[10.20.30.40/32]
cidr_block = [var.client_ip]
to check the file
#terraform valid
To execute
#terraform plan
#terraform apply
Output:-
After created resource fetch the value from resource attribute
Now create a elastic ip in aws
Eip.tf
Resource “aws_eip” “myeip”{
Vpc = true
}
To view the output of the eip
Output.tf
Resource “Demo_outputeip”{
Value = aws_eip. Myeip.public_ip
}
V8:- attributes value to other resources
As as above combined and execute
Variable “username”{
type = number
https://registry.terraform.io/modules/terraform-aws-modules/elb/aws/latest
for reference.
#terraform output
# terraform state mv
to change the State file without destroy
#terraform state pull > backup
this cmd to take a backup the state file
#terraform state rm
to remove the state file
We have to write the code for manual creation iin aws console same
comnfiguration. Below import cmd
V15:-terraaform graph
- prevent_destroy
- ignore_changes
# export TF_LOG=TRACE
We can set the variables TF_LOG= TRACE most of them are use Trace and
Debug.we can easily find out the checkpoints step by step.
#export TF_LOG_PATH=/tmp/tfdebug.log