Skip to main content
CloudAdvanced18 min readUpdated March 2025

Infrastructure as Code with Terraform

Define cloud infrastructure in version-controlled code. Learn the Terraform workflow, state, variables and how to structure repeatable deployments.

Why Infrastructure as Code?

Clicking through a console does not scale and cannot be reviewed or reproduced. Infrastructure as Code (IaC) describes resources in files you can store in Git, review in pull requests and apply consistently across environments.

Terraform is declarative: you describe the desired end state, and it works out the steps needed to reach it.

A Minimal Configuration

This configuration creates an S3 bucket with versioning enabled. Variables keep it reusable across environments:

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

provider "aws" {
  region = var.region
}

variable "region" {
  type    = string
  default = "us-east-1"
}

variable "bucket_name" {
  type = string
}

resource "aws_s3_bucket" "assets" {
  bucket = var.bucket_name
}

resource "aws_s3_bucket_versioning" "assets" {
  bucket = aws_s3_bucket.assets.id
  versioning_configuration {
    status = "Enabled"
  }
}

output "bucket_arn" {
  value = aws_s3_bucket.assets.arn
}

The Core Workflow

Terraform follows a predictable cycle:

bash
terraform init                      # download providers
terraform fmt                       # format code
terraform validate                  # check syntax
terraform plan -var="bucket_name=my-assets-bucket"   # preview changes
terraform apply -var="bucket_name=my-assets-bucket"  # create resources
terraform destroy -var="bucket_name=my-assets-bucket" # tear down

State and Collaboration

Terraform records what it manages in a state file. Never edit it by hand and never commit it to Git, because it can contain secrets.

For teams, store state in a remote backend (such as S3 with DynamoDB locking) so everyone shares one source of truth and two people cannot apply at the same time.

Best Practices

Keep your infrastructure code maintainable:

  • Split reusable pieces into modules.
  • Use separate state per environment (dev, staging, prod).
  • Always run `plan` and review it before `apply`.
  • Pin provider versions for predictable builds.
  • Run `plan` automatically in CI on every pull request.

Key Takeaways

  • IaC makes infrastructure reviewable, repeatable and version-controlled.
  • Terraform is declarative: describe the end state, review the plan, then apply.
  • Use remote state with locking for team workflows.
  • Modules and pinned versions keep configurations maintainable.

Contact Us

Have a question or feedback? Fill out the form below or reach us directly at support@nvaitraining.com