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.
Customized Cloud Solutions to Drive your Business Success
- Cloud Migration
- Devops
- AIML & IoT
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.
Get your new hires billable within 1-60 days. Experience our Capability Development Framework today.
- Cloud Training
- Customized Training
- Experiential Learning
About CloudThat
CloudThat is a leading provider of Cloud Training and Consulting services with a global presence in India, the USA, Asia, Europe, and Africa. Specializing in AWS, Microsoft Azure, GCP, VMware, Databricks, and more, the company serves mid-market and enterprise clients, offering comprehensive expertise in Cloud Migration, Data Platforms, DevOps, IoT, AI/ML, and more.
CloudThat is the first Indian Company to win the prestigious Microsoft Partner 2024 Award and is recognized as a top-tier partner with AWS and Microsoft, including the prestigious ‘Think Big’ partner award from AWS and the Microsoft Superstars FY 2023 award in Asia & India. Having trained 650k+ professionals in 500+ cloud certifications and completed 300+ consulting projects globally, CloudThat is an official AWS Advanced Consulting Partner, Microsoft Gold Partner, AWS Training Partner, AWS Migration Partner, AWS Data and Analytics Partner, AWS DevOps Competency Partner, AWS GenAI Competency Partner, Amazon QuickSight Service Delivery Partner, Amazon EKS Service Delivery Partner, AWS Microsoft Workload Partners, Amazon EC2 Service Delivery Partner, Amazon ECS Service Delivery Partner, AWS Glue Service Delivery Partner, Amazon Redshift Service Delivery Partner, AWS Control Tower Service Delivery Partner, AWS WAF Service Delivery Partner and many more.
To get started, go through our Consultancy page and Managed Services Package, CloudThat’s offerings.
WRITTEN BY Mehar Nafis
Click to Comment