Skip to content

fix: resolve Terraform variable validation cross-reference error#304

Merged
lgallard merged 3 commits intomasterfrom
claude/issue-292-20251119-1809
Nov 19, 2025
Merged

fix: resolve Terraform variable validation cross-reference error#304
lgallard merged 3 commits intomasterfrom
claude/issue-292-20251119-1809

Conversation

@lgallard
Copy link
Copy Markdown
Owner

This PR fixes the Terraform variable validation error reported in issue #292.

Problem

Users reported terraform init failures with the error:

The condition for variable "vault_name" can only refer to the variable itself, using var.vault_name.

This occurred because the vault_name validation block referenced another variable (var.vault_name_validation_bypass), which violates Terraform's validation constraints.

Solution

  • Moved vault name word validation from variables.tf to main.tf using locals
  • Simplified variable validation to only check format requirements
  • Added runtime validation with helpful error messages
  • Preserved all existing bypass functionality

Changes

  • variables.tf: Simplified vault_name validation to remove cross-variable reference
  • main.tf: Added locals for vault name validation and runtime checks

Testing

The original user configuration should now work:

module "aws_backup_example" {
  source = "lgallard/backup/aws"
  vault_name = "EC2"
  # ...rest of config
}

Backward Compatibility

  • No breaking changes
  • All existing functionality preserved
  • vault_name_validation_bypass still works as intended

Fixes #292

🤖 Generated with Claude Code

Move vault name word validation from variables.tf to main.tf to comply
with Terraform requirement that variable validations can only reference
the variable itself.

Changes:
- Remove invalid cross-variable reference in vault_name validation
- Add vault name word validation logic using locals in main.tf
- Add runtime validation with clear error messages for restricted words
- Preserve vault_name_validation_bypass functionality
- Maintain backward compatibility

Fixes terraform init error: 'The condition for variable vault_name can
only refer to the variable itself, using var.vault_name'

Co-authored-by: Luis M. Gallardo D. <lgallard@users.noreply.github.com>
@lgallard
Copy link
Copy Markdown
Owner Author

Validation Results ✅

I've tested this PR with the exact user configuration from issue #292 and confirmed the fix works correctly.

Test Configuration

Used the example from issue #292 comment:

module "aws_backup_example" {
  source = "lgallard/backup/aws"
  
  vault_name    = "EC2"
  iam_role_name = "aws-backup-plan-EC2-role"
  plan_name     = "EC2"
}

Test Scenarios & Results

✅ Scenario 1: Success Path (vault name without restricted words)

  • terraform init: PASSED - No cross-variable reference error
  • terraform plan: PASSED - Shows 8 resources to create (vault, IAM role, policies)
  • terraform apply: PASSED - Successfully created all AWS resources
  • AWS validation: CONFIRMED - Backup vault and IAM resources created successfully
  • terraform destroy: PASSED - All resources cleaned up

✅ Scenario 2: Validation Path (vault name with restricted word)

Changed vault_name = "test-vault" to test validation logic:

  • terraform init: PASSED - Format validation only (no cross-variable reference)
  • terraform plan: PASSED - Shows null_resource for validation
  • terraform apply: FAILED AS EXPECTED with helpful error:
ERROR: Vault name validation failed!
The vault name 'test-vault' contains restricted words (test, temp, delete, remove, default).
These words are not recommended for security reasons.

Solutions:
1. Change the vault name to avoid these words
2. For existing vaults, set: vault_name_validation_bypass = true

Key Findings

  1. Resolves the original issue - Users can now run terraform init without errors
  2. Preserves validation - Word validation still works at runtime via locals and null_resource
  3. Helpful error messages - Clear guidance when validation fails
  4. Backward compatible - vault_name_validation_bypass flag works as intended
  5. Terraform compliant - Variable validation only references itself

Recommendation

This PR is ready to merge. The fix successfully resolves issue #292 while maintaining all existing functionality and security validations.

- Add null provider to required_providers in versions.tf (>= 3.0)
- Fix trailing whitespace in main.tf
- Required for null_resource.vault_name_validation used in runtime validation
@lgallard lgallard merged commit 474fa11 into master Nov 19, 2025
39 checks passed
@lgallard lgallard deleted the claude/issue-292-20251119-1809 branch November 19, 2025 19:36
@github-actions github-actions Bot mentioned this pull request Nov 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Relax vault name validation

1 participant