CSCv0.10rz
GitHub

Terraform Basics Cheatsheet

Essential Terraform commands, concepts, and configuration snippets for infrastructure as code.

Core Workflow Commands

These are the most common commands you will use in your day-to-day Terraform workflow.

  • terraform init: Initializes a working directory containing Terraform configuration files. This is the first command that should be run after writing a new Terraform configuration.
  • terraform plan: Creates an execution plan. Terraform performs a refresh, unless explicitly disabled, and then determines what actions are necessary to achieve the desired state specified in the configuration files.
  • terraform apply: Executes the actions proposed in a Terraform plan to create, update, or destroy infrastructure.
  • terraform destroy: Destroys all remote objects managed by a particular Terraform configuration.
Provider Configuration

Providers are plugins that Terraform uses to interact with cloud providers, SaaS providers, and other APIs.

# main.tf
 
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}
 
provider "aws" {
  region = "us-west-2"
}
Resource Block

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.

resource "aws_instance" "web_server" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
 
  tags = {
    Name = "HelloWorld"
  }
}