Skip to main content
Welcome to Lab 4. In this lesson, you’ll configure Terragrunt to deploy and manage an AWS VPC module. You have access to an AWS account—follow the steps below to set up credentials, initialize modules, enforce safeguards, and customize Terragrunt settings for a robust infrastructure workflow.
Keep your AWS credentials secure. You can retrieve them with:
Or log in via the provided link using your username and password. Consider opening a second terminal tab to streamline copy-and-paste.

1. Configure the VPC Module

In Terraform stack/VPC/terragrunt.hcl, reference the remote AWS VPC module (v5.8.1) from the Terraform Registry:
Initialize and review the plan:
You should see 4 to add. If everything checks out, continue to the next section.

2. Configure a Custom Terragrunt Cache

Terragrunt can cache downloaded modules locally to speed up repeated runs. Add a top-level download_dir in your root terragrunt.hcl:
Re-initialize and verify the cache directory:
Then plan and apply:
After confirming the apply, check the AWS Console under VPC to see your new VPC.

3. Prevent Accidental Destruction

Protect critical resources by adding a prevent_destroy lifecycle rule:
Re‐apply and test destruction:
Terragrunt will refuse to destroy due to the prevent_destroy setting.
If you need to remove the resource later, you must first remove or comment out the prevent_destroy block.

4. Use a Specific IAM Role

All Terragrunt operations should assume the KodeKloudTerragruntRole role. Retrieve your AWS account ID:
Then add the role ARN to terragrunt.hcl:
Verify the role is in use:

5. Specify a Custom Terraform Binary & Version

Use the Terraform 1.82 binary packaged in this stack:
Re‐run:

6. Enforce a Terragrunt Version Constraint

Require Terragrunt in the >= 0.34.0, < 0.40.0 range:
If you encounter a compatibility error (e.g., on version 0.58.8), update to include your version:
Then re‐plan:

7. Configure Retryable Errors

Handle transient network or locking issues by specifying retry patterns:
Run:
Terragrunt will retry on matching errors automatically.

Terragrunt Settings at a Glance

SettingPurposeExample
terraform.sourceModule source"registry.terraform.io/.../vpc/aws"
download_dirCache directory for modules"/path/to/.terragrunt_config"
lifecycle.prevent_destroyPrevent critical-resource deletionprevent_destroy = true
iam_roleSpecifies assumed IAM role"arn:aws:iam::123456789012:role/...TerragruntRole"
terraform_binaryCustom Terraform CLI path"/path/to/terraform_1.82/terraform"
terraform_version_constraintLock Terraform to a specific version"1.82"
terragrunt_version_constraintLock Terragrunt to a version range">= 0.34.0, <= 0.59"
retryable_errorsPatterns that trigger automatic retries["Error locking state:.*", "no such host"]

That completes Lab 4. Thank you for following along!

Watch Video

Practice Lab