Terragrunt Module for Dagger
A powerful Dagger module for managing Terragrunt, Terraform, and OpenTofu operations in a containerized environment.
Features π¨
| Feature |
Description |
| π οΈ Flexible Base Image |
Built using APKO for a secure and optimized container environment. |
| π§ Multi-Tool Support |
Primarily focused on Terragrunt, but also supports Terraform and OpenTofu. |
| βοΈ Customizable Configurations |
Extensive options for Terragrunt and Terraform settings. |
| πΎ Caching Mechanisms |
Implements caching for Terragrunt and Terraform for improved performance. |
| βοΈ AWS CLI Integration |
Option to include AWS CLI in the container. |
| π Permissions Management |
Fine-grained control over directory permissions. |
| π Environment Variable Handling |
Easy setting and management of environment variables. |
| π Secret Management |
Secure handling of sensitive information like Terraform tokens. |
| π Execution Flexibility |
Run Terragrunt, Terraform, or shell commands within the container. |
Terragrunt Batteries Included π
| Feature |
Description |
| π οΈ Terragrunt, Terraform, and OpenTofu binaries |
Pre-installed binaries for Terragrunt, Terraform, and OpenTofu. |
| π Terragrunt Configuration |
Best practice configuration files for Terragrunt. |
| βοΈ Terragrunt Options |
Configurable options for Terragrunt (see terragrunt_opts.go). |
| π§ Directory Permissions |
Manage directory permissions (see terragrunt_cfg.go). |
| πΎ Caching Configuration |
Setup caching for Terragrunt and Terraform (see terragrunt_cfg.go). |
| π Environment Variables |
Handle environment variables for Terragrunt (see terragrunt_opts.go). |
| π Secret Management |
Secure handling of sensitive information like Terraform tokens. |
Configuration π οΈ
Base Container Options
ctr: Specify a custom base container.
imageURL: Specify a custom base image URL.
tgVersion: Set the version of Terragrunt (default: 0.68.1).
tfVersion: Set the version of Terraform (default: 1.9.5).
openTofuVersion: Set the version of OpenTofu (default: 1.8.2).
enableAWSCLI: Enable or disable the installation of the AWS CLI (default: false).
awscliVersion: Set the version of the AWS CLI to install (default: 2.15.1).
extraPackages: A list of extra packages to install with APKO, from the Alpine packages repository (default: []).
- Terragrunt, Terraform, and OpenTofu versions can be specified or will use defaults.
Permissions and Caching
- Configure directory permissions and set up caching for Terragrunt and Terraform.
Environment and Secrets
- Set environment variables and manage secrets securely.
Usage Examples π
Basic Terragrunt Execution
testEnvVars := []string{
"AWS_ACCESS_KEY_ID=test",
"AWS_SECRET_ACCESS_KEY=test",
"AWS_SESSION_TOKEN=test",
}
// Initialize the Terragrunt module
tgModule := dag.
Terragrunt(dagger.TerragruntOpts{
EnvVarsFromHost: testEnvVars,
}).
WithTerragruntPermissionsOnDirsDefault().
WithTerragruntLogOptions(
dagger.TerragruntWithTerragruntLogOptionsOpts{
TgLogLevel: "debug",
TgForwardTfStdout: true,
},
)
// Execute the init command, but don't run it in a container
tgCtrConfigured := tgModule.
Exec("init", dagger.TerragruntExecOpts{
Source: m.getTestDir("").
Directory("terragrunt"),
})
// Evaluate the terragrunt init command.
tgInitCmdOut, tgInitCmdErr := tgCtrConfigured.
Stdout(ctx)
Running Terragrunt with Custom Options
tgModule := dag.
Terragrunt(dagger.TerragruntOpts{
EnvVarsFromHost: testEnvVars,
TfVersion: "1.7.0",
}).
WithTerragruntPermissionsOnDirsDefault().
WithTerragruntLogOptions(dagger.TerragruntWithTerragruntLogOptionsOpts{
TgLogDisableFormatting: true,
TgLogShowAbsPaths: true,
TgLogLevel: "debug",
}).
WithTerraformLogOptions(dagger.TerragruntWithTerraformLogOptionsOpts{
TfLog: "debug",
TfLogPath: "/mnt/tflogs", // it's a directory that the terragrunt user owns.
}).
// Extra options added for more realism.
WithTerragruntOptions(dagger.TerragruntWithTerragruntOptionsOpts{
IgnoreDependencyErrors: true,
IgnoreExternalDependencies: true,
DisableBucketUpdate: true,
})
// Execute the plan command and get the container back.
tgCtr := tgModule.Exec("plan", dagger.TerragruntExecOpts{
Source: m.
getTestDir("").
Directory("terragrunt"),
Secrets: []*dagger.Secret{
dbPasswordSecret,
apiKeySecret,
sshKeySecret,
},
// Args to output the plan to a file.
Args: []string{
"-out=plan.tfplan",
"-refresh=true",
},
})
Testing π§ͺ
The module includes comprehensive tests covering various aspects of functionality. You can run these tests using:
just test terragrunt
Developer Experience π οΈ
To contribute or modify the module:
- Use Just for task automation.
- Utilize Nix for managing the development environment.
Common commands:
just run-hooks # Initialize pre-commit hooks
just lintall terragrunt # Run linter
just test terragrunt # Run tests
just ci terragrunt # Run entire CI tasks locally
APKO Base Image
This module uses APKO to build its base image, ensuring:
- Enhanced security through minimal attack surface
- Optimized container size and performance
- Reproducible and declarative image builds
For more information on APKO, refer to the Chainguard APKO documentation.
For detailed API documentation and more examples, please refer to the source code and test files in the tests/ directory.