DevOps

5 Mins Read

Getting Started with Terragrunt: A Beginner’s Guide to Streamlined Terraform Management

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
Know More

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:
  • Package Manager Installation:
    • Windows: Install using Chocolatey:
    • macOS: Install using Homebrew:
    • Linux: Install using a package manager like Homebrew or Pacman for Arch Linux:
  • Verification: Confirm the installation by running:

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:

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:

Expected Output

  • Plan the Infrastructure Changes:

This command checks your configuration and shows what changes will be made to your infrastructure.

Expected Output

  • Apply the Configuration:

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:

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

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:

Include the common environment variables in your terragrunt.hcl:

7. Handling Inputs and Variable Precedence

Terragrunt follows a specific order for handling inputs, similar to Terraform. The order of precedence is:

  1. Environment variables
  2. Terraform configuration files (terraform.tfvars)
  3. JSON configuration files (terraform.tfvars.json)
  4. Auto-loading files (.auto.tfvars and .auto.tfvars.json)
  5. 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
Read More

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 PartnerAWS Migration PartnerAWS Data and Analytics PartnerAWS DevOps Competency PartnerAWS GenAI Competency PartnerAmazon QuickSight Service Delivery PartnerAmazon EKS Service Delivery Partner AWS Microsoft Workload PartnersAmazon EC2 Service Delivery PartnerAmazon ECS Service Delivery PartnerAWS Glue Service Delivery PartnerAmazon Redshift Service Delivery PartnerAWS Control Tower Service Delivery PartnerAWS WAF Service Delivery Partner and many more.

To get started, go through our Consultancy page and Managed Services PackageCloudThat’s offerings.

WRITTEN BY Mehar Nafis

Share

Comments

    Click to Comment

Get The Most Out Of Us

Our support doesn't end here. We have monthly newsletters, study guides, practice questions, and more to assist you in upgrading your cloud career. Subscribe to get them all!