Terraform-Concepts

References:

  1. Course video - https://www.youtube.com/watch?v=YcJ9IeukJL8&t=3014s
  2. Labs - https://bit.ly/YT_Terraform

What is Terraform?

Pasted image 20231202161526.png In Traditional way you want to build an Infrastructure you have to go through manual method deploying each and every component one by one and configuring them which would be very tedius task and difficult to achieve between limited time space.
While Infrastructure as Code Helps you to be focus on mapping out architecture of Infrastructure in Code format so with click or command you can deploy reproduceable and scalable infrastructure within seconds of time.

Pasted image 20231215144608.png

Types of IAC Tools

Pasted image 20231215145513.png

Different Providers wanted to solve the problem Scripting IAC with their own tools or proprietary scripting language.
In this race they made their own platforms and methods.
Collectively these set of Tools called as IAC tools, each tool addressing the specific IAC need.

IAC can be classified into three major common types:

We'll see each of these in a bit detail:

Configuration Management:

These includes Ansible, puppet, saltstack

Server Templating

These includes Docker, Packer, Vagrant

Provisioning Tools

These includes Terraform and Cloud Formation

Terraform leverages the use of APIs across various platform provider to deploy the infrastructure.

Pasted image 20240116142023.png

HCL - Hashicorp Configuration Language

Using programming scripts or shell scripts to deploy architecture is not always everyones cup of tea and not neccessary everyone are well equiped with skills require to do so.
Therefore HCL Hashicorp Configuration Language Provides Easy to use and intuitive way to create your infrastucture with one click.

Pasted image 20240116140803.png

resource "aws_instance" "webserver" {
	ami           = "ami-0edab43b6fa892279"
	instance_type = "t2.micro"
}

resource "aws_s3_bucket" "finance" {
	bucket = "finance-21092020"
	tags   = {
		Description = "Finance and Payroll"
		}
}
resource "aws_iam_user" "admin-user" {
	name = "lucy"
	tags = {
		Description = "Team Leader"
	}
}

This sample code is use to provision a new ec2-instance on aws cloud.
This code is declarative and can be maintain in a version control system allowing to be distributed to other teams.

Keypoints

The code we defined is a desired state that we want our infrastructure to be in.
but before deploying this code the state of our infrastructure is empty and there is nothing so what does the terraform do to transform the empty state into desired state.

Terraform works in three phases:

  1. Init
  2. Plan
  3. Apply
Init phase:

During the init phase terraform initializes the project and identifies the providers used for the target enviroment.

Plant Phase:

During the plan phase terraform draws plan to get to the target state.

Apply Phase

During the apply phase terraform makes the neccessary changes to the target to bring it to the desired state.

Resource

Every object that Terraform manages is called as resource.
A resource can be a file, vm, or could be services like ec2 instance and s3 bucket, dynamodb table, Iam users, Iam groups, roles policies, etc.

Pasted image 20240116155220.png

State

Terraform records the state of infrastructure as it is seen the real world and based on this it can determine what actions to take to updating resources for a particular platform.

It make sure that infrastructure is always in the defined state at all times.
The state is blueprint of the infrastructure defined by terraform.