Infrastructure as Code for Modern Cloud and On-Premises Environments
Chapter 1: The OpenTofu Story: From Terraform to Open Source
- What Is OpenTofu?
- The Terraform License Change: A Turning Point
- HashiCorp vs. Linux Foundation: The Fork That Changed Everything
- OpenTofu vs. Terraform: Compatibility, Differences, and Trade-offs
- When to Choose OpenTofu (and When Not To)
Chapter 2: Architecture and Core Concepts
- The Declarative Paradigm: What You Define vs. What Happens
- OpenTofu’s Execution Model: Plan, Apply, Destroy
- Providers: The Bridge Between Configuration and Reality
- Resources: The Fundamental Building Blocks
- State: Why It Exists and How It Works
- Data Sources: Reading the World Without Creating It
Chapter 3: Installation and Project Structure
- Installing OpenTofu: Linux, macOS, Windows, and Containerized Options
- Verifying Your Installation and Checking Versions
- The Minimal Working Project
- Directory Layout Conventions: Single Project vs. Modular Organization
- Configuration File Naming Rules: main.tf, variables.tf, and Beyond
- .terraform-version, .gitignore, and Essential Project Files
Chapter 4: HCL Syntax and Language Fundamentals
- Blocks, Arguments, Comments, and Basic Structure
- Attributes and Types: Strings, Numbers, Booleans, Lists, Maps, Objects
- Expressions: Interpolation, Conditionals, and Operators
- For Expressions: Transforming Collections Elegantly
- Dynamic Blocks: Generating Repetitive Configuration Programmatically
- Null, Unknown, and Type Conversion: Gotchas Every Engineer Must Know
Chapter 5: Variables, Locals, and Outputs
- Input Variables: Types, Defaults, Validation Rules, and Sensitive Values
- Variable Files (.tfvars): Environment-Specific Configuration Management
- Locals: Organizing Complex Logic and Avoiding Repetition
- Outputs: Exposing Information and Connecting Modules
- Passing Data Between Modules: The Parent-Child Contract
Chapter 6: Providers, Resources, and Data Sources in Depth
- How Providers Work: Plugins, Authentication, and Configuration Blocks
- Resource Arguments: Required, Optional, Computed, and Deprecated
- Creating Resources Across Clouds: AWS, Azure, GCP Examples
- Data Sources: Querying Existing Infrastructure Safely
- Provider Aliases: Managing Multiple Configurations Simultaneously
- Version Constraints: Pinning Providers for Stability
Chapter 7: State Management and Backends
- Understanding State: What It Stores and Why Local Is Not Enough
- Backend Types: Local, S3, Azure Blob, GCS, Consul, and More
- Remote State Configuration: Locking, Encryption, and Versioning
- Reading Remote State: Cross-Referencing Infrastructure Between Projects
- State Migration: Moving Between Backends Safely
- State Troubleshooting: Corruption, Drift, and Recovery Procedures
Chapter 8: Modules: Building Reusable Infrastructure Components
- What Is a Module?: Root Modules, Child Modules, and Published Modules
- Designing Your First Module: Inputs, Outputs, and Responsibility Boundaries
- Calling Modules: Local Paths, Registry Sources, and Git Repositories
- The OpenTofu Registry: Publishing and Consuming Community Modules
- Advanced Module Patterns: Stacks, Composition, and Versioned Releases
- Anti-Patterns: Monolithic Modules, Hidden Dependencies, and Over-Abstraction
Chapter 9: Workspaces, Lifecycle Rules, and Dependencies
- Workspaces: Multiple Environments from One Configuration
- When Workspaces Help (and When They Hurt): Practical Guidance
- Lifecycle Arguments: create_before_destroy, prevent_destroy, ignore_changes
- Dependency Management: Explicit depends_on vs. Implicit References
- The Dependency Graph: Visualizing and Debugging Execution Order
- Tainting Resources: Forcing Recreation When Needed
Chapter 10: Provisioners and Importing Existing Infrastructure
- The Problem With Provisioners: Why They Are Discouraged
- When Provisioners Make Sense: file, local-exec, remote-exec, and Beyond
- Best Practices If You Must Use Provisioners: Idempotency and Error Handling
- Importing Existing Resources: The import Command and Workflow
- Generating Configuration for Imported Resources: State Inspection Techniques
- Migration Strategies: Bringing Legacy Infrastructure Under OpenTofu Control
Chapter 11: Testing, Validation, Formatting, and Policy Enforcement
- Formatting and Linting: tofu fmt, tofu validate, and Custom Rules
- Pre-commit Hooks: Catching Errors Before They Reach the Repository
- Testing Strategies: Unit Tests, Integration Tests, and Plan Validation
- OpenTofu Test Framework: Built-in Testing Capabilities
- Policy as Code: Sentinel, OPA, and openpolicyagent Integration
- Security Scanning: Detecting Misconfigurations with tfsec, checkov, and Trivy
Chapter 12: Security Best Practices and Secrets Management
- Securing State Files: Encryption at Rest and in Transit
- Managing Secrets: Never Hardcoding Credentials
- Provider Authentication Patterns: IAM Roles, Service Principals, Workload Identity
- Least Privilege for OpenTofu Execution: IAM Policies and RBAC
- Audit Logging and Compliance: Tracking Who Changed What and When
- Common Security Pitfalls: Exposed Keys, Overly Permissive Policies, and Insecure Backends
Chapter 13: CI/CD Integration, GitOps, and Automation Workflows
- The Infrastructure Pipeline: Commit, Plan, Review, Apply
- GitHub Actions Integration: Complete Workflow Examples
- GitLab CI, Jenkins, and Azure DevOps: Multi-Platform Pipeline Patterns
- GitOps With OpenTofu: Declarative Deployment Using ArgoCD and Flux
- Approval Gates and Change Management: Human Review in Automated Workflows
- Parallel Execution and Performance Optimization: Scaling Plans and Applies
Chapter 14: Enterprise-Scale Patterns and Production Operations
- Multi-Cloud and Hybrid Infrastructure: Managing AWS, Azure, GCP, and On-Premises Together
- Organizational Structure: Team-Based Repository Layouts and Shared Responsibility
- Environment Strategy: Development, Staging, Production Separation Patterns
- Versioning and Upgrades: OpenTofu Versions, Provider Updates, and Breaking Changes
- Debugging and Logging: Tracing Failures in Complex Deployments
- Real-World Case Studies: Production Patterns from Leading Organizations