0
0
TerraformConceptBeginner · 3 min read

What is Infrastructure as Code in Terraform: Simple Explanation

Infrastructure as Code (IaC) in Terraform means writing simple text files to define and manage cloud resources automatically. Instead of clicking buttons, you describe your infrastructure in code, and Terraform builds or changes it for you.
⚙️

How It Works

Think of Terraform as a recipe book for your cloud setup. You write down what ingredients (servers, databases, networks) you want and how they should be arranged. This recipe is a plain text file using Terraform's language.

When you run Terraform, it reads your recipe and talks to your cloud provider to create or update the resources exactly as you described. If you change the recipe, Terraform figures out what needs to be added, removed, or changed, and does it for you.

This approach helps avoid mistakes from manual setup and makes your infrastructure easy to share, repeat, and track over time.

💻

Example

This example creates a simple virtual server on AWS using Terraform code. It shows how you describe the server and its settings in a file.

terraform
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = ">= 4.0"
    }
  }
  required_version = ">= 1.0"
}

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

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"  # Amazon Linux 2 AMI
  instance_type = "t2.micro"

  tags = {
    Name = "ExampleServer"
  }
}
Output
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
🎯

When to Use

Use Terraform's infrastructure as code when you want to manage cloud resources reliably and repeatedly. It is great for:

  • Setting up new environments quickly without manual steps.
  • Keeping infrastructure consistent across teams or projects.
  • Tracking changes over time with version control.
  • Automating updates and scaling your infrastructure safely.

For example, a company launching a website can use Terraform to create servers, databases, and networks with one command, then update or destroy them easily as needed.

Key Points

  • Terraform uses code files to define cloud infrastructure.
  • This code is easy to share, review, and reuse.
  • Terraform automates creating, updating, and deleting resources.
  • Infrastructure as code reduces manual errors and speeds up deployments.

Key Takeaways

Terraform lets you manage cloud resources by writing code instead of manual steps.
Infrastructure as code makes setups repeatable, consistent, and easy to track.
Terraform reads your code and automatically creates or updates your infrastructure.
Use Terraform to automate cloud resource management and reduce errors.