Skip to content

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

  1. ๐Ÿ“ฅ Clone or pull the latest version of the module repository
  2. ๐Ÿ“‚ Ensure you have the module code locally (e.g., c:\forge\src\terraform\saif-web-service)
  3. ๐Ÿ” 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

  1. ๐Ÿš€ Initialize Terraform
terraform init
  1. ๐Ÿ“‹ Plan Changes
terraform plan
  1. โšก Apply Changes (if safe)
terraform apply
  1. โœ… Validate Results
  2. ๐Ÿ” Check Azure resources in the portal
  3. โœ”๏ธ Verify expected behavior
  4. ๐Ÿงช Test application functionality

โš ๏ธ Step 5: Important Considerations

๐Ÿ“ Path Separators

  • ๐Ÿ–ฅ๏ธ Use double backslashes (\\) or forward slashes (/) in Windows paths
  • ๐Ÿ’ก Example: c:\\forge\\src\\terraform\\module-name or c:/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

  1. ๐Ÿ“‚ Path Not Found
  2. ๐Ÿ” Verify the local module path exists
  3. โœ”๏ธ Check path separator usage (use \\ or /)

  4. ๐Ÿ”— Module Dependencies

  5. ๐Ÿ“ฆ Ensure all nested modules are available locally
  6. ๐Ÿ”„ Update nested module sources if needed

๐Ÿ”ง Recovery Steps

If you encounter state issues:

  1. ๐Ÿ”„ Reinitialize if needed
terraform init -reconfigure
  • ๐Ÿ“– 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:

  1. ๐Ÿ–ฅ๏ธ Use proper path syntax for your operating system
  2. ๐Ÿšซ Remove version constraints for local modules
  3. ๐Ÿงช Test thoroughly before publishing changes
  4. โš ๏ธ Always switch back to Terraform Cloud modules before committing

This approach enables efficient module development while maintaining production stability.