Skip to main content
RunBook Academy

← All break/fix scenarios in Terraform

intermediateterraform-config~20 min

Break/Fix: Terraform Test Failure in CI

Reported symptoms

  • Terraform test fails in CI
  • The test was passing before the change
  • The change is in the configuration
  • The test expects a specific resource attribute

Evidence

  • · The test log shows the assertion failure
  • · The structure of the test does not match the structure of the change
  • · The change is in the configuration but not in the test
  • · The change is consistent with the documentation
Diagnosis and resolutionclick to reveal

Root cause

The configuration was changed but the test was not updated. The test expects a specific attribute that the new configuration does not provide.

Remediation

1. Update the test to match the new configuration. 2. Verify the test passes. 3. Document the change.

Verification

The test passes. The CI pipeline is green.

Prevention

- 'Update the test when the configuration changes' - 'Run the test locally before pushing' - 'Use the test to validate the change'

Scenario

You are operating a production Terraform estate. The CI pipeline runs terraform test. The test fails after a configuration change.

Error: test failure

  on main.tf line 12, in resource "aws_instance" "web":
  12:   instance_type = "t3.medium"

The test expects "t3.small" but the configuration produces
"t3.medium".

FAIL: tests/main.tftest.hcl

The test was passing before the change.

Your task

Investigate the cause and remediate.

Evidence to discover

# Check the recent commits
git log --oneline -10

# Check the diff
git diff HEAD~1 main.tf

# Check the test
cat tests/main.tftest.hcl

Questions to answer

  1. What changed in the configuration?
  2. What does the test expect?
  3. What is the correct remediation?

Recovery procedure

  1. Identify the cause. The recent commit changed the instance_type from t3.small to t3.medium. The test expects t3.small. The test is now wrong.

  2. Decide the remediation. The configuration change is intentional. The test should be updated to match.

  3. Apply the remediation.

# Update the test to match the new configuration
vim tests/main.tftest.hcl

# Run the test locally
terraform test

# Verify the test passes
echo "Test passed"
  1. Document the change.
git add tests/
git commit -m "Update test for new instance type"
git push

The CI pipeline re-runs the test. The test passes.

Remediation

The cause was the configuration change without a corresponding test update. The remediation was to update the test. The CI pipeline is green.

Prevention

  • Update the test when the configuration changes.
  • Run the test locally before pushing.
  • Use the test to validate the change.
  • Document the test update in the same commit as the configuration change.

What you learned

  • A test failure after a change is a signal that the test is out of date.
  • The test should be updated alongside the configuration.
  • The CI pipeline is the production control.
  • The local test is the first line of defence.