Skip to main content
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:
You can also open the VS Code IDE in a new browser tab to copy and paste commands.
The image shows an AWS IAM user sign-in page with fields for account ID, username, and password. There's also an advertisement for AWS Skill Builder, offering access to free digital courses.

Terraform Modules Overview


1. Define the VPC Module

Create a Terragrunt configuration under Terraform stack/vpc/terragrunt.hcl:
The image shows a split-screen view of a coding environment, with instructions for defining a VPC module on the left and a Visual Studio Code editor with a README file and terminal information on the right.
Save the file and validate the configuration:

2. Configure Remote State in the Root

What is path_relative_to_include()?

The image shows a split-screen view with a coding task on the left, asking about the path_relative_to_include() function, and a code editor on the right displaying a Terraform configuration file with AWS VPC settings.
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.
In your root Terraform stack/terragrunt.hcl, add:
  1. List your S3 buckets to find the Terraform state bucket:
  2. Reinitialize Terragrunt and confirm updating the backend:

3. Define Common Locals

To DRY out your configuration, define shared values at the root:
Validate once more:

4. Generate Provider and Terraform Version Files

Leverage Terragrunt’s generate blocks to auto-create providers.tf and a Terraform version override:
Re-run initialization:

5. Include Root Configuration in the VPC Module

In Terraform stack/vpc/terragrunt.hcl, inherit root settings:
The image shows a split-screen view with a task description on the left about setting up an include block in Terragrunt, and a code editor on the right displaying a Terraform configuration file.
Apply the VPC module:
Verify the new VPC in the AWS Console.

6. Deploy EC2 Web Module with VPC Dependency

Create Terraform stack/ec2-web/terragrunt.hcl and reference the VPC outputs:
The image shows a split-screen view of a coding environment. On the left, there's a task description about setting dependencies for an EC2 web module, and on the right, there's a code editor displaying Terraform configuration files.
Initialize and apply:

7. Add Database Dependency to the Web Module

If you have a database module under Terraform stack/ec2-database, ensure it deploys before the web server:
Re-run in ec2-web:
Terragrunt will sequence: VPC → Database → Web.

8. Deploy All Modules Simultaneously

From the root of Terraform stack, run:
This command orchestrates every module in dependency order.

9. Verify Remote State Files in S3

Check that each module has its own state file:
Expected output:

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!

Watch Video

Practice Lab