Skip to main content

Command Palette

Search for a command to run...

Day 60 - Terraform

Published
3 min read
Day 60 - Terraform

What is Terraform?

Terraform is an Infrastructure as Code (IaC) tool by HashiCorp, allowing developers to define, provision, and manage cloud (AWS, Azure, GCP) and on-premises infrastructure using human-readable configuration files (HCL) instead of manual processes. It automates building, updating, and destroying resources like VMs, networks, and storage, ensuring safe, efficient, version-controlled, and repeatable infrastructure deployments across different providers.

Key Concepts

Infrastructure as Code (IaC): Managing infrastructure (servers, databases, networks) through code, allowing for versioning, testing, and automation, similar to application code.

Declarative Syntax: You declare the desired end-state of your infrastructure (e.g., "I need two web servers"), and Terraform figures out the steps to get there.

Providers: Terraform uses providers (like AWS, Azure, Kubernetes) to interact with different service APIs, creating a unified language for diverse environments.

State File: Terraform maintains a state file to track the actual state of your deployed infrastructure, helping it compare against your code for updates or deletions

How it Works (Simplified Workflow)

  1. Write Code: Define your infrastructure in .tf files using HashiCorp Configuration Language (HCL).

  2. Initialize: Run terraform init to set up the backend and providers.

  3. Plan: Run terraform plan to see what changes Terraform will make.

  4. Apply: Run terraform apply to provision or update the infrastructure to match your code.

Benefits

  1. Automation: Automates manual, error-prone infrastructure setup.

  2. Consistency: Ensures environments (dev, staging, prod) are identical.

  3. Multi-Cloud: Manages resources across different cloud providers with a single tool. Version Control: Code can be stored in Git, enabling history, collaboration, and rollbacks.

  4. Modularity: Create reusable modules for complex setups, reducing repetition.

Task 2: Answer below questions

  • Why we use terraform?

    We use Terraform to manage infrastructure (like servers, networks, databases) as code, allowing for automated, repeatable, and version-controlled provisioning across different cloud providers (AWS, Azure, GCP) and on-premise systems, ensuring consistency, reducing errors, simplifying multi-cloud setups, and enabling faster, safer deployments and disaster recovery. It provides a unified way to manage diverse environments (dev, staging, prod) using a single language (HCL).

  • What is Infrastructure as Code (IaC)?

    We use Terraform to manage infrastructure (like servers, networks, databases) as code, allowing for automated, repeatable, and version-controlled provisioning across different cloud providers (AWS, Azure, GCP) and on-premise systems, ensuring consistency, reducing errors, simplifying multi-cloud setups, and enabling faster, safer deployments and disaster recovery. It provides a unified way to manage diverse environments (dev, staging, prod) using a single language (HCL).

  • What is Resource?

    A resource is anything available in the environment that can be used to satisfy human needs, wants, or achieve goals, including tangible items like water and minerals, intangible assets like skills, or people themselves (human resources). Resources must be technologically accessible, economically feasible, and culturally acceptable to be considered useful, and they can be natural (sunlight, timber) or human-made, renewable or non-renewable, and have different values (economic or otherwise).

  • What is Provider?

    A resource is anything available in the environment that can be used to satisfy human needs, wants, or achieve goals, including tangible items like water and minerals, intangible assets like skills, or people themselves (human resources). Resources must be technologically accessible, economically feasible, and culturally acceptable to be considered useful, and they can be natural (sunlight, timber) or human-made, renewable or non-renewable, and have different values (economic or otherwise).

  • What is State file in terraform? What’s the importance of it ?

    Terraform's state file (e.g., terraform.tfstate) is a JSON file that maps your infrastructure code to real-world resources, tracking their current state, metadata, and attributes, allowing Terraform to understand what exists, plan changes (create, update, delete), and manage infrastructure efficiently. Its importance lies in providing a crucial reference for infrastructure management, enabling intelligent updates, facilitating collaboration (when stored remotely), and ensuring consistency, preventing resources from being recreated unnecessarily.

  • What is Desired and Current State?

    Desired state is the outcome you want to achieve, explicitly defined in your .tf configuration files using the HashiCorp Configuration Language (HCL).The current state is the actual reality of your provisioned infrastructure.