Back to Home
    TerraformTerraformintermediate

    Terraform

    This cheat sheet covers practical Terraform usage for managing infrastructure as code.

    terraformiaccloud

    Getting Started

    2 topics

    Install Terraform

    Steps to install Terraform on your system.

    bash
    # On macOS
    brew install terraform
    
    # On Ubuntu
    sudo apt-get update && sudo apt-get install -y terraform

    Ensure your PATH includes Terraform binaries.

    Verify installation using `terraform --version`.

    installationsetup

    Initialize a Terraform Project

    How to set up a new Terraform project.

    bash
    # Create a directory for your project
    mkdir my-terraform-project
    cd my-terraform-project
    
    # Initialize Terraform
    terraform init

    Run `terraform init` whenever you add new providers.

    Keep your project organized with a `main.tf` file.

    project setupinitialization

    Basic Configuration

    2 topics

    Define a Provider

    Specify the cloud provider for Terraform to use.

    hcl
    provider "aws" {
      region = "us-east-1"
    }

    Always specify the region to avoid unexpected behavior.

    Use environment variables for sensitive data like credentials.

    provideraws

    Create a Resource

    Define a resource to provision infrastructure.

    hcl
    resource "aws_instance" "example" {
      ami           = "ami-0c55b159cbfafe1f0"
      instance_type = "t2.micro"
    }

    Use descriptive names for resources.

    Check the AWS AMI IDs for your region.

    resourceaws

    State Management

    2 topics

    Understand Terraform State

    Learn how Terraform tracks infrastructure state.

    plaintext
    # Terraform state is stored in a file called 'terraform.tfstate'
    # Example content:
    {
      "version": 4,
      "resources": [
        {
          "type": "aws_instance",
          "name": "example",
          "instances": [
            {
              "attributes": {
                "id": "i-0abcd1234efgh5678"
              }
            }
          ]
        }
      ]
    }

    Never manually edit the state file.

    Use remote state for collaboration.

    statemanagement

    Remote State Configuration

    Set up remote state storage for collaboration.

    hcl
    terraform {
      backend "s3" {
        bucket         = "my-terraform-state"
        key            = "state/terraform.tfstate"
        region         = "us-east-1"
      }
    }

    Use versioning on your S3 bucket for state backups.

    Secure your state file with encryption.

    remote states3

    Variables and Outputs

    2 topics

    Define Variables

    Use variables to make configurations reusable.

    hcl
    variable "instance_type" {
      default = "t2.micro"
    }
    
    resource "aws_instance" "example" {
      ami           = "ami-0c55b159cbfafe1f0"
      instance_type = var.instance_type
    }

    Use `default` for optional variables.

    Group variables in a `variables.tf` file.

    variablesconfiguration

    Define Outputs

    Expose useful information after applying Terraform.

    hcl
    output "instance_id" {
      value = aws_instance.example.id
    }

    Use outputs for debugging and integration.

    Group outputs in an `outputs.tf` file.

    outputsdebugging

    Advanced Topics

    2 topics

    Modules

    Organize Terraform configurations using modules.

    hcl
    module "ec2_instance" {
      source       = "./modules/ec2"
      instance_type = "t2.micro"
      ami           = "ami-0c55b159cbfafe1f0"
    }

    Use modules for reusable configurations.

    Store modules in a `modules` directory.

    modulesorganization

    Terraform Workspaces

    Manage multiple environments using workspaces.

    bash
    # Create a new workspace
    terraform workspace new dev
    
    # Switch to an existing workspace
    terraform workspace select dev

    Use workspaces for staging and production environments.

    Workspace names should be descriptive.

    workspacesenvironments

    Related Articles

    Background reading and deeper explanations for this sheet.

    How to Create an Agent Using LangGraph: A Practical Developer Cheat Sheet

    Want to build reliable AI agents instead of brittle prompt chains? This practical guide shows you how to create an agent using LangGraph step by step, from architecture and state design to tools, memory, and production hardening. You’ll get beginner-friendly explanations, real-world examples, and runnable code patterns you can adapt immediately.

    keyword overlap

    System Design Cheat Sheet: Practical Guide for Scalable Architecture

    This system design cheat sheet gives you a practical, beginner-friendly framework to design scalable, reliable applications without getting lost in theory. You’ll learn what to evaluate, why each component matters, and how to make trade-offs with real-world examples you can use in interviews and production.

    keyword overlap

    FastAPI Interview Questions: Practical Cheat Sheet for Developers

    Preparing for FastAPI interviews can feel overwhelming if you only memorize definitions. This practical cheat sheet helps you answer common FastAPI interview questions with confidence using real-world examples, production-ready patterns, and beginner-friendly explanations with depth.

    keyword overlap

    Generative AI Roadmap: A Practical, Beginner-Friendly Guide to Learning and Building Real Projects

    Generative AI can feel overwhelming, but you don’t need a PhD to get started and create useful applications. This roadmap breaks down what to learn, when to learn it, and how to apply each skill in real-world projects—from prompt engineering and APIs to RAG, evaluation, and production deployment.

    keyword overlap