🐶
Terraform

Using Terragrunt with Terraform Cloud: A Guide

By Ondřej Dolanský on 01/06/2025

Learn how to leverage the power of Terragrunt's modularity and code reusability within the collaborative environment of Terraform Cloud.

Using Terragrunt with Terraform Cloud: A Guide

Table of Contents

Introduction

This guide outlines the process of integrating Terragrunt with Terraform Cloud for streamlined infrastructure management. We'll cover installing Terragrunt, configuring the Terraform Cloud backend, setting up credentials, and executing Terragrunt commands. By following these steps, you can leverage Terragrunt's enhanced capabilities while centralizing your Terraform state in Terraform Cloud.

Step-by-Step Guide

  1. Install Terragrunt: Download and install Terragrunt on your local machine.

    brew tap gruntwork-io/terragrunt && brew install terragrunt
  2. Configure Terraform Cloud Backend: In your Terragrunt configuration file (terragrunt.hcl), specify the Terraform Cloud backend.

    remote_state {
      backend = "remote"
      config = {
        hostname     = "app.terraform.io"
        organization = "your-organization"
        workspaces {
          name = "your-workspace"
        }
      }
    }
  3. Set Terraform Cloud Credentials: Configure your Terraform Cloud API token as an environment variable.

    export TF_API_TOKEN="your-terraform-cloud-token"
  4. Run Terragrunt Commands: Execute Terragrunt commands as usual. Terragrunt will use the configured Terraform Cloud backend for state management.

    terragrunt apply

Explanation:

  • Terragrunt acts as a wrapper around Terraform, extending its functionality.
  • By configuring the Terraform Cloud backend in terragrunt.hcl, Terragrunt delegates state management to Terraform Cloud.
  • Your Terraform Cloud credentials, stored in the TF_API_TOKEN environment variable, authenticate Terragrunt to interact with your Terraform Cloud workspace.
  • When you run Terragrunt commands, it communicates with Terraform Cloud to store and retrieve state data, enabling seamless integration.

Note:

  • Replace placeholders like your-organization and your-workspace with your actual values.
  • Ensure that your Terraform Cloud workspace is created before running Terragrunt.
  • Terragrunt enhances Terraform's capabilities, providing features like remote state management, code reusability, and workflow automation.

Code Example

This codebase demonstrates a simple infrastructure setup using Terragrunt and Terraform Cloud. It includes a Terragrunt configuration file (terragrunt.hcl) that defines the source Terraform module and configures the Terraform Cloud backend. An example environment directory (environments/dev) contains a main.tf file with a sample resource to be deployed. The setup utilizes a modular approach, pulling a Terraform module from a Git repository and leverages Terraform Cloud for state management.

├── environments
│   └── dev
│       └── main.tf
└── terragrunt.hcl

1. terragrunt.hcl:

terraform {
  source = "git::https://github.com/your-organization/your-terraform-modules//module-name?ref=v0.1.0"
}

remote_state {
  backend = "remote"
  config = {
    hostname     = "app.terraform.io"
    organization = "your-organization"
    workspaces {
      name = "your-workspace"
    }
  }
}

2. environments/dev/main.tf:

# Example resource to be deployed
resource "random_pet" "this" {
  length = 2
}

Explanation:

  • terragrunt.hcl:
    • Defines the Terraform module to be used.
    • Configures the Terraform Cloud backend with your organization, workspace, and hostname.
  • environments/dev/main.tf:
    • Contains the actual Terraform code for your resources. This is a simple example using the random_pet resource.

Steps:

  1. Install Terragrunt:

    brew tap gruntwork-io/terragrunt && brew install terragrunt
  2. Set Terraform Cloud Credentials:

    export TF_API_TOKEN="your-terraform-cloud-token"
  3. Navigate to the environment directory:

    cd environments/dev
  4. Run Terragrunt Commands:

    terragrunt init  # Initialize Terragrunt and download modules
    terragrunt plan  # Preview changes
    terragrunt apply # Apply changes to your infrastructure

Key Points:

  • This example uses a modular approach, pulling a Terraform module from a Git repository.
  • You can create multiple environment directories (e.g., staging, prod) with different configurations.
  • Terragrunt will automatically handle state management in your Terraform Cloud workspace.

