Voiced by Amazon Polly |
Introduction
Terraform is a powerful tool for Infrastructure as Code (IaC), allowing users to manage cloud infrastructure efficiently. However, as your projects grow and you manage multiple environments like Development, Testing, Staging, and Production, you may encounter challenges with Terraform, such as code redundancy and the need for manual updates. This is where Terragrunt, a wrapper for Terraform, comes into play. Terragrunt helps mitigate these issues by promoting code reuse and simplifying infrastructure management.
This beginner’s guide will help you get started with Terragrunt, using a practical example of setting up an EC2 instance for Development and Test environments.
Freedom Month Sale — Upgrade Your Skills, Save Big!
- Up to 80% OFF AWS Courses
- Up to 30% OFF Microsoft Certs
1. Installing Terragrunt
Before you install Terragrunt, you must have Terraform installed, as Terragrunt operates as an additional layer on top of Terraform.
Installation Methods:
- Manual Installation: Download the Terragrunt binary from the Terragrunt GitHub releases page. Choose the appropriate binary for your operating system, rename it to terragrunt, make it executable, and move it to /usr/local/bin on Linux or macOS:
1 2 3 |
chmod u+x terragrunt mv terragrunt /usr/local/bin/ |
- Package Manager Installation:
- Windows: Install using Chocolatey:
1 |
choco install terragrunt |
-
- macOS: Install using Homebrew:
1 |
brew install terragrunt |
-
- Linux: Install using a package manager like Homebrew or Pacman for Arch Linux:
1 |
pacman -S terragrunt |
- Verification: Confirm the installation by running:
1 |
terragrunt --version |
2. Creating Your First Terragrunt Configuration
After installing Terragrunt, the next step is to create a Terragrunt configuration to manage your cloud infrastructure.
Understanding Terragrunt Basics
Terragrunt emphasizes keeping your Terraform code DRY (Don’t Repeat Yourself). It encourages the use of modules and promotes a clear, organized file structure.
Example Project Structure:
We will set up a simple project to create an EC2 instance in DEV, TEST and PROD environments using Terragrunt.
Your basic directory structure should look like below
Writing Your terragrunt.hcl Configuration
Below is a basic example of a terragrunt.hcl file for the DEV environment:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
terraform { source = "tfr:///terraform-aws-modules/ec2-instance/aws?version=4.0.0" } generate "provider" { path = "provider.tf" if_exists = "overwrite_terragrunt" contents = <<EOF provider "aws" { profile = "default"s region = "eu-central-1" shared_credentials_file = "/path/to/credentials" } EOF } inputs = { ami = "ami-0767046d1677be5a0" tags = { Name = "Terragrunt Tutorial: EC2" } } |
This code snippet defines a configuration using Terragrunt to deploy an EC2 instance on AWS. It leverages a Terraform module and dynamically generates a provider configuration. Let’s break it down:
- terraform block
-
- The terraform block specifies that Terragrunt should use the Terraform module for creating an EC2 instance.
- The source URL refers to a versioned module (version=4.0.0) from the Terraform registry (terraform-aws-modules/ec2-instance/aws). This module contains the logic for provisioning EC2 instances.
- generate “provider” block
-
- This block dynamically generates a file named provider.tf containing the AWS provider configuration for Terraform.
- It ensures that if the provider.tf file already exists, it will be overwritten (if_exists = “overwrite_terragrunt”).
- Inside the generated file, the AWS provider is configured with:
- profile = “default”: Specifies the default profile from AWS credentials.
- region = “eu-central-1”: The AWS region is set to eu-central-1 (Frankfurt).
- shared_credentials_file = “/path/to/credentials”: Specifies the path to the AWS credentials file.
- inputs block
-
- The inputs block contains the values passed to the Terraform module defined earlier.
- ami = “ami-0767046d1677be5a0”: Specifies the Amazon Machine Image (AMI) ID for the EC2 instance.
- tags = { Name = “Terragrunt Tutorial: EC2” }: Adds a tag with the name “Terragrunt Tutorial: EC2” to the EC2 instance for identification.
3. Running Terragrunt Commands
To initialize and apply your Terragrunt configuration, navigate to the directory containing your terragrunt.hcl and run:
- Initialize Terragrunt:
1 |
terragrunt init |
Expected Output
- Plan the Infrastructure Changes:
1 |
terragrunt plan |
This command checks your configuration and shows what changes will be made to your infrastructure.
Expected Output
- Apply the Configuration:
1 |
terragrunt apply |
This command applies the changes and creates the resources defined in your configuration.
Expected Output
4. Verifying AWS Resources
You can verify the created EC2 instance in two ways:
- AWS Management Console: Log in to the AWS console and navigate to the EC2 dashboard to see your running instances.
- Terragrunt Output Command:
1 |
terragrunt output |
This command displays the details of the resources managed by Terragrunt.
Expected Output
5. Cleaning Up Resources
To delete the resources you’ve created, run
1 |
terragrunt destroy |
This command destroys the infrastructure managed by Terragrunt.
Expected Output
6. Managing Locals and Values Files
Terragrunt allows you to use local values and external files to manage input variables efficiently. This is particularly useful when managing multiple environments, such as DEV ,TEST and PROD to ensure configurations are environment-specific and avoid code duplication.
Example:
Create a common file common-environment.yaml and specific files like dev-environment.yaml and test-environment.yaml for environment-specific variables.
Locals Example:
1 2 3 4 5 |
# common-environment.yaml locals: instance_type: "t2.micro" |
Include the common environment variables in your terragrunt.hcl:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
locals { env_vars = yamldecode( file("${find_in_parent_folders("common-environment.yaml")}") ) } inputs = { ami = "ami-0767046d1677be5a0" instance_type = local.env_vars.locals.instance_type tags = { Name = "Terragrunt Tutorial: EC2" } } |
7. Handling Inputs and Variable Precedence
Terragrunt follows a specific order for handling inputs, similar to Terraform. The order of precedence is:
- Environment variables
- Terraform configuration files (terraform.tfvars)
- JSON configuration files (terraform.tfvars.json)
- Auto-loading files (.auto.tfvars and .auto.tfvars.json)
- Command line variables (-var and -var-file)
By understanding this precedence, you can better manage how variables are set and overridden in your Terragrunt configurations.
8. Conclusion
Terragrunt is a powerful tool that enhances Terraform by simplifying configuration management and promoting code reuse. By following this guide, you should now have a basic understanding of how to install Terragrunt, create configurations, and manage AWS resources efficiently.
Freedom Month Sale — Discounts That Set You Free!
- Up to 80% OFF AWS Courses
- Up to 30% OFF Microsoft Certs
About CloudThat
CloudThat is an award-winning company and the first in India to offer cloud training and consulting services worldwide. As a Microsoft Solutions Partner, AWS Advanced Tier Training Partner, and Google Cloud Platform Partner, CloudThat has empowered over 850,000 professionals through 600+ cloud certifications winning global recognition for its training excellence including 20 MCT Trainers in Microsoft’s Global Top 100 and an impressive 12 awards in the last 8 years. CloudThat specializes in Cloud Migration, Data Platforms, DevOps, IoT, and cutting-edge technologies like Gen AI & AI/ML. It has delivered over 500 consulting projects for 250+ organizations in 30+ countries as it continues to empower professionals and enterprises to thrive in the digital-first world.

WRITTEN BY Mehar Nafis
Comments