Loading...
Loading...
Infrastructure as Code best practices for Terraform, Docker, Ansible, and CloudFormation. Covers secure-by-default configurations, multi-stage builds, state management, and modular patterns. Use when working with .tf, Dockerfile, docker-compose.yml, .yaml/.yml Ansible files, CloudFormation templates, or when asking about IaC, containers, or infrastructure automation.
npx skill4agent add kiraneswaran/engineering-skills infrastructure-iac| Use Case | Tool |
|---|---|
| Cloud infrastructure | Terraform |
| Containers | Docker |
| Configuration management | Ansible |
| AWS-native IaC | CloudFormation |
terraform init # Initialize working directory
terraform plan # Preview changes
terraform plan -out=tfplan # Save plan for apply
terraform apply tfplan # Apply saved plan
terraform fmt -recursive # Format all files
terraform validate # Validate configuration# 1. Pin provider versions
terraform {
required_version = ">= 1.6.0"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.60"
}
}
}
# 2. Use for_each for stability
resource "aws_subnet" "private" {
for_each = var.private_subnets # NOT: count
}
# 3. Validate inputs
variable "environment" {
type = string
validation {
condition = can(regex("^(dev|staging|prod)$", var.environment))
error_message = "Must be: dev, staging, prod."
}
}
# 4. Mark sensitive data
variable "db_password" {
type = string
sensitive = true
}
# 5. Lifecycle protection
resource "aws_s3_bucket" "state" {
lifecycle {
prevent_destroy = true
}
}# Build stage
FROM node:20-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
RUN npm run build
# Production stage
FROM node:20-alpine
WORKDIR /app
COPY /app/dist ./dist
COPY /app/node_modules ./node_modules
USER node
EXPOSE 3000
CMD ["node", "dist/index.js"]# Pin versions
FROM python:3.12.1-slim-bookworm
# Run as non-root
RUN useradd -m appuser
USER appuser
# Layer optimization
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
# Health check
HEALTHCHECK \
CMD curl -f http://localhost:8080/health || exit 1| Tool | Convention | Example |
|---|---|---|
| Terraform | snake_case | |
| Docker | lowercase, hyphens | |
| Ansible | snake_case | |
| CloudFormation | Hungarian | |