Terragrunt for Beginners
Terragrunt Commands
terragrunt init and validate
In this lesson, we explore how terragrunt init
and terragrunt validate
work under the hood and in practice. These commands streamline your Terraform workflow when using Terragrunt.
terragrunt init
The terragrunt init
command sets up your working directory based on terragrunt.hcl
, handling:
- Provider plugin installation and updates
- Module dependency resolution and download
- Backend initialization for state management
Note
Terragrunt automatically inherits backend settings from your root terragrunt.hcl
. If you modify source or backend blocks, re-run terragrunt init
.
After initialization, your directory is ready for planning and applying infrastructure changes.
terragrunt validate
The terragrunt validate
command performs a syntax and semantic check on your Terraform configurations:
- Ensures HCL syntax is correct
- Verifies required variables and providers are defined
- Catches common misconfigurations before planning
Run terragrunt validate
early to catch errors in development:
Under the hood, Terragrunt calls Terraform’s terraform validate
:
Parallel Validation Across Modules
In large, modular projects you can run all validations in parallel:
terragrunt run-all validate
Warning
Running run-all validate
may trigger API rate limits if modules share provider endpoints. Monitor your quotas during parallel execution.
Best Practices
Integrate terragrunt validate
into your CI/CD pipeline to enforce checks before deployment:
Command Reference
Command | Purpose | Example Usage |
---|---|---|
terragrunt init | Initialize plugins, modules, backend | terragrunt init |
terragrunt validate | Check HCL syntax and semantics | terragrunt validate |
terragrunt run-all validate | Validate all modules in parallel | terragrunt run-all validate |
Example: Using terragrunt init
& terragrunt validate
Below is a basic terragrunt.hcl
for an AWS VPC module:
# terragrunt.hcl
terraform {
source = "tfr://terraform-aws-modules/vpc/aws//?version=5.8.1"
}
inputs = {
name = "KodeKloud-VPC"
}
Initialize and validate in the same folder:
$ terragrunt init
Initializing the backend...
Initializing provider plugins...
- Reusing previous version of hashicorp/aws from the dependency lock file
- Using previously-installed hashicorp/aws v5.51.1
Terraform has been successfully initialized!
You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. All Terraform commands should now work.
If you ever set or change modules or backend configuration for Terraform, rerun this command to reinitialize your working directory. If you forget, other commands will detect it and remind you to do so if necessary.
$ terragrunt validate
Success! The configuration is valid.
That concludes our overview of terragrunt init
and terragrunt validate
. By initializing early and validating frequently, you ensure reliable, maintainable infrastructure as code.
Watch Video
Watch video content