ESC
Type to search guides, tutorials, and reference documentation.
Verified by Garnet Grid

Infrastructure Automation with Terraform

Automate infrastructure provisioning and management with Terraform. Covers module design, state management, CI/CD integration, drift detection, and production-grade patterns.

Infrastructure automation replaces clickops with code — every server, database, network, and permission is defined in version-controlled configuration files. Terraform is the dominant tool for this pattern, but the principles apply regardless of tool: declarative definitions, state management, modular composition, and automated validation.


Why Terraform

FactorTerraformCloudFormationPulumiBicep
Multi-cloud✅ Yes❌ AWS only✅ Yes❌ Azure only
LanguageHCLYAML/JSONTypeScript/Python/GoDSL
State managementExternal (S3, TFC)Managed by AWSExternal or managedManaged by Azure
CommunityLargestLarge (AWS)GrowingGrowing (Azure)
Learning curveModerateLow (AWS users)Low (developers)Low (Azure users)
Drift detectionterraform planDrift detectionpulumi previewWhat-if

Module Architecture

infrastructure/
├── modules/                    # Reusable modules
│   ├── vpc/
│   │   ├── main.tf
│   │   ├── variables.tf
│   │   └── outputs.tf
│   ├── eks-cluster/
│   ├── rds-database/
│   └── monitoring/
├── environments/               # Environment-specific configs
│   ├── dev/
│   │   ├── main.tf            # Composes modules
│   │   ├── terraform.tfvars   # Dev-specific values
│   │   └── backend.tf         # State backend config
│   ├── staging/
│   └── production/
├── global/                     # Shared resources (DNS, IAM)
│   ├── iam/
│   └── dns/
└── scripts/
    ├── plan.sh
    └── apply.sh

State Management

BackendBest ForTrade-offs
S3 + DynamoDBAWS-native teamsReliable, low cost, requires setup
Terraform CloudTeams wanting managed stateEasy, collaboration features, cost
Azure BlobAzure-native teamsNative integration
GCSGCP-native teamsNative integration
GitLab ManagedGitLab CI usersIntegrated, simple setup

State Locking

Without locking:
    Engineer A runs terraform apply
    Engineer B runs terraform apply simultaneously
    → State corruption, resources in unknown state

With locking (DynamoDB/TFC):
    Engineer A runs terraform apply → acquires lock
    Engineer B runs terraform apply → "State locked, try again later"
    Engineer A finishes → releases lock
    Engineer B retries → acquires lock → safe to apply

CI/CD Integration

Pull Request:
    → terraform fmt -check (formatting)
    → terraform validate (syntax)
    → terraform plan (preview changes)
    → Plan output posted as PR comment
    → Security scan (tfsec, checkov)

Merge to main:
    → terraform plan (confirm changes)
    → Manual approval gate
    → terraform apply (deploy)
    → Notify team of changes

Common Patterns

PatternWhen to UseDescription
Module compositionAlwaysBuild infrastructure from reusable modules
Remote state data sourcesCross-project referencesRead outputs from other state files
WorkspacesSame config, different environmentsDev/staging/prod from same code
ImportExisting resourcesBring clickops resources under Terraform
State migrationRestructuringMove resources between state files
Targeted applyEmergency changesApply specific resources only

Security Scanning

ToolWhat It ChecksIntegration
tfsecTerraform security misconfigsCLI, CI, pre-commit
CheckovMulti-framework securityCLI, CI
TerrascanPolicy violationsCLI, CI
OPA/ConftestCustom policiesCI
SentinelPolicy-as-code (TFC Enterprise)Terraform Cloud

Anti-Patterns

Anti-PatternProblemFix
Local state fileLost state = orphaned resourcesRemote backend with locking
Monolithic configSingle plan takes 30+ minutesSplit by domain/service with modules
Hardcoded valuesCan’t reuse across environmentsVariables and tfvars per environment
terraform apply without planUnexpected changes deployedAlways plan first, review, then apply
Manual changes alongside TerraformState drift, conflictsAll changes through Terraform; import existing resources
No state lockingConcurrent applies corrupt stateEnable DynamoDB/TFC locking

Checklist

  • Remote backend configured with state locking
  • Module structure: reusable modules + environment-specific composition
  • CI/CD pipeline: format → validate → plan → approve → apply
  • Security scanning integrated (tfsec or Checkov)
  • State backup and versioning enabled on backend
  • Variables used for all environment-specific values
  • Drift detection scheduled (weekly terraform plan comparison)
  • Resource tagging standard enforced (owner, environment, cost center)
  • Import plan for any existing clickops resources
  • Documentation: module README with inputs, outputs, and usage examples

:::note[Source] This guide is derived from operational intelligence at Garnet Grid Consulting. For infrastructure automation consulting, visit garnetgrid.com. :::

Jakub Dimitri Rezayev
Jakub Dimitri Rezayev
Founder & Chief Architect • Garnet Grid Consulting

Jakub holds an M.S. in Customer Intelligence & Analytics and a B.S. in Finance & Computer Science from Pace University. With deep expertise spanning D365 F&O, Azure, Power BI, and AI/ML systems, he architects enterprise solutions that bridge legacy systems and modern technology — and has led multi-million dollar ERP implementations for Fortune 500 supply chains.

View Full Profile →