This example provides a basic structure for using Terragrunt with Terraform Cloud. You can adapt it to your specific needs and project requirements.

Additional Notes

Prerequisites:

  • Terraform Installation: Ensure you have Terraform installed on your local machine.
  • Terraform Cloud Account: A Terraform Cloud account is required for remote state management.
  • API Token Generation: Generate a Terraform Cloud API token with appropriate permissions for your workspace.

Best Practices:

  • Version Control: Store your Terragrunt configuration files (terragrunt.hcl) and Terraform code in a version control system (e.g., Git) for better collaboration and tracking.
  • Modular Design: Utilize Terraform modules to promote code reusability and maintainability.
  • Environment-Specific Configurations: Create separate directories for different environments (e.g., dev, staging, prod) and customize configurations accordingly.
  • Secrets Management: Avoid storing sensitive information directly in configuration files. Use environment variables or a dedicated secrets management solution.
  • Testing: Implement automated tests to validate your infrastructure code and prevent regressions.

Troubleshooting:

  • Authentication Issues: Double-check your Terraform Cloud API token and ensure it has the necessary permissions.
  • Workspace Availability: Verify that the specified Terraform Cloud workspace exists and is accessible.
  • Network Connectivity: Ensure your machine has network connectivity to Terraform Cloud.
  • Terragrunt Version Compatibility: Use compatible versions of Terragrunt and Terraform Cloud.

Additional Considerations:

  • Terragrunt Features: Explore other Terragrunt features like dependency management, before and after hooks, and custom commands to further enhance your workflow.
  • Terraform Cloud Workspaces: Consider using Terraform Cloud workspaces to isolate environments and manage permissions effectively.
  • Alternative Backends: While this guide focuses on Terraform Cloud, Terragrunt supports other remote state backends like AWS S3 and HashiCorp Consul.

This comprehensive guide provides a solid foundation for integrating Terragrunt with Terraform Cloud, enabling you to streamline your infrastructure management processes and leverage the power of both tools effectively.

Summary

This guide outlines how to configure Terragrunt to leverage Terraform Cloud for managing your infrastructure's state files.

Steps:

  1. Install Terragrunt: Use the following command to install Terragrunt via Homebrew:

    brew tap gruntwork-io/terragrunt && brew install terragrunt
  2. Configure Terraform Cloud Backend: In your terragrunt.hcl file, define the remote_state block to point to your Terraform Cloud workspace:

    remote_state {
      backend = "remote"
      config = {
        hostname     = "app.terraform.io"
        organization = "your-organization"
        workspaces {
          name = "your-workspace"
        }
      }
    }

    Remember to replace placeholders with your actual organization and workspace names.

  3. Set Terraform Cloud Credentials: Set your Terraform Cloud API token as an environment variable:

    export TF_API_TOKEN="your-terraform-cloud-token"
  4. Run Terragrunt Commands: Execute Terragrunt commands like terragrunt apply. Terragrunt will automatically use the configured Terraform Cloud backend for state management.

Key Points:

  • Terragrunt extends Terraform's functionality, simplifying remote state management.
  • Configuring the Terraform Cloud backend in terragrunt.hcl enables Terragrunt to store and retrieve state data from your Terraform Cloud workspace.
  • Your Terraform Cloud API token, stored in the TF_API_TOKEN environment variable, authenticates Terragrunt to interact with your Terraform Cloud account.

Before you begin:

  • Ensure you have a Terraform Cloud account and a workspace created for your project.
  • Replace all placeholders in the code examples with your specific values.

Conclusion

By following these steps, you can leverage Terragrunt's enhanced capabilities while centralizing your Terraform state in Terraform Cloud, leading to a more streamlined and efficient infrastructure management workflow. This approach not only simplifies remote state management but also promotes code reusability, enforces consistency across environments, and integrates seamlessly with Terraform Cloud's collaboration and automation features. As you delve deeper into Terragrunt's capabilities, you can explore additional features and best practices to further optimize your infrastructure as code processes.

References

Were You Able to Follow the Instructions?

😍Love it!
😊Yes
😐Meh-gical
😞No
🤮Clickbait