Welcome to Lab Three! In this session, you’ll learn how to structure Terragrunt configurations for a multi-module AWS deployment. Follow these steps to provision a VPC, EC2 instances, and manage remote state with Terraform and Terragrunt. If you ever need to retrieve your AWS credentials during the lesson, run:Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.

Terraform Modules Overview
| Module | Source | Purpose |
|---|---|---|
| VPC | terraform-aws-modules/vpc/aws//?version=5.8.1 | Create a scalable VPC with public & private subnets |
| EC2 Web | terraform-aws-modules/ec2-instance/aws//?version=2.0.0 | Launch a web server in the public subnet |
| EC2 Database | (Your custom module) | Deploy a database instance in a private subnet |
1. Define the VPC Module
Create a Terragrunt configuration underTerraform stack/vpc/terragrunt.hcl:

2. Configure Remote State in the Root
What is path_relative_to_include()?

The
path_relative_to_include() function computes the relative path from the current terragrunt.hcl to the included parent. This ensures each module’s state key is unique in S3.Terraform stack/terragrunt.hcl, add:
- List your S3 buckets to find the Terraform state bucket:
- Reinitialize Terragrunt and confirm updating the backend:
3. Define Common Locals
To DRY out your configuration, define shared values at the root:4. Generate Provider and Terraform Version Files
Leverage Terragrunt’sgenerate blocks to auto-create providers.tf and a Terraform version override:
5. Include Root Configuration in the VPC Module
InTerraform stack/vpc/terragrunt.hcl, inherit root settings:

6. Deploy EC2 Web Module with VPC Dependency
CreateTerraform stack/ec2-web/terragrunt.hcl and reference the VPC outputs:

7. Add Database Dependency to the Web Module
If you have a database module underTerraform stack/ec2-database, ensure it deploys before the web server:
ec2-web:
8. Deploy All Modules Simultaneously
From the root ofTerraform stack, run:
9. Verify Remote State Files in S3
Check that each module has its own state file:10. Why Generate at the Root
By generating the backend, provider, and Terraform-version files at the project root, you:- Centralize configuration for consistency
- Avoid duplication in child modules
- Simplify maintenance as your infrastructure grows
Thank you for completing Lab Three! You now have a reusable Terragrunt setup for AWS VPC, EC2, and remote state management. See you in the next lab!