Skip to main content
โšก Calmops

Infrastructure as Code: Terraform with Go

Infrastructure as Code: Terraform with Go

Introduction

Terraform enables defining infrastructure as code. This guide covers provisioning cloud resources, managing state, and best practices for IaC.

Core Concepts

Terraform Basics

  • Resources: Cloud infrastructure components
  • Variables: Input parameters
  • Outputs: Return values
  • State: Current infrastructure state
  • Modules: Reusable configurations

Good: Terraform Configuration

Basic Configuration

# โœ… GOOD: Terraform configuration
terraform {
  required_version = ">= 1.0"
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}

provider "aws" {
  region = var.aws_region
}

# โœ… GOOD: Variables
variable "aws_region" {
  description = "AWS region"
  type        = string
  default     = "us-east-1"
}

variable "app_name" {
  description = "Application name"
  type        = string
}

variable "environment" {
  description = "Environment"
  type        = string
  validation {
    condition     = contains(["dev", "staging", "prod"], var.environment)
    error_message = "Environment must be dev, staging, or prod."
  }
}

# โœ… GOOD: Resources
resource "aws_instance" "app" {
  ami           = data.aws_ami.ubuntu.id
  instance_type = "t3.micro"

  tags = {
    Name        = var.app_name
    Environment = var.environment
  }
}

# โœ… GOOD: Data sources
data "aws_ami" "ubuntu" {
  most_recent = true
  owners      = ["099720109477"]

  filter {
    name   = "name"
    values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"]
  }
}

# โœ… GOOD: Outputs
output "instance_ip" {
  description = "Instance IP address"
  value       = aws_instance.app.public_ip
}

Modules

# โœ… GOOD: Module usage
module "vpc" {
  source = "./modules/vpc"

  cidr_block = "10.0.0.0/16"
  environment = var.environment
}

module "app" {
  source = "./modules/app"

  vpc_id = module.vpc.vpc_id
  app_name = var.app_name
}

Good: Terraform Commands

Common Commands

# โœ… GOOD: Initialize Terraform
terraform init

# โœ… GOOD: Validate configuration
terraform validate

# โœ… GOOD: Format configuration
terraform fmt -recursive

# โœ… GOOD: Plan changes
terraform plan -out=tfplan

# โœ… GOOD: Apply changes
terraform apply tfplan

# โœ… GOOD: Destroy resources
terraform destroy

# โœ… GOOD: Show state
terraform show

# โœ… GOOD: List resources
terraform state list

Advanced Patterns

Remote State

# โœ… GOOD: Remote state configuration
terraform {
  backend "s3" {
    bucket         = "terraform-state"
    key            = "prod/terraform.tfstate"
    region         = "us-east-1"
    encrypt        = true
    dynamodb_table = "terraform-locks"
  }
}

Workspaces

# โœ… GOOD: Workspace management
terraform workspace new prod
terraform workspace select prod
terraform apply

Best Practices

1. Use Variables

# โœ… GOOD: Use variables
variable "instance_type" {
  type = string
}

resource "aws_instance" "app" {
  instance_type = var.instance_type
}

# โŒ BAD: Hardcoded values
resource "aws_instance" "app" {
  instance_type = "t3.micro"
}

2. Use Modules

# โœ… GOOD: Modular structure
module "vpc" { ... }
module "app" { ... }

# โŒ BAD: Monolithic configuration
resource "aws_vpc" { ... }
resource "aws_instance" { ... }

3. Use Remote State

# โœ… GOOD: Remote state
backend "s3" { ... }

# โŒ BAD: Local state
# State stored locally

Resources

Summary

Terraform enables managing infrastructure as code. Use modules for reusability, remote state for collaboration, and variables for flexibility. Proper IaC practices ensure reproducible, maintainable infrastructure.

Comments