← All guides
Guide

Infrastructure as Code with Terraform on AWS: A Practical Guide for Small Businesses

Learn how to use Terraform to manage your AWS infrastructure as code. Step-by-step guide with real examples for small business teams.

If you're managing AWS resources by clicking through the console, you're probably spending too much time on repetitive tasks and worrying about configuration drift. Infrastructure as Code (IaC) with Terraform offers a better way: define your infrastructure in simple text files, version control everything, and deploy consistently every time.

This guide walks you through getting started with Terraform on AWS, written for small business teams who need practical results without the enterprise complexity.

What Is Infrastructure as Code?

Infrastructure as Code means managing your servers, networks, and cloud resources using configuration files instead of manual setup. Instead of clicking through AWS console screens to create an EC2 instance, you write a file that describes what you want, and Terraform builds it for you.

The benefits are straightforward:

  • Repeatability: Deploy identical environments for development, staging, and production
  • Version control: Track every infrastructure change in Git
  • Documentation: Your code becomes living documentation of your setup
  • Disaster recovery: Rebuild everything from scratch if needed
  • Team collaboration: Multiple people can work on infrastructure safely

Why Terraform for AWS?

AWS offers its own IaC tool called CloudFormation, so why use Terraform? For small businesses, Terraform has some practical advantages:

  • Plain language syntax: Terraform's HCL language is easier to read than CloudFormation's JSON or YAML
  • Multi-cloud capability: If you ever use Azure, Google Cloud, or other providers, the same tool works everywhere
  • Strong community: Thousands of ready-made modules and active forums for troubleshooting
  • State management: Terraform tracks what's deployed and only changes what needs updating

Setting Up Terraform for AWS

Prerequisites

Before you start, you'll need:

  • An AWS account with admin access
  • AWS CLI installed and configured
  • Terraform installed (download from terraform.io)
  • A text editor (VS Code works well with Terraform extensions)

Configure AWS Credentials

Terraform needs permission to create AWS resources. The simplest approach for getting started is using AWS CLI credentials:

bash aws configure

Enter your AWS Access Key ID and Secret Access Key when prompted. For production use, consider using IAM roles or more secure credential management through our security services.

Your First Terraform Configuration

Create a new directory for your project and add a file called main.tf:

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

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

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

tags = { Name = "terraform-example" } }

This configuration tells Terraform to create a single EC2 instance. Let's break it down:

  • The terraform block specifies which providers you're using
  • The provider block configures AWS settings like region
  • The resource block defines what to create (an EC2 instance in this case)

Working with Terraform: The Basic Workflow

Initialize Your Project

Run this command in your project directory:

bash terraform init

This downloads the AWS provider plugin and sets up your working directory. You only need to do this once per project (or when you add new providers).

Preview Changes

Before creating anything, see what Terraform plans to do:

bash terraform plan

Terraform shows you exactly what will be created, modified, or destroyed. This preview is invaluable for catching mistakes before they happen.

Apply Your Configuration

When you're ready to create the resources:

bash terraform apply

Terraform will show the plan again and ask for confirmation. Type yes to proceed. In a minute or two, your EC2 instance will be running.

Make Changes

Edit your main.tf file to change something (like the instance type or add tags), then run terraform plan and terraform apply again. Terraform figures out what needs to change and updates only those resources.

Clean Up

When you're done testing:

bash terraform destroy

This removes all resources Terraform created. Always destroy test resources to avoid unnecessary AWS charges.

Practical Example: A Small Business Web Application

Here's a more realistic example that creates a simple web application infrastructure:

hcl resource "aws_vpc" "main" { cidr_block = "10.0.0.0/16"

tags = { Name = "main-vpc" } }

resource "aws_subnet" "public" { vpc_id = aws_vpc.main.id cidr_block = "10.0.1.0/24"

tags = { Name = "public-subnet" } }

resource "aws_security_group" "web" { name = "web-sg" description = "Allow web traffic" vpc_id = aws_vpc.main.id

ingress { from_port = 80 to_port = 80 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] }

egress { from_port = 0 to_port = 0 protocol = "-1" cidr_blocks = ["0.0.0.0/0"] } }

This creates a VPC, subnet, and security group. Notice how resources reference each other using aws_vpc.main.id - Terraform handles the dependencies automatically.

Best Practices for Small Business Teams

Use Version Control

Store your Terraform files in Git from day one. This gives you history, backup, and collaboration capabilities. Add a .gitignore file with:

.terraform/ *.tfstate *.tfstate.backup .terraform.lock.hcl

Never commit state files or credentials to Git.

Organize with Modules

As your infrastructure grows, break configurations into reusable modules. Create a modules/ directory with subdirectories for common patterns like web-server or database.

Use Variables

Don't hardcode values. Use variables for things that change between environments:

hcl variable "environment" { description = "Environment name" type = string default = "development" }

variable "instance_type" { description = "EC2 instance type" type = string default = "t2.micro" }

Remote State Storage

For team collaboration, store Terraform state in S3 instead of locally:

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

This prevents conflicts when multiple people work on infrastructure.

Common Pitfalls to Avoid

Don't modify resources manually: If you create something with Terraform, always modify it through Terraform. Manual changes cause state drift and confusion.

Watch your costs: Terraform makes it easy to spin up resources, but they all cost money. Always destroy test environments when you're done.

Start small: Don't try to Terraform your entire infrastructure at once. Start with one application or service, learn the patterns, then expand.

Test in development first: Always test changes in a development environment before applying to production.

Getting Help with Your AWS Infrastructure

Terraform is powerful, but it has a learning curve. If you're looking to modernize your infrastructure but don't have time to become a Terraform expert, our cloud services team can help you design, implement, and manage infrastructure as code tailored to your business needs.

Contact us to discuss how we can help you build reliable, repeatable AWS infrastructure without the complexity.

FAQ

How much does Terraform cost?

Terraform itself is free and open source. You only pay for the AWS resources you create. Terraform Cloud offers paid features for teams, but most small businesses do fine with the free version.

Can I use Terraform with existing AWS resources?

Yes. Terraform can import existing resources into its state management. Use the terraform import command for each resource. It takes some work to import a large existing infrastructure, but it's definitely possible.

What happens if I lose my Terraform state file?

The state file is critical - it's how Terraform knows what resources it manages. If you lose it, Terraform can't manage those resources anymore. This is why storing state in S3 with versioning enabled is important for anything beyond personal testing. You can rebuild state by importing resources, but it's tedious.

Need a hand with this?

O-Cyrus helps small businesses with websites, DNS, custom apps, and the everyday tech that keeps things running.

Talk to us →