Terraform

download (2)

What is Terraform?

What is Infrastructure as Code?

Why Terraform?

How does Terraform work?

Terraform creates and manages resources on a cloud platform and other services through their APIs. Providers enable Terraform to work with virtually any platform or service with an accessible API.

Providers are a logical abstraction of an upstream API. They are responsible for understanding API interactions and exposing resources. Providers supported by Terraform, there are officially 130 providers supported by Terraform:

Install Terraform

Windows

Download Terraform

Extract the executable to a directory (E.g. c:\terraform)
Go to the Environment variable and update the system variables in the PATH
Ubuntu/Debian
wget -O- https://apt.releases.hashicorp.com/gpg | gpg --dearmor | sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install terraform
Centos/RHEL
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
sudo yum -y install terraform

Let’s get started with Terraform

To use AWS provider and create AWS infrastructure we need to configure AWS CLI

Follow this article to configure AWS CLI

Typical Terraform workflow involves 3 steps - Write, Plan, and Apply

When we create infrastructure after executing “terraform apply” command. Terraform creates a state file called terraform.tfstate this state file contains all the information about the resources created using Terraform. This state file keeps track of resources created by your configuration and maps them to real-world resources. The state file is a sensitive file as it contains information about the infrastructure that we have created. You should never push this file to any version control system like GitHub. Store terraform.tfstate file in the backend to keep it safe.

The backend supported by Terraform:

Variables in terraform

variables can be defined in variables.tf file and can be used in configuration files as var.variable_name

Types of variables

The type constructors allow you to specify complex types such as collections:

How to define variables in variables.tf file using a parameter (default, type, and description)
variable "filename" {
   deafult = "test"
   type = string
   description = "configuration file namee"
   sensitive = false
   }

Define environment variables in terraform.tfvars or terraform.tfvars.json

Terraform Commands

Terraform Lifecycle Rule

#### Output Variables in Terraform
Output variables are used to store the value of the expression in terraform

output “public_ip_addr” { value = aws_instance.jenkinsserver.public_ip description = “print public ipv4 of Jenkis Server” }

#### If one resource is dependent on another and we want that resource to be provisioned before use depends_on

depends_on = [ aws_instance.jenkins ] ```

Terraform hands-on