Infrastructure as Code (IaC)


Infrastructure as Code (IaC)

What Is IaC?

Infrastructure as Code (IaC) is the practice of defining and provisioning infrastructure (such as servers, storage, networking) using code. It can be done in two main styles:

  • Declarative: Describe what the infrastructure should look like.
  • Imperative: Write how to build it using real code.

Common Categories and Tools

CategoryToolsLanguage UsedPurpose
Declarative IaCTerraform, CloudFormationHCL, YAML, JSONDefine infrastructure as a desired state
Imperative IaCAWS CDK, PulumiTypeScript, Python, etc.Define and deploy infrastructure using real programming
Container Definition (not IaC itself)Dockerfile, docker-composeDocker syntax, YAMLDefine app containers and services layout
Configuration ManagementAnsible, Chef, PuppetYAML, RubySet up OS-level configurations

Declarative vs. Imperative IaC

AspectDeclarativeImperative
You write...What you wantHow to achieve it
ToolsTerraform, CloudFormationAWS CDK, Pulumi
StyleConfigurationProgramming code
Analogy"Build a 3 BHK house with garden""Lay bricks, install plumbing, then paint"
FlexibilityLimited, staticHigh, with loops, conditions, classes
Ease of useEasier to read and manageEasier to reuse and integrate with logic
DebuggingLess transparentEasy to debug with standard tools

Declarative IaC Example (Terraform)

resource "aws_s3_bucket" "my_bucket" {
  bucket = "my-cool-bucket"
  versioning {
    enabled = true
  }
}
  • You describe the final outcome.
  • Terraform figures out the steps to achieve it.

Pros:

  • Simple and readable
  • Great for managing large infra at scale
  • Easy auditing and compliance

Cons:

  • Limited logic (no loops, conditions without workarounds)

Imperative IaC Example (AWS CDK, Pulumi)

const bucket = new s3.Bucket(this, 'MyBucket', {
  versioned: true,
});
  • You write code that builds infrastructure.
  • Supports logic, reuse, and integration with app pipelines.

Pros:

  • Familiar to developers
  • Flexible and reusable
  • More automation-friendly

Cons:

  • More complex for non-developers
  • Slightly harder to audit visually

Why Both Styles Exist

Use CaseBest Fit
Large-scale infra teamsDeclarative (e.g., Terraform)
DevOps teams building infra with appsImperative (e.g., CDK, Pulumi)
Environments needing full visibility/complianceDeclarative
Code-based logic reuseImperative

Tools That Mix Both

  • AWS CDK generates CloudFormation templates, blending both styles.
  • Pulumi uses imperative code but also tracks desired state like declarative tools.

Quick Summary

GoalRecommended Approach
Simplicity and visibilityDeclarative (e.g., Terraform)
Logic and flexibilityImperative (e.g., CDK, Pulumi)
Hybrid usageTools that support both (CDK, Pulumi)

Related Terms and Concepts in IaC

Core Concepts

TermDescription
ProvisioningAutomatically creating cloud resources
Declarative ConfigurationDefines what infrastructure should exist
Imperative ConfigurationDefines how to create infrastructure
Desired StateThe target infrastructure configuration
IdempotencyRunning code multiple times produces the same result
State ManagementTracks current vs. desired infrastructure state (e.g., Terraform .tfstate)

Common IaC Tools

ToolType
TerraformDeclarative, multi-cloud
AWS CloudFormationDeclarative, AWS-only
AWS CDKImperative, AWS
PulumiImperative, multi-cloud
AnsibleDeclarative (mostly for config)
Chef / PuppetConfig management
Kubernetes YAMLDeclarative (container infra)

DevOps Practices Related to IaC

PracticeHow It Relates to IaC
CI/CDIaC is used to set up infra automatically in pipelines
GitOpsInfra code stored in Git and applied via automation
Immutable InfrastructureDestroy and recreate infra instead of editing in-place
Blue-Green DeploymentUse IaC to switch traffic safely between environments
Rolling UpdateGradually deploy updates using IaC
Automated TestingInfrastructure can be tested like application code

Cloud Resources Commonly Managed by IaC

Cloud ComponentExample Use via IaC
EC2 / Compute Engine / Azure VMCreate virtual machines
S3 / GCS / Blob StorageCreate storage buckets
VPC / Subnets / Route TablesDefine networking and connectivity
IAM Roles / PoliciesSet up permissions and access control
RDS / DynamoDB / Cloud SQLProvision managed databases
ECS / EKS / AKSSet up container orchestration (including services, tasks, etc.)

Supporting Concepts

TermDescription
DockerfileDescribes how to build container images (used with IaC)
Container OrchestrationKubernetes and IaC often used together
Secrets ManagementIaC integrates with tools like AWS Secrets Manager, Vault
Monitoring and LoggingIaC can provision tools like CloudWatch, Datadog
Cost OptimizationIaC can define auto-scaling and shut down idle resources

Last updated on July 3, 2025

🔍 Explore More Topics

Discover related content that might interest you

TwoAnswers Logo

Providing innovative solutions and exceptional experiences. Building the future.

© 2025 TwoAnswers.com. All rights reserved.

Made with by the TwoAnswers.com team