Terraform for Multi-Cloud: Modules & Reusability
Terraform for Multi-Cloud: Modules & Reusability
As infrastructure grows, reusability becomes the cornerstone of maintainable Terraform projects.
When you start small — say, spinning up a VPC or a few EC2 instances — it’s easy to keep all your code in one file. But in multi-cloud or multi-environment setups, that approach becomes unmanageable fast. You need a way to reuse components across projects while keeping configurations consistent and auditable.
That’s where Terraform modules shine.
What Are Modules?
Modules are simply containers for Terraform code — a way to group related resources into a reusable, self-contained package.
For example, a “network” module might contain:
Then, in your root configuration, you can reuse that same module across multiple environments or even across AWS and Azure:
his modular structure promotes:
-
Consistency: Everyone uses the same network structure.
-
Speed: New environments can be deployed quickly.
-
Maintainability: Updates to the module propagate automatically.
Recommended Folder Structure
A clean folder layout makes a huge difference. Here’s a commonly used approach:
Each
env uses the same modules but with different variables — creating a powerful multi-environment setup without code duplication.If you’ve ever heard the phrase “Terraform state is everything,” it’s true — and it’s critical to understand why.
Terraform maintains a state file (terraform.tfstate) that records the actual infrastructure deployed in your cloud environments. This file acts as Terraform’s “source of truth” — it knows what exists and what changes need to be made on the next run.
In a small setup, local state works fine. But in real-world, team-based environments — especially across AWS and Azure — you’ll need remote state management to ensure:
-
Everyone uses the same state data.
-
No one overwrites another’s changes.
-
There’s a record of all updates.
Using Remote State Backends
Terraform supports several backends for storing state remotely:
-
AWS S3 (with DynamoDB for locking)
Example: AWS S3 remote state
This configuration stores the state in a central S3 bucket and uses a DynamoDB table for state locking — preventing two people from running terraform apply at the same time.
Benefits of Remote State
-
Team Collaboration: Everyone reads from the same source of truth.
-
Security: Sensitive outputs are not exposed locally.
-
Scalability: Ideal for managing multiple environments and cloud accounts.
State Drift Detection
Terraform automatically detects drift — differences between your declared configuration and the real infrastructure. Using terraform plan, you can preview exactly what’s changed, making it easy to maintain consistency even when manual changes slip in.
🔍 Pro Tip: Always version your backend and enable encryption for state storage — your state files often contain sensitive data like resource IDs, IP addresses, and secrets.
Workspaces for Multi-Environment Infrastructure
Managing multiple environments (e.g., dev, staging, prod) is another key challenge for DevOps teams — especially when the same infrastructure must exist with slight variations.
Terraform’s workspaces make this easier by providing isolated state files within a single configuration.
How Workspaces Work
Each workspace maintains its own separate state. You can switch between them with simple commands:
Then, in your configuration, you can reference the current workspace dynamically:
This would create:
-
myapp-dev-bucket -
myapp-staging-bucket -
myapp-prod-bucket
—all from the same Terraform codebase.
Workspaces vs. Directory-Based Environments
Workspaces are perfect when your environments are similar and differ only by a few variables.
However, for highly customized environments, directory-based structures (like the earlier envs/dev, envs/prod) offer more flexibility and control.
In multi-cloud projects, it’s common to combine both:
-
Use directories for provider-specific configurations (AWS vs Azure).
-
Use workspaces to separate environment states (dev, staging, prod).
This hybrid approach ensures scalability, clarity, and separation of concerns.