Things To Learn In Devops

Things To Learn In Devops

Reading time1 min
#DevOps#Cloud#Automation#InfrastructureAsCode#IaC#Terraform

Mastering Infrastructure as Code: Foundations for Reliable DevOps

Manual server setup doesn’t scale, and hand-crafted infrastructure inevitably drifts. Infrastructure as Code (IaC) solves these operational headaches by bringing version control, repeatability, and automation into the deployment lifecycle. For modern DevOps, IaC isn’t just a tool—it's the method.


Scenario: Rolling Out a Production-Grade Web Application

You’re tasked to spin up identical staging and production environments across AWS and Azure. Manual console work? Risky, slow, and difficult to audit. Instead, you need codified infrastructure: configuration files in Git, peer-reviewed, tested, deployed via CI/CD. A typical workflow:

$ git clone git@github.com:org/webapp-infra.git
$ terraform init
$ terraform apply -var="env=prod"

After a successful deployment, you notice only the changes in main.tf are applied—no accidental overwrites. Critical for compliance and for quick disaster recovery.


Infrastructure as Code (“IaC”): What and Why

IaC defines resources (compute, networking, IAM, etc.) in machine-readable config files—typically HCL (Terraform), YAML (CloudFormation/Ansible), or Python/Go (Pulumi). These configs serve a single source of truth and are managed in version control systems like Git.

  • Declarative configuration describes the desired state.
  • Automation eliminates repetitive tasks.
  • Version control enables auditability and collaboration.
  • Fast provisioning with minimal human error.

Legacy practice: SSH to each VM, run shell scripts, hope for consistency. Modern approach: terraform plan shows the delta, terraform apply enforces it—identically, anywhere.


Core IaC Concepts Every Engineer Should Master

Declarative vs. Imperative

  • Declarative (preferred for infra): Specify “what” (e.g., three web servers, specific VPC and subnets). Example:
    resource "aws_instance" "web" { ... }
    
  • Imperative: Specify the sequence of operations (e.g., Ansible plays, shell scripts).

State Management

Most IaC tools maintain resource state, e.g., .tfstate for Terraform. State consistency is crucial; mismanaged remote state is a known failure mode in teams.

Modularity

Reuse patterns with modules (Terraform), roles (Ansible), nested stacks (CloudFormation). Avoids code duplication and promotes consistency.

Idempotency

Applying the same config repeatedly should converge to the same infrastructure. Not all resources are truly idempotent—watch out for certain platform APIs (AWS IAM roles, Azure network policies).


Tool Selection: Fast Comparison

ToolLanguage(s)StyleCloud TargetsBest Use Cases
Terraform 1.8.xHCLDeclarativeMulti-cloudGeneral-purpose, modules ecosystem
AWS CloudFormationYAML/JSONDeclarativeAWSDeep AWS integration, drift detection
Ansible 8.xYAMLImperative-ishAny/On-premInfra + config mgmt, procedural logic
Pulumi 3.xPython, TS, Go, etc.HybridMulti-cloudComplex logic, language-native testing

Note: For serious multi-cloud deployments, Terraform remains the pragmatic choice—though Pulumi is gaining ground for teams heavily invested in general-purpose languages.


Example: Provisioning an EC2 Instance with Terraform (Correct Practice)

Pre-reqs:

  • Terraform >=1.5.0
  • AWS CLI configured with profile devops
  • Valid AWS credentials (prefer federated/assume-role access)

Directory structure:

terraform-demo/
  main.tf
  variables.tf
  outputs.tf

main.tf

provider "aws" {
  region  = var.aws_region
  profile = "devops"
}

resource "aws_instance" "web" {
  ami           = var.ami_id
  instance_type = "t2.micro"
  tags = {
    Name = "IaC-Demo"
  }
}

variables.tf

variable "aws_region" { default = "us-east-1" }
variable "ami_id"     { default = "ami-0c94855ba95c71c99" } # Update for your region

outputs.tf

output "public_ip" {
  value = aws_instance.web.public_ip
}

Commands:

terraform init
terraform plan -out=tfplan
terraform apply tfplan

Sample output:

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Outputs:
public_ip = "54.204.12.34"

To destroy (don’t forget—stranded resources incur cost):

terraform destroy -auto-approve

Gotcha: If you see

Error: Error acquiring the state lock

, another user is probably running terraform apply concurrently. Coordinate or use a proper remote backend (e.g., S3 with DynamoDB lock for AWS).


Non-Obvious Tips & Trade-offs

  • Remote State: Always use a shared backend (e.g., S3, GCS) for team workflows, never local state for production.
  • Secrets Management: Never commit AWS keys or cloud tokens to your repo. Integrate with Hashicorp Vault or cloud KMS.
  • Resource Drift: Periodically run terraform refresh or equivalent. Automated drift detection is possible but rarely perfect.
  • CI/CD Pipelines: Run terraform fmt -check and terraform validate as part of your pipeline.
  • Module Versioning: Pin module versions in source to avoid unplanned upgrades.

Engineering Perspective

Infrastructure as Code turns infrastructure operations into a software engineering problem—a significant leap for reliability, compliance, and speed. The first configuration is often the hardest; subsequent changes (cloning, scaling, disaster recovery) become trivial.

Still, no IaC tool is flawless. Cloud providers change APIs; “idempotent” doesn’t always mean “side-effect free.” Always verify state before destroy or plan operations—especially across regions and multiple providers.


Infrastructure as Code isn’t magic. It’s disciplined configuration, tested and delivered like any upstream artifact. Teams embracing it move faster and break less.


Side note: For fast prototyping, consider running terraform apply -auto-approve in a dev sandbox, but never in production—always review plans. Ignore this at your own risk.