Archive · DevOps

Terraform: Infrastructure as Code Basics

Write, plan and apply a small AWS resource with Terraform 1.2.

1 min read

  • Terraform 1.2
  • AWS provider 4.x
  • Region ap-south-1

Terraform describes infrastructure in .tf files and creates it through a plan, then apply cycle.

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

provider "aws" { region = "ap-south-1" }

resource "aws_s3_bucket" "assets" {
  bucket = "acme-assets-2022"
  tags   = { Project = "shop", Env = "dev" }
}

resource "aws_s3_bucket_versioning" "assets" {
  bucket = aws_s3_bucket.assets.id
  versioning_configuration { status = "Enabled" }
}
terraform init
terraform fmt && terraform validate
terraform plan -out tfplan
terraform apply tfplan
terraform destroy

In version 4 of the AWS provider, bucket settings such as versioning moved into separate resources, as shown. Always read the plan before applying, commit the .terraform.lock.hcl file, and never commit terraform.tfstate, which can contain secrets.