What is infrastructure as code's role in CI/CD?
See how Infrastructure as Code makes CI/CD pipelines reproducible: version-controlled environments, plan/apply gates, and drift control explained.
Expected Interview Answer
Infrastructure as Code (IaC) defines servers, networks, and cloud resources in version-controlled files, letting a CI/CD pipeline provision and update environments automatically and identically instead of clicking through consoles by hand.
In CI/CD, IaC tools such as Terraform, CloudFormation, or Pulumi turn infrastructure into another artifact the pipeline builds, tests, reviews, and deploys alongside application code. Because the desired state lives in Git, every environment is created from the same definitions, changes go through pull requests and automated plan/apply steps, and drift between environments is eliminated. This makes environments reproducible, auditable, and disposable — you can spin up an identical staging stack or tear down a preview environment as a pipeline step.
- Reproducible, identical environments across dev, staging, and production
- Version control and code review for infrastructure changes
- Eliminates manual, error-prone console clicking
- Enables ephemeral preview environments per pull request
- Auditable history of who changed what and when
- Fast, reliable disaster recovery by re-applying definitions
AI Mentor Explanation
IaC is like a documented, repeatable ground preparation manual for a stadium instead of a groundsman doing it from memory. Every pitch is rolled, watered, and marked to the exact same written spec, so a match at any venue plays consistently. If a pitch gets damaged, you re-apply the manual and rebuild it identically, rather than guessing what settings produced last week's surface.
Step-by-Step Explanation
Step 1
Declare infrastructure in code
Write the desired state of servers, networks, and resources in files using a tool like Terraform or CloudFormation.
Step 2
Store it in version control
Commit the definitions to Git so changes are reviewed, versioned, and auditable like application code.
Step 3
Plan on pull request
Have the pipeline run a plan/preview step to show exactly what will change before anything is applied.
Step 4
Gate and approve
Require reviews and policy checks (e.g., cost or security scans) before the change is allowed to apply.
Step 5
Apply automatically
On merge, the pipeline applies the definitions to provision or update the environment consistently.
Step 6
Detect and correct drift
Periodically compare live infrastructure to the code and re-apply to remove any manual, out-of-band changes.
What Interviewer Expects
- Definition of IaC and common tools (Terraform, CloudFormation, Pulumi)
- How IaC integrates as plan/apply steps in a pipeline
- Value of version control and code review for infrastructure
- Understanding of environment reproducibility and drift
- Awareness of ephemeral/preview environments
- State management and idempotency concepts
Common Mistakes
- Confusing IaC with configuration scripts run manually
- Applying changes without a plan/preview step
- Making manual console changes that cause drift from code
- Committing secrets or state files into version control
- Not distinguishing declarative (Terraform) from imperative approaches
Best Answer (HR Friendly)
“Infrastructure as Code means writing down all your servers and cloud setup as files kept in version control, so the pipeline can build identical environments automatically. In CI/CD it removes manual setup mistakes and makes every environment reproducible, reviewable, and easy to rebuild.”
Code Example
# main.tf — declarative desired state
resource "aws_s3_bucket" "app_assets" {
bucket = "skillveris-${var.environment}-assets"
tags = {
Environment = var.environment
ManagedBy = "terraform"
}
}
variable "environment" {
type = string
}steps:
- run: terraform init
- run: terraform plan -var="environment=staging" -out=plan.tfplan
# approval gate happens here
- run: terraform apply plan.tfplanFollow-up Questions
- What is the difference between declarative and imperative IaC?
- How do you manage Terraform state safely in a team?
- How would you prevent secrets from leaking into IaC files?
- What is configuration drift and how do you detect it?
- How do ephemeral preview environments work with IaC?
MCQ Practice
1. What is a key benefit of using IaC in a CI/CD pipeline?
IaC stores infrastructure as reviewable, versioned files so the pipeline can recreate identical environments on demand.
2. What does 'configuration drift' refer to?
Drift happens when manual changes make the running infrastructure diverge from what the IaC files declare.
3. Why run a 'plan' step before 'apply' in Terraform?
The plan step shows the diff between current and desired state, letting reviewers approve changes before they are applied.
Flash Cards
What is Infrastructure as Code? — Defining servers, networks, and cloud resources in version-controlled files so environments can be provisioned automatically and identically.
Name common IaC tools. — Terraform, AWS CloudFormation, Pulumi, and Azure Bicep.
What is configuration drift? — When live infrastructure diverges from its code definition due to manual, out-of-band changes.
Why put IaC in a CI/CD pipeline? — So infrastructure changes are planned, reviewed, gated, and applied consistently like application code.