Infrastructure as Code: Reproducible Environments

Why console-built infrastructure drifts, what infrastructure as code gives you, separating infrastructure, configuration and secrets, environment isolation and drift detection.

  • Advanced
  • About 16 minutes
  • Includes an exercise and a knowledge check

The problem Infrastructure built by hand

Apex needs compute, a relational database, settlement file storage, messaging, network isolation, secrets, monitoring and backups. Clicking each one together in the console works once. Then environments drift apart, changes cannot be reviewed, nothing can be recreated reliably, mistakes cannot be traced, and nobody holds a full record of what was intended.

A loop: write the definition in Git, plan what will change, review it like application code, apply it through the pipeline, and the deployed environment matches the code. A dashed red arrow marks a manual console change. Drift detection compares the deployed environment with the code and sends every difference back to be fixed in code. Below, development, test, staging and production, each with its own account, configuration and secrets, built from the same definitions.
The code is the source of truth. A manual change is drift until it is back in the code.

Infrastructure as code (IaC) describes infrastructure in machine-readable files that are reviewed, tested, versioned and applied the same way every time. AWS CloudFormation, the AWS CDK and Terraform are common choices; pick one and use it consistently. The skill is understanding the infrastructure model and its consequences, not memorizing syntax.

hclA database that is private, encrypted and backed up (Terraform excerpt)

resource "aws_db_instance" "reconciliation" {
  identifier              = "apex-reconciliation-${var.environment}"
  engine                  = "postgres"
  instance_class          = var.db_instance_class
  db_subnet_group_name    = aws_db_subnet_group.isolated.name
  vpc_security_group_ids  = [aws_security_group.db.id]
  publicly_accessible     = false
  storage_encrypted       = true
  kms_key_id              = aws_kms_key.data.arn
  backup_retention_period = var.backup_retention_days
  deletion_protection     = true
  manage_master_user_password = true   # the password lives in Secrets Manager, not in this file
}

Three kinds of setting Infrastructure, configuration and secrets

KindExamplesWhere it lives
Infrastructure definitionNetwork and subnets, compute, database, buckets, queues, IAM roles, security groups, monitoringIaC repository, reviewed
Environment configurationConnection parameters, feature flags, endpoints, log levels, processing limitsPer-environment configuration
SecretsDatabase credentials, provider credentials, signing keys, API tokensSecrets Manager, never Git or images

Isolation Separate environments, separate accounts

Development, test, staging and production should be separated by accounts or equally strong boundaries, matching the customer’s security model. Different names inside one account do not isolate anything: one over-broad role can still reach production.

The file that knows everything Terraform state: secure, locked and separate

Terraform records what it built in a state file, and that file can contain secrets in plain text, such as generated database passwords. Treat it as sensitive data, and set up four things before the first production apply:

PracticeHow
Secure remote stateAn S3 bucket with versioning, encryption with a KMS key, all public access blocked, and access limited to the pipeline role and a few named administrators. Never on a laptop
LockingOnly one apply at a time: S3 native locking (use_lockfile, Terraform 1.10 and later) or, on older versions, a DynamoDB lock table
One state per environmentSeparate state for development, staging and production, each in its own account, so a mistake in one cannot touch another
Protected production plansThe pipeline creates the plan and saves it; a reviewer approves that exact plan; the apply uses the saved plan file, so nothing changes between review and apply. No production apply from a laptop

Stay honest Infrastructure drift

An engineer opens a security group in the console to fix a connectivity problem and never updates the code. Months later the environment is recreated from code and the fix disappears, or worse, the open rule stays forever unreviewed. Run drift detection on a schedule (a plan against production is enough) and treat every difference as a change to review.

Practice Define the Apex infrastructure

Practical exercise

Exercise 02: infrastructure for the platform

Document or provision the Apex infrastructure and show a reviewed plan.

  1. VPC and subnet strategy, application runtime and relational database.
  2. Settlement file storage, queue and IAM execution roles.
  3. Secrets management, logging, monitoring and backups.
  4. Environment separation and a drift detection strategy.
Show a sample drift policy
  • Daily: the pipeline runs a plan against production; any difference opens a ticket for the platform owner.
  • Emergency console change: allowed only in an incident, recorded, and back in code within two working days.

2 questions

Check your understanding

1. Why review infrastructure code like application code?
2. What is infrastructure drift?
Answer every question to submit.

What you can do now

  • Define infrastructure as reviewed code.
  • Separate infrastructure, configuration and secrets.
  • Isolate environments and detect drift.