Day 60 - Terraform🔥
What is Terraform?
Terraform is an Infrastructure as Code (IAC) tool that allows you to create a blueprint of your infrastructure in code. It helps automate infrastructure tasks, such as cloud services.
With Terraform, you can create, manage, and update resources like virtual machines, networks, and storage in an automated, repeatable way.
For example, in AWS, you can automate the creation of EC2 instances, allowing you to create multiple instances at once using Terraform.
Terraform is useful for automation across multiple cloud platforms like AWS, Azure, and GCP. You only need to learn Terraform to use it on all these platforms. It uses HCL (HashiCorp Configuration Language), which is similar to JSON but easier to read and understand.
Task 1: Install Terraform
Follow the official Terraform installation guide based on your operating system. Here’s a quick overview:
Installation Steps (Ubuntu/Linux):
Update your system:
bashCopy codesudo apt update && sudo apt upgrade
Download and Install Terra**form**:
bashCopy codesudo apt install -y wget unzip wget https://releases.hashicorp.com/terraform/<VERSION>/terraform_<VERSION>_linux_amd64.zip unzip terraform_<VERSION>_linux_amd64.zip sudo mv terraform /usr/local/bin/
Verify Installation:
bashCopy codeterraform --version
Task 2: Answer the Questions
What is IAC ?
Infrastructure as Code (IAC) involves managing and setting up “provisioning” infrastructure using code instead of doing it manually. Instead of manually configuring resources, you define them in configuration files, ensuring automation, consistency, and repeatability.
→ manual: to organize tasks like creating, deleting, or forming
→ provisioning: to assign resources, e.g., allocate 2 RAM and 2 CPU to a server.
Example of IaC in Terraform:
hclCopy coderesource "aws_instance" "example" {
ami = "ami-12345678"
instance_type = "t2.micro"
}
Example: Instead of clicking buttons to launch an EC2 instance, you define it in a .tf
file and deploy it with Terraform.
Benefits of IaC:
Reduces human error.
Ensures consistency.
Simplifies version control.
Why do we use Terraform?
Automation: No need to manually create infrastructure resources. Terraform automates everything.
Version Control: Terraform configurations can be stored in Git, making it easy to track and manage changes.
Multi-Cloud Support: Terraform works with multiple cloud providers like AWS, Azure, and GCP.
Consistency: Ensures that your infrastructure setup is consistent across all environments (dev, staging, production).
Scalability: Easily scales resources up or down.
Collaboration: Teams can version control and share Terraform configurations.
What is a Resource?
A resource is a single infrastructure component, such as an EC2 instance, S3 bucket, or VPC.
AWS Instance: An EC2 virtual machine.
Google Cloud Storage: A storage bucket.
resource "aws_instance" "example" { ami = "ami-0c55b159cbfafe1f0" instance_type = "t2.micro" }
Here, aws_instance
defines an EC2 instance as the resource.
What is a Provider?
A resource is a single infrastructure component, such as an EC2 instance, S3 bucket, or VPC.
Examples:
aws
: Manages AWS resources.azurerm
: Manages Azure resources.google
: Manages Google Cloud resources.
Example Configuration:
hclCopy codeprovider "aws" {
region = "us-east-1"
}
This tells Terraform to use AWS as the provider and work in the us-east-1
region.
What is a State File? Why is it Important?
The state file (terraform.tfstate
) keeps track of the current state of your infrastructure.
Purpose:
It helps Terraform know what resources are already created and what needs to be updated, added, or removed.Importance:
Prevents re-creation of existing resources.
Ensures that the actual infrastructure matches the configuration.
What are Desired State and Current State?
Desired State: The infrastructure defined in your Terraform configuration files.
Current State: The infrastructure actually running in your cloud environment.
Terraform ensures these states match. If they don’t, it applies the necessary changes to align them.
Sample Terraform Configuration
Here’s an example to create an EC2 instance on AWS:
provider "aws" {
region = "us-east-1"
}
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
tags = {
Name = "ExampleInstance"
}
}
Run the following commands to deploy it:
Initialize Terraform:
terraform init
Plan Changes:
terraform plan
Apply Configuration:
terraform apply