Dung (Donny) Nguyen

Senior Software Engineer

Infrastructure as Code (IaC)

Infrastructure as Code (IaC) is the practice of provisioning and managing infrastructure—servers, networks, databases, load balancers, and more—through machine-readable configuration files rather than manual processes or interactive tools. Instead of clicking through a cloud console, you describe the desired state of your infrastructure in code, then let a tool create, update, or destroy resources to match that description.

Why IaC Matters

Manually configuring infrastructure is slow, error-prone, and hard to reproduce. IaC solves these problems by treating infrastructure the same way we treat application source code:

Declarative vs. Imperative Approaches

There are two main styles of IaC:

Declarative tools are generally preferred because they let the tool handle dependency ordering, detect drift, and reconcile the current state with the desired state.

Key Concepts

  1. Desired State: The target configuration you define in code.
  2. State Management: Many tools (like Terraform) keep a state file that maps your configuration to real-world resources, enabling them to detect changes and plan updates.
  3. Idempotency: Applying the same configuration multiple times yields the same result without unintended side effects.
  4. Drift Detection: Identifying when the real infrastructure has diverged from the code (for example, due to a manual change in the console).
  5. Modules / Reusability: Packaging configuration into reusable components to avoid duplication across environments and projects.

Common IaC Tools

Tool Provider Language / Format Notes
Terraform HashiCorp HCL Cloud-agnostic, huge provider ecosystem, declarative.
AWS CloudFormation AWS YAML / JSON Native AWS service, tight integration with AWS.
AWS CDK AWS TypeScript, Python, Java, etc. Define infrastructure using familiar programming languages; synthesizes to CloudFormation.
Pulumi Pulumi TypeScript, Python, Go, etc. Multi-cloud, uses general-purpose languages.
Ansible Red Hat YAML Configuration management and provisioning, often imperative.
Bicep Microsoft Bicep DSL Simplified language for Azure Resource Manager.

Example: Terraform

A simple Terraform configuration that provisions an AWS S3 bucket:

provider "aws" {
  region = "us-east-1"
}

resource "aws_s3_bucket" "app_bucket" {
  bucket = "my-application-bucket"

  tags = {
    Environment = "Production"
    ManagedBy   = "Terraform"
  }
}

The typical workflow is:

terraform init      # Download providers and set up the working directory
terraform plan      # Preview the changes without applying them
terraform apply     # Create or update resources to match the configuration
terraform destroy   # Tear down all managed resources

Example: AWS CloudFormation

The same S3 bucket described in CloudFormation YAML:

Resources:
  AppBucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: my-application-bucket
      Tags:
        - Key: Environment
          Value: Production
        - Key: ManagedBy
          Value: CloudFormation

Best Practices

Conclusion

Infrastructure as Code transforms infrastructure management from a manual, error-prone chore into a repeatable, automated, and collaborative engineering discipline. By defining infrastructure declaratively, versioning it, and integrating it into automated pipelines, teams gain reliability, speed, and confidence when building and scaling modern cloud systems.