System Design AI

Infrastructure as Code in System Design Interviews: When to Use It and What to Say

Infrastructure as code (IaC) is the practice of defining cloud resources—servers, databases, networking, IAM policies—in declarative or imperative code that can be stored in version control and applied automatically.

The Problem IaC Solves

When you manually create a VPC, a set of EC2 instances, and a load balancer, two things go wrong over time. First, the configuration drifts: a teammate adds a security group rule, another removes a tag, and the live environment no longer matches the documented architecture. Second, scaling the same setup for a new region or a new environment (dev, staging, prod) requires copy-and-paste, which is error-prone and slow.

A concrete system illustrates this. Imagine a social-media feed service that stores user posts in DynamoDB, serves them through an API gateway, and uses Lambda functions for business logic. The service must run in three AWS regions for latency and resilience. Without IaC, each region’s resources would be created manually, leading to inconsistent IAM policies, mismatched capacity settings, and a high chance of forgetting a required CloudWatch alarm.

How IaC Works, Step by Step

  1. Choose a language or tool – Terraform, AWS CloudFormation, or the CDK are common. All expose a set of resource types (e.g., aws_vpc, aws_dynamodb_table).
  2. Write a module – Define each component once. For the feed service you might have a vpc.tf, dynamodb.tf, and lambda.tf file.
  3. Parameterize – Use variables for region, environment name, and capacity. This lets the same code be reused for us-east-1, eu-west-1, etc.
  4. Plan – Run terraform plan (or cdk diff) to see the exact changes the tool will make to the cloud.
  5. Apply – Execute terraform apply. The tool talks to the cloud provider’s APIs, creates or updates resources, and records the state.
  6. Version Control – Store the files in Git. Every change is a commit, making roll-backs and peer review trivial.
  7. Automation – Hook the apply step into a CI/CD pipeline. When a PR merges, the pipeline runs terraform apply for the target environment, guaranteeing that code and infrastructure stay in sync.

Small Worked Example

hcl
# variables.tf
variable "region" { default = "us-east-1" }
variable "env" { default = "dev" }

# vpc.tf
resource "aws_vpc" "feed_vpc" {
 cidr_block = "10.0.0.0/16"
 tags = {
 Name = "feed-${var.env}"
 Env = var.env
 }
}

# dynamodb.tf
resource "aws_dynamodb_table" "posts" {
 name = "posts-${var.env}"
 billing_mode = "PAY_PER_REQUEST"
 hash_key = "post_id"
 attribute {
 name = "post_id"
 type = "S"
 }
}

# lambda.tf
resource "aws_lambda_function" "handler" {
 function_name = "feed-handler-${var.env}"
 runtime = "nodejs20.x"
 handler = "index.handler"
 role = aws_iam_role.lambda_exec.arn
 source_code_hash = filebase64sha256("lambda.zip")
}

With a provider "aws" { region = var.region } block added, running terraform apply -var env=prod -var region=eu-west-1 provisions an identical stack in Europe, with all names automatically suffixed by prod. The state file records the exact IDs, so the next run knows whether to update or replace a resource.

Where IaC Shows Up in Classic Design Questions

Design QuestionTypical Spot IaC Is MentionedWhy It Matters
Design a URL shortenerWhen describing the data store provisioning (e.g., DynamoDB tables) and the need for identical tables across regions.Shows you can spin up identical back-ends without manual steps.
Build a video streaming platformIn the section on CDN edge configuration and automated origin server scaling.Demonstrates repeatable, version-controlled edge cache policies.
Create a real-time chat serviceWhen you talk about provisioning a fleet of WebSocket servers behind a load balancer.Guarantees that each server group has the same security groups and autoscaling rules.

In each case, the interviewer expects you to note that the infrastructure definition lives in code, is part of the overall design, and can be deployed via a pipeline.

Trade-offs and the Alternative Interviewer May Probe

AspectIaC AdvantagesIaC Drawbacks
Speed of rolloutOne command creates a whole environment; you can spin up a new region in minutes.Initial learning curve; the first write may be slower than a quick manual test.
ConsistencyGuarantees identical resources; drift is eliminated.Over-reliance can hide operational nuances (e.g., manual tuning of DB partitions).
ComplexityEncapsulates many resources in a single file, making review easier.Large templates become hard to read; you may need modules to keep things manageable.
Vendor lock-inTools like Terraform are multi-cloud, but many examples use AWS-specific resources.If you rely on provider-specific features, moving to another cloud becomes costly.

Interviewers often follow up with: “What if you can’t use IaC because the team has legacy scripts?” The alternative they want you to discuss is manual provisioning with scripts (e.g., Bash + AWS CLI) or configuration management tools like Ansible that focus on software install rather than resource creation. Explain that scripts give you flexibility but lose the declarative state tracking and drift prevention that IaC provides.

30-Second Pitch

"Infrastructure as code lets you describe every cloud component—VPCs, databases, IAM roles—in version-controlled scripts. It eliminates drift, lets you spin up identical environments in seconds, and fits naturally into a CI/CD pipeline. In a design interview you’d mention IaC when you talk about automated provisioning of data stores, load balancers, or multi-region deployments, and you’d balance its speed and consistency against the extra learning curve and potential vendor lock-in."

Common Mistakes Candidates Make

  • Treating IaC as a silver bullet – Claiming that IaC automatically solves scaling, security, and cost without acknowledging the need for proper parameterization and monitoring.
  • Confusing IaC with container orchestration – Mentioning Kubernetes when the question is about provisioning the underlying VPC, subnets, and IAM roles.
  • Skipping the “plan” step – Jumping straight to “apply” in the narrative, which suggests you ignore the safety net that shows what will change.
  • Over-specifying – Hard-coding region-specific IDs instead of using variables, which defeats the purpose of reuse.
  • Ignoring limits – Not mentioning that some services have quota limits (e.g., VPCs per account) and that IaC scripts must respect them.

How to Practice This

  1. Study the worked design – The System Design AI library includes a worked design for an infrastructure-as-code engine like CloudFormation. Review the step-by-step walkthrough and the notes on each component.
  2. Re-create the design on the canvas – Open a blank canvas, copy the resource list, and write your own Terraform (or CloudFormation) snippets. Practice explaining each component out loud.
  3. Run a mock interview – Use a friend or the AI coach to answer a classic question (e.g., URL shortener). Insert the IaC discussion at the provisioning stage, then ask for feedback on clarity and depth.

Questions people ask

When should I bring up IaC in a design interview?

Mention it whenever the design includes provisioning of cloud resources, especially if the solution spans multiple regions or environments. It signals that you think about repeatability and operational hygiene.

Do I need to know a specific IaC language?

No. Interviewers care about the concepts—declarative definitions, version control, and a plan-apply workflow. You can cite Terraform, CloudFormation, or the CDK without writing full syntax.

What if the company uses a hybrid cloud?

Explain that IaC can target multiple providers from the same codebase (Terraform) or that you would maintain separate modules for each cloud, keeping the same interface for resources.

How does IaC differ from configuration management?

IaC creates the underlying infrastructure (networks, storage, compute). Configuration management configures software on already-provisioned machines (installing packages, setting configs). Both can coexist in a pipeline.