Skip to main content

Module Reference Syntax

Git Source with Version

Always use version tags to ensure reproducible deployments:

Branch Reference

For testing or development:
Avoid using branch references in production. Always pin to specific version tags.

Commit SHA Reference

For maximum stability:

Common Patterns

Multi-Environment Setup

Organize modules across environments:
Example dev environment:
Example production environment:

Module Composition

Build complex infrastructure by composing modules:

Using Module Outputs

Pass outputs between modules:

Variable Management

Using terraform.tfvars

Store environment-specific values:

Using Variable Files

Load different configurations:

Environment Variables

Use TF_VAR prefix:

State Management

Remote State Backend

Always use remote state for team collaboration:

State Isolation

Isolate state by layer or component:

Remote State Data Source

Reference outputs from other state files:

Tagging Strategy

Consistent Tags Across Modules

Provider-Level Default Tags

Testing Modules

Plan Before Apply

Always review changes:

Target Specific Modules

Test individual modules:
Using -target should be temporary. Always run a full plan/apply afterward to ensure consistency.

Validate Configuration

Module Updates

Upgrading Module Versions

  1. Review changelog:
  2. Update source version:
  3. Re-initialize:
  4. Plan and review:
  5. Apply changes:

Pinning Versions in CI/CD

Best Practices

Version Pinning

Always pin module versions using Git tags. Never use branches in production.

State Locking

Use DynamoDB for state locking to prevent concurrent modifications.

Environment Isolation

Separate state files and AWS accounts per environment.

Least Privilege IAM

Grant minimum required permissions. Use IAM roles over access keys.

Module Documentation

Document your usage patterns and custom configurations.

Change Review

Always review terraform plan output before applying.

Troubleshooting

Module Not Found

Error:
Solution:

State Lock Errors

Error:
Solution:

Module Output Not Available

Error:
Solution: Check if the output exists in the module:

VPC Module

Complete VPC infrastructure

EKS Module

Kubernetes cluster setup

State Module

Remote state backend

Infrastructure Guide

Complete deployment workflow