Local Terraform Testing¶
This document provides guidance on how to test Terraform modules locally during development, allowing you to iterate quickly without publishing modules to Terraform Cloud.
๐ Overview¶
Local testing allows you to:
- โ Test module changes before publishing to Terraform Cloud
- ๐ Debug module issues more efficiently
- ๐ Develop and validate new module features locally
- ๐ Ensure module compatibility across different environments
โ Prerequisites¶
- ๐ป Terraform installed locally
- ๐ Access to the module source code
- โ๏ธ Access to Terraform Cloud workspace (credentials handled by Terraform Cloud)
- ๐ Understanding of swap-terraform-modules.md process
๐ฏ Step 1: Prepare Your Local Environment¶
- ๐ฅ Clone or pull the latest version of the module repository
- ๐ Ensure you have the module code locally (e.g.,
c:\forge\src\terraform\saif-web-service) - ๐ Verify your Terraform Cloud workspace access is configured
๐ Step 2: Modify Module Source¶
โ๏ธ Before (Terraform Cloud Module)¶
module "api-service" {
source = "app.terraform.io/SAIFCorp/api-service/saif"
version = "~>2.1.0"
# insert required variables here
project_id = var.project_id
environment = var.environment
}
๐ป After (Local Module)¶
module "api-service" {
source = "c:\\forge\\src\\terraform\\saif-api-service"
# Note: Remove version constraint for local modules
# insert required variables here
project_id = var.project_id
environment = var.environment
}
๐ Step 3: Common Local Module Patterns¶
๐ Web Service Module¶
# From Terraform Cloud
module "web-service" {
source = "app.terraform.io/SAIFCorp/web-service/saif"
version = "~>2.1.0"
# variables...
}
# To Local
module "web-service" {
source = "c:\\forge\\src\\terraform\\saif-web-service"
# variables...
}
๐ API Service Module¶
# From Terraform Cloud
module "api-service" {
source = "app.terraform.io/SAIFCorp/api-service/saif"
version = "~>2.1.0"
# variables...
}
# To Local
module "api-service" {
source = "c:\\forge\\src\\terraform\\saif-api-service"
# variables...
}
๐ Security Module¶
# From Terraform Cloud
module "security" {
source = "app.terraform.io/SAIFCorp/security/azure//modules/application"
version = "~>2.1.0"
# variables...
}
# To Local
module "security" {
source = "c:\\forge\\src\\terraform\\azure-security\\modules\\application"
# variables...
}
๐งช Step 4: Testing Process¶
- ๐ Initialize Terraform
- ๐ Plan Changes
- โก Apply Changes (if safe)
- โ Validate Results
- ๐ Check Azure resources in the portal
- โ๏ธ Verify expected behavior
- ๐งช Test application functionality
โ ๏ธ Step 5: Important Considerations¶
๐ Path Separators¶
- ๐ฅ๏ธ Use double backslashes (
\\) or forward slashes (/) in Windows paths - ๐ก Example:
c:\\forge\\src\\terraform\\module-nameorc:/forge/src/terraform/module-name
๐ท๏ธ Version Constraints¶
- โ Remove version constraints when using local modules
- ๐ซ Local modules don't support version pinning
- ๐ Version is implicit based on your local code state
๐ Relative vs Absolute Paths¶
# ๐ Absolute path (recommended)
source = "c:\\forge\\src\\terraform\\saif-web-service"
# โ ๏ธ Relative path (use with caution)
source = "..\\..\\saif-web-service"
๐ Step 6: Switch Back to Terraform Cloud¶
โ ๏ธ CRITICAL: Always switch back to Terraform Cloud modules before committing!
After testing, revert your changes:
# Switch back from local
module "api-service" {
source = "c:\\forge\\src\\terraform\\saif-api-service"
# variables...
}
# To Terraform Cloud
module "api-service" {
source = "app.terraform.io/SAIFCorp/api-service/saif"
version = "~>2.1.0"
# variables...
}
๐ก Best Practices¶
1. ๐ฟ Use Git Branches¶
- ๐ Create a feature branch for local testing
- ๐๏ธ Keep local module changes separate from production code
2. ๐ Document Changes¶
- ๐ Keep track of what you're testing
- ๐ Document any temporary modifications
3. ๐งฉ Test Incrementally¶
- ๐ถ Test small changes first
- โ Validate each change before proceeding
4. ๐งน Clean Up¶
- ๐ Always revert to Terraform Cloud modules
- ๐๏ธ Remove any temporary test resources
5. โ Validate Before Commit¶
# Ensure you're back to using Terraform Cloud modules
Select-String -Path "*.tf" -Pattern "source.*c:\\\\"
# Should return no results before committing
๐ ๏ธ Troubleshooting¶
๐จ Common Issues¶
- ๐ Path Not Found
- ๐ Verify the local module path exists
-
โ๏ธ Check path separator usage (use
\\or/) -
๐ Module Dependencies
- ๐ฆ Ensure all nested modules are available locally
- ๐ Update nested module sources if needed
๐ง Recovery Steps¶
If you encounter state issues:
- ๐ Reinitialize if needed
๐ Related Documentation¶
- ๐ swap-terraform-modules.md - Process for swapping module sources
- ๐ Terraform Module Development Guidelines
๐ Summary¶
Local Terraform module testing is a powerful development technique that allows for rapid iteration and debugging. Remember to:
- ๐ฅ๏ธ Use proper path syntax for your operating system
- ๐ซ Remove version constraints for local modules
- ๐งช Test thoroughly before publishing changes
- โ ๏ธ Always switch back to Terraform Cloud modules before committing
This approach enables efficient module development while maintaining production stability.