Keep your AWS credentials handy. Run the following at any time to fetch them:
Prerequisites
Before you begin, confirm you have the following tools installed:| Tool | Minimum Version | Install / Docs |
|---|---|---|
| Terraform | v1.8.3+ | https://www.terraform.io/downloads.html |
| Terragrunt | v0.58.8+ | https://terragrunt.gruntwork.io/docs/getting-started/install/ |
| AWS CLI | v2.x | https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html |
Project Setup
- Clone or navigate to your Terragrunt root.
-
Create a working demo directory:
Step 1: Account Configuration
Createterragrunt-stack/demo/account.hcl to define account-level locals:
terragrunt.hcl, add remote state and provider generation:
Step 2: Region Configuration
-
Create
terragrunt-stack/demo/us-east-1/region.hcl: -
Update root
terragrunt.hcl:
Step 3: Environment Configuration (Dev)
-
Create
terragrunt-stack/demo/us-east-1/dev/env.hcl: -
Add to root
terragrunt.hcl:
Step 4: VPC Module
File:terragrunt-stack/demo/us-east-1/dev/vpc/terragrunt.hcl
Step 5: Key Pair & Security Group Modules
Key Pair
terragrunt-stack/demo/us-east-1/dev/key-pair/terragrunt.hcl:
Security Group
terragrunt-stack/demo/us-east-1/dev/security-group/terragrunt.hcl:
Step 6: EC2 Module
Ensuremodules/ec2/main.tf contains:
terragrunt-stack/demo/us-east-1/dev/ec2/terragrunt.hcl:
Step 7: Apply the Dev Environment
Run all modules in order fromterragrunt-stack/demo/us-east-1:
- Key Pair
- VPC
- Security Group
- EC2
Step 8: Stage Environment
Duplicate thedev folder to stage and apply:
stage directory mirrors dev in your editor.
Step 9: Production in us-west-2
-
Copy
us-east-1→us-west-2, removedev, renamestage→prod. -
In
prod/ec2/terragrunt.hcl, update: -
Apply prod:
Step 10: Review Remote State Files
List state files in your S3 bucket:Congratulations! You’ve successfully built a modular, scalable AWS infrastructure with Terragrunt.