Terraform

The universal remote for the cloud. Use one language (HCL) to build infrastructure on AWS, Azure, Google Cloud, and 100+ other providers.

🧱 Core Building Blocks

1. The Provider

The plugin that talks to the API. You tell Terraform "I want to use AWS", and it downloads the AWS translator.

2. The Plan

The safety check. Terraform calculates exactly what it will do (Add +1, Change ~2, Destroy -0) so you can approve it.

3. The State

The "Brain". A file that remembers what resources exist in real life, mapping your code to real IDs.

4. Data Sources

Read-Only. Allows you to fetch info about existing resources (e.g., finding the ID of the latest Amazon Machine Image) without creating them.

📂 Best Practice: Folder Structure

my-project/
├── modules/        // Reusable blueprints
│   ├── vpc/
│   └── ec2/
├── environments/   // Live deployments
│   ├── dev/
│   │   ├── main.tf
│   │   └── terraform.tfvars  (Small size)
│   └── prod/
│       ├── main.tf
│       └── terraform.tfvars  (Large size)
      
Always separate DEV and PROD into different folders (or workspaces) so they have separate State Files.

đŸ•šī¸ The Universal Constructor

Mission: Write a multi-cloud config and deploy it.
Bonus: Try clicking a created server to "Sabotage" (delete) it, then run Plan/Apply to fix it (Self-Healing).

main.tf HCL
resource "aws_instance" "web" { ami = "ami-0c55b159" instance_type = "t2.micro" } resource "azurerm_vm" "db" { location = "West Europe" size = "Standard_DS1_v2" } resource "google_compute_instance" "app" { zone = "asia-east1-a" machine_type = "f1-micro" }
$ terraform init
Terraform has been successfully initialized!

$ _