Skip to content

Infrastructure as Code with Terraform

Medium ~50 min

What you'll build

A Terraform configuration that provisions a VPC, subnet, security group, and an EC2 instance on AWS — all defined as code and reproducible.

Goals

  • Write Terraform configuration files
  • Define AWS resources: VPC, subnet, EC2
  • Use variables and outputs
  • Practice the plan-apply-destroy workflow

Prerequisites

  • Cloud computing basics
  • AWS account (or any cloud provider)
  • Understanding of infrastructure concepts

Steps

  1. 1

    Set up the provider and variables

    Create a main.tf with the AWS provider and variables for region and instance type. This makes the configuration reusable.

    Code
    # main.tf
    terraform {
      required_providers {
        aws = {
          source  = "hashicorp/aws"
          version = "~> 5.0"
        }
      }
    }
    
    provider "aws" {
      region = var.region
    }
    
    variable "region" {
      default = "us-east-1"
    }
    
    variable "instance_type" {
      default = "t3.micro"
    }

    Expected result

    Terraform is configured to use AWS in us-east-1 with a t3.micro instance type.

  2. 2

    Define a VPC and subnet

    Create a VPC with a public subnet. This is the network foundation for the EC2 instance.

    Code
    # Add to main.tf
    resource "aws_vpc" "main" {
      cidr_block           = "10.0.0.0/16"
      enable_dns_hostnames = true
      tags = { Name = "terraform-vpc" }
    }
    
    resource "aws_subnet" "public" {
      vpc_id                  = aws_vpc.main.id
      cidr_block              = "10.0.1.0/24"
      map_public_ip_on_launch = true
      tags = { Name = "terraform-subnet" }
    }

    Expected result

    A VPC and public subnet are defined in code.

  3. 3

    Add a security group and EC2 instance

    Create a security group allowing SSH and HTTP, then launch an EC2 instance in the subnet. Acceptance: terraform plan shows 3 resources to create.

  4. 4

    Add outputs

    Define outputs for the instance's public IP and the VPC ID so they're visible after apply. Acceptance: terraform output shows the public IP after apply.

  5. 5

    Run the plan-apply-destroy workflow

    Initialize Terraform, plan the changes, apply to create resources, verify the instance is running, then destroy to clean up. This is the core Terraform workflow.

    Code
    # Initialize (downloads provider plugins)
    terraform init
    
    # Preview the changes
    terraform plan
    
    # Apply the changes (creates resources)
    terraform apply
    
    # Verify the instance is running
    aws ec2 describe-instances --filters Name=tag:Name,Values=terraform-web
    
    # Destroy everything (cleans up)
    terraform destroy

    Expected result

    terraform apply creates the VPC, subnet, security group, and EC2 instance; destroy removes them all.

Try it yourself

What you learned

You provisioned cloud infrastructure with Terraform — defining resources as code, managing state, and using the plan-apply-destroy workflow. IaC makes infrastructure reproducible, versionable, and reviewable.

Related Resources

Related Glossary

Related Glossary

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to topic