Learning Terraform: Notes from a Beginner

Table of Contents


Introduction

What is Infrastructure as Code (IaC)?

Infrastructure as Code means managing cloud or server resources using code instead of clicking around in a web console. It helps automate setups and makes infrastructure easier to repeat and maintain.

What is Terraform?

Terraform is an open-source IaC tool developed by HashiCorp. It allows infrastructure to be defined and managed using configuration files. Terraform supports various providers such as AWS, Azure, and Google Cloud.

Advantages of Terraform

  • Supports multiple cloud providers with the same workflow and syntax
  • Uses a simple and readable configuration language (HCL) for faster setup and easier maintenance
  • Maintains a state file to track infrastructure changes and ensure accurate updates
  • Works well with version control systems for team collaboration, history tracking, and safe rollbacks

Deploying with Terraform

A typical workflow includes:

  • Writing configuration files
  • Initializing the project
  • Planning changes
  • Applying changes to deploy infrastructure

Build Infrastructure

Write Configuration

Terraform uses .tf files to define infrastructure. A basic configuration usually includes:

Terraform block

Defines the required Terraform version and the providers to use. Providers are fetched from the Terraform Registry, which hosts official and community-supported integrations (e.g., AWS, Azure, GCP).

Provider block

Specifies which cloud or service provider to connect to (e.g., AWS), along with any required configuration such as region.

Resource block

Describes the actual infrastructure to create, such as virtual machines, storage, or networking components. A resource block includes:

  • Resource type – The kind of resource (e.g., aws_instance, aws_s3_bucket)
  • Resource name – A unique name used within the Terraform configuration to refer to the resource

Example

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.16"
    }
  }

  required_version = ">= 1.2.0"
}

provider "aws" {
  region  = "us-west-2"
}

resource "aws_instance" "app_server" {
  ami           = "ami-830c94e3"
  instance_type = "t2.micro"

  tags = {
    Name = "ExampleAppServerInstance"
  }
}

This configuration sets up an AWS EC2 instance in the us-west-2 region using a specific AMI and instance type.

Initialize

  • Run terraform init to prepare the working directory and download necessary provider plugins.

Format and Validate

  • terraform fmt formats configuration files according to standard style
  • terraform validate checks the configuration for syntax errors or issues

Create Infrastructure

  • Use terraform plan to preview what changes will be made to the infrastructure.
  • Run terraform apply to apply those changes and provision the resources.

Inspect State

Terraform stores the current infrastructure state in a file called terraform.tfstate. This file allows Terraform to track which resources have been created and what their current configuration is.


References

Install Terraform | Terraform | HashiCorp Developer
Install Terraform on Mac, Linux, or Windows by downloading the binary or using a package manager (Homebrew or Chocolatey). Then create a Docker container locally by following a quick-start tutorial to check that Terraform installed correctly.
Create infrastructure | Terraform | HashiCorp Developer
Authenticate to AWS and create infrastructure with Terraform. Write and validate Terraform configuration, initialize a configuration directory, and plan and apply a configuration to create infrastructure.
Manage infrastructure | Terraform | HashiCorp Developer
Update your infrastructure configuration and apply changes with Terraform. Add variables and output values to your configuration. Use a module to reuse configuration.
Destroy infrastructure | Terraform | HashiCorp Developer
Destroy AWS infrastructure managed by Terraform. Remove a resource from your configuration. Run and apply a destroy plan to remove all of your workspace’s infrastructure.