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:
Extract the executable to a directory (E.g. c:\terraform)
Go to the Environment variable and update the system variables in the PATH
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
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
terraform version
Resources are the most important element in the Terraform language. Each resource block describes one or more infrastructure objects, such as virtual networks, compute instances, or higher-level components such as DNS records. Terraform configuration files are written in HCL to deploy infrastructure resources, these files have .tf extensions
To use AWS provider and create AWS infrastructure we need to configure AWS CLI
Follow this article to configure AWS CLI
mkdir terraform-project
cd terraform-project
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "4.35.0"
}
}
}
resource "aws_instance" "my_ec2_instance" {
ami = "ami-08c40ec9ead489470"
instance_type = "t2.micro"
tags = {
Name = "FirstEC2Instnace"
}
}
Typical Terraform workflow involves 3 steps - Write, Plan, and Apply
terraform init
terraform plan
terraform 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 can be defined in variables.tf file and can be used in configuration files as var.variable_name
The type constructors allow you to specify complex types such as collections:
variable "filename" {
deafult = "test"
type = string
description = "configuration file namee"
sensitive = false
}
Define environment variables in terraform.tfvars or terraform.tfvars.json
terraform init
terraform plan
terraform apply
terraform validate
terraform fmt
terraform providers
terraform providers mirror /<file_path>
terraform output
terraform refresh
terraform show
terraform destroy
lifecycle {
create_before_destroy = true
}
lifecycle {
prevent_destroy = true
}
``` provider “aws” { region = “us-east-1” alias = “east” }
use it – aws.east
#### 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 ] ```