Infrastructure as Code: Terraform Patterns for Data Platforms

Infrastructure as Code: Terraform Patterns for Data Platforms

Why Terraform for Data Infrastructure?

Data infrastructure is uniquely complex. You're managing compute clusters, storage systems, networking, IAM policies, monitoring, and orchestration, all of which need to work together seamlessly. Manual configuration leads to drift, undocumented changes, and environment inconsistencies that cause production incidents.

Terraform treats infrastructure as code, giving you version control, peer review, automated testing, and reproducible deployments across environments. For data platforms specifically, Terraform's declarative approach ensures your infrastructure always matches your desired state.

Project Structure for Data Platforms

Organize your Terraform code into reusable modules that map to data platform components:

terraform/
├── modules/
│   ├── networking/          # VPC, subnets, security groups
│   ├── eks/                 # Kubernetes cluster
│   ├── rds/                 # PostgreSQL, MySQL
│   ├── s3/                  # Data lake buckets
│   ├── glue/                # Data catalog, crawlers
│   ├── emr/                 # Spark clusters
│   ├── iam/                 # Roles, policies
│   └── monitoring/          # CloudWatch, Grafana
├── environments/
│   ├── dev/
│   │   ├── main.tf
│   │   ├── variables.tf
│   │   └── terraform.tfvars
│   ├── staging/
│   │   ├── main.tf
│   │   ├── variables.tf
│   │   └── terraform.tfvars
│   └── prod/
│       ├── main.tf
│       ├── variables.tf
│       └── terraform.tfvars
├── shared/                  # Shared services (DNS, accounts)
└── ci/                      # Pipeline configurations

Module: S3 Data Lake

A production-grade S3 data lake with proper lifecycle policies, encryption, and access controls:

# modules/s3/main.tf
resource "aws_s3_bucket" "data_lake" {
  bucket = var.bucket_name
  
  tags = merge(var.common_tags, {
    DataClassification = var.classification
    Environment       = var.environment
  })
}

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

resource "aws_s3_bucket_server_side_encryption_configuration" "data_lake" {
  bucket = aws_s3_bucket.data_lake.id
  rule {
    apply_server_side_encryption_by_default {
      sse_algorithm     = "aws:kms"
      kms_master_key_id = var.kms_key_arn
    }
    bucket_key_enabled = true
  }
}

resource "aws_s3_bucket_lifecycle_configuration" "data_lake" {
  bucket = aws_s3_bucket.data_lake.id
  
  rule {
    id     = "transition-to-ia"
    status = "Enabled"
    transition {
      days          = 90
      storage_class = "STANDARD_IA"
    }
    transition {
      days          = 365
      storage_class = "GLACIER"
    }
    expiration {
      days = 2555  # 7 years retention
    }
  }
  
  rule {
    id     = "cleanup-incomplete-uploads"
    status = "Enabled"
    abort_incomplete_multipart_upload {
      days_after_initiation = 7
    }
  }
}

Managing State at Scale

For enterprise data platforms with multiple teams, proper state management is critical:

# Backend configuration for remote state
terraform {
  backend "s3" {
    bucket         = "company-terraform-state"
    key            = "data-platform/prod/terraform.tfstate"
    region         = "us-east-1"
    dynamodb_table = "terraform-locks"
    encrypt        = true
    kms_key_id     = "alias/terraform-state"
  }
}

# State isolation per environment
# Use workspaces or separate state files
locals {
  state_key = "${var.environment}/terraform.tfstate"
}

CI/CD Integration

Automate Terraform workflows with GitHub Actions for plan, review, and apply:

# .github/workflows/terraform.yml
name: Terraform
on:
  pull_request:
    paths: ['terraform/**']
  push:
    branches: [main]
    paths: ['terraform/**']

jobs:
  terraform:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: hashicorp/setup-terraform@v3
        with:
          terraform_version: "1.7.0"
      
      - name: Terraform Init
        run: terraform init
        working-directory: terraform/environments/prod
      
      - name: Terraform Plan
        run: terraform plan -out=tfplan
        working-directory: terraform/environments/prod
      
      - name: Terraform Apply
        if: github.ref == 'refs/heads/main'
        run: terraform apply -auto-approve tfplan
        working-directory: terraform/environments/prod

Conclusion

Infrastructure as Code is not optional for modern data platforms. It's essential. Terraform provides the tooling, ecosystem, and patterns to manage complex data infrastructure at scale. By investing in modular, well-tested Terraform code, you gain the ability to spin up entire data platforms in minutes, replicate environments for testing, and maintain compliance through auditable infrastructure changes.

Share this article

Back to Blog See Projects