Terratest provides two approaches for testing Terragrunt configurations:
| Approach | Use Case | Package |
|---|---|---|
| Single-module | Testing individual modules | modules/terraform with TerraformBinary: "terragrunt" |
| Multi-module | Testing multiple modules with --all commands |
modules/terragrunt |
For testing a single Terragrunt module, use the terraform package with TerraformBinary set to "terragrunt":
func TestTerragruntModule(t *testing.T) {
t.Parallel()
terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
TerraformDir: "../examples/my-module",
TerraformBinary: "terragrunt",
})
defer terraform.Destroy(t, terraformOptions)
terraform.Apply(t, terraformOptions)
output := terraform.Output(t, terraformOptions, "my_output")
assert.Equal(t, "expected_value", output)
}
For testing multiple modules with dependencies, use the dedicated terragrunt package:
func TestMultiModule(t *testing.T) {
t.Parallel()
testFolder, err := files.CopyTerragruntFolderToTemp("../live/prod", t.Name())
require.NoError(t, err)
options := &terragrunt.Options{
TerragruntDir: testFolder,
}
defer terragrunt.DestroyAll(t, options)
terragrunt.ApplyAll(t, options)
exitCode := terragrunt.PlanAllExitCode(t, options)
require.Equal(t, 0, exitCode)
}
| Function | Description |
|---|---|
Init |
Run terragrunt init |
ApplyAll |
Run terragrunt apply --all |
DestroyAll |
Run terragrunt destroy --all |
PlanAllExitCode |
Run terragrunt plan --all, return exit code |
ValidateAll |
Run terragrunt validate --all |
FormatAll |
Run terragrunt fmt --all |
RunAll |
Run any command with --all flag |
For Terragrunt stacks:
| Function | Description |
|---|---|
StackGenerate |
Generate stack from terragrunt.stack.hcl |
StackRun |
Run command on generated stack |
StackClean |
Remove .terragrunt-stack directory |
Output |
Get stack output value |
OutputAll |
Get all stack outputs as map |
Your entire infrastructure. Defined as code. In about a day.
Explore Gruntwork.io