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.

Freedom Month Sale — Upgrade Your Skills, Save Big!

  • Up to 80% OFF AWS Courses
  • Up to 30% OFF Microsoft Certs
Act Fast!

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.

Freedom Month Sale — Discounts That Set You Free!

  • Up to 80% OFF AWS Courses
  • Up to 30% OFF Microsoft Certs
Act Fast!

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

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!