Terraform Hands On Notes

What is Terraform?
Terraform, is an infrastructure as a code tool that lets you build, change and version cloud and on-prem resources safely and efficiently.

Create EC2 instance using Terraform

**Ec2.tf

Resource “aws_instances” “myec2”{

Ami=”ami-08323r2yr28e0992”

Instance_type=”t2.micro”

Provider.tf

Provider “aws”{

Region=”us-east-1”

Access_key=”rh38ff1of”

Secret_key=”dvnievievev”

}

->After writing the configuration in main.tf and provider.tf when

--> Terraform init command is given it initializes the backend, downloads the necessary

-->packages and plugins based on the provider.(if provider is AWS, the AWS version will be installed and all the necessary plugins and packages of AWS will be installed)

-->After this by giving ->ls-al command will list the files created after executing Terraform init

--> .terraform and .terraform.lock.HCL files are created.
----------------------------------------------------------------------------------------

terraform plan ->By running terraform plan only we will know what is going to be created
plan will show>– add-1 , change -0,destroy-0

-----------------------------------------------------------------------------------------Terraform apply->Will create the resource and the instance will be in running state

After applying , state file will be created(terraform.tfstate)

->terraform show->shows the state file that got created

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

Scenario- To Modify instance type

Now have to modified the instance_type from t2.micro t2.large

After making the change in ec2.tf file,

When terraform plan is given ,it refreshes the state ,and changes the state to desired state.

Now plan in output shows added-0, changed-1 , destroy-0

Then when terraform apply is given, the change happens

Now when terraform show is given it shows the instance_type as t2.large

Delete AWS resource

To delete the resource, terraform destroy is used

Now the output will show 0-added,0-changed,1-destroyed

Terraform Providers

1.Official->AWS,Azure,GCP

2.Verified->Third party Providers-Alibaba cloud

3.Community-Individual person -ucloud

Terraform Providers vs Resources

AWS is an example of provider under which we have many Services like EC2,AMP ..under each of this services we have resources

![](file:///C:/Users/918305/AppData/Local/Temp/1/msohtmlclip1/01/clip_image002.jpg align="left")

For example EC2 service has …aws_ami, aws_instances as resources.

Terraform State

->Terraform stores state to manage the infrastructure and configuration

->State is used to map the real world resources to the configuration

Terraform Refresh-> Updates state with real infrastructure

Desired state = current state

SCENARIO 1: The ec2.tf and Provider.tf is configured as below. Now after executing terraform apply the instance is created and is in the running state. Now if we manually stop the instance from the AWS dashboard and give terraform plan what will happen?

Ec2.tf

Resource “aws_instances” “myec2”{

Ami=”ami-08323r2yr28e0992”

Instance_type=”t2.micro”

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

Provider.tf

Provider “aws”{

Region=”us-east-1”

Access_key=”rh38ff1of”

Secret_key=”dvnievievev”

}

Answer->The terraform plan command shows no change in infrastructure ,its up to date.

This is because the instance state as running is not configured /managed by terraform in the configuration file, So the terraform doesn’t consider.

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

SCENARIO 2: The ec2.tf and Provider.tf is configured as above. Now after executing terraform apply the instance is created and is in the running state. Now if we manually stop the instance and change the instance_type from t2.micro to t2.large from the AWS dashboard and give terraform plan what will happen?

Answer-> In this case terraform plan refreshes the state and asks for change.This happens because we have managed the instance_type in the configuration file(instance_type=t2,micro)

But When given terraform show, the state file shows the old values as t2.micro and running even though the instance is stopped manually and instance_type is changed.

Now when we give terraform refresh, it updates the state with real infrastructure->updates as t2.large and stopped.

Well now the state change is happened according to the current state(t2.large) but the desired state is t2.micro

Now when terraform apply is given it campares with the desired state and refreshes the state as t2.micro and running.

So whats in the desired state will be changed in the state file.

Doubt-> while performing terraform plan,automatically refresh happens why does the state file didn’t change? Since the change was done manually? But the change is showed in plan? But it didn’t get updated in state file why?

Terraform Variables

->Repeated static value easily handle

->Replace the hardcoded value

Example

Security.tf

Resource “aws_security_group” “demo_var”{

Name=”demo_var”

Ingress = {

Description =”TLS form VPC”

Form_port=443

To_port=443

Protocol=”tcp”

Cidr_blocks=[var.client.ip]

Variables.tf

Variable “client_ip”{

Default=”10.60.60.60/32”

}

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

Attributes and Output

Attributes

-Every resource has arguments and Attributes

-Terraform use this for output

-Terraform input this to other resources being created

Output

-After resource is created fetch the value from the resource attributes

Eip.tf

Resource “aws_eip” “myeip”{

Vpc=true

}

Output.tf

Output “demo_output”{
Value=aws_eip.myeip.public_id à public_id is the attribute present for this resource

}

Output

Demo_output=”34.345.123.356”

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

If wanna display all the attribute of the resource just leave blank by just giving value = Value=aws_eip.myeip

Terraform fmt vs Validate

Terraform fmt

->Terraform fmt is used to rewrite the configuration files to a conanical format and style(In readable format)

->If there is no formatting issues nothing will be shown after executing the command

->If there is any identation and space issues shows the file with changes and does the changes as well

-write=false->don’t overwrite the input files

-diff-display differene of formatting changes

->terraform fmt -diff output.tf -> this command shows the diff and makes the changes

->terraform fmt -write=false -diff output.tf- this command shows the difference in files and the changes will not be done

Validate

Terraform Validate- Checks whether any code error in the configuration file

-This command checks for any error in code and syntax. It doesn’t show formatting issues

Saturday-0ct-1

1)Terraform State Command

2)Terraform Import

3)Terraform Graph

4)Terraform Lifecycle Rules’

5)Terraform resource behaviour

6)Terraform Remote State

17. what is lifecycle rules?

----create_before_destroy ->create resource first and destroy later

----prevent_destroy->prevents destroy of a respurce

---ignore_chnages->ignore changes to resource attributes

Terraform Import

Used to update the existing infra,updates only the state file, need to write configuration file for the resource