Comments
Sign in to join the conversation
In the age of cloud computing, managing infrastructure manually (ClickOps) is no longer scalable or reliable. Infrastructure as Code (IaC) has emerged as the standard for provisioning and managing IT infrastructure through machine-readable definition files.
Terraform, developed by HashiCorp, is an open-source tool that allows you to define both cloud and on-prem resources in human-readable configuration files that you can version, reuse, and share. It uses the HashiCorp Configuration Language (HCL).
The core workflow consists of three steps:
You define resources in .tf files. Here is a more complex example involving variables and outputs.
main.tf
provider "aws" {
region = var.region
}
resource "aws_s3_bucket" "example_bucket" {
bucket = "my-unique-bucket-name-${var.environment}"
acl = "private"
tags = {
Name = "My bucket"
Environment = var.environment
}
}
variables.tf
variable "region" {
description = "AWS region"
default = "us-west-2"
}
variable "environment" {
description = "Deployment environment (dev, staging, prod)"
type = string
}
Run terraform plan. Terraform creates an execution plan describing what it will do to reach the desired state. It checks the current state vs. the configuration and calculates the delta.
Run terraform apply. Terraform executes the plan, making the API calls to the provider to create, update, or destroy resources.
In a team environment, you cannot store terraform.tfstate on your local laptop. You must use a remote backend like S3 with DynamoDB for state locking.
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "global/s3/terraform.tfstate"
region = "us-east-1"
dynamodb_table = "terraform_locks"
encrypt = true
}
}
Terraform enables you to treat your infrastructure with the same rigor as your application code: versioned, reviewed, and automated.