How to Avoid Hardcoding Providers in Terraform Using Terragrunt’s Generate Block

Terraform Providers:

         plugins  that interact with cloud providers, SaaS providers, and other APIs. Each provider adds a set of resource types and/or data sources that Terraform can manage. Without providers, Terraform can't manage any kind of infrastructure.

All Terraform configurations must declare which providers they require so that Terraform can install and use them


Provider Configuration




Sample Example of Provider Declaration

In many Terraform setups especially in growing teams . it's common to see each module define its own provider block. It often looks like this:
  
terraform {
    required_providers {
      incus = {
        source  = "lxc/incus"
        version = "0.3.1"
    }
  }

As infrastructure evolves the need of multiple modules becomes inevitable . Each of the modules if following the hardcoded approach includes its own provider block. That means every new module brings new opportunity for inconsistency, duplication and drift

Cons of Hardcoding Providers Block in every module?

  1. Duplication: Violates the DRY principle
  2. Configuration Drift
  3. Tightly coupled modules
  4. Scalability Problems
  5. Risks of Version conflicts
  6. State inconsistencies

How do we solve it ?

In this demo, I walk through how to centralize your provider configuration using Terragrunt’s generate block, eliminating the need to hardcode provider blocks in each of your Terraform modules. We’ll work with two repositories:

Terraform Modules Walk Through



Terragrunt Provider block injection


Provision and Destroy


This blog concludes the series on writing DRY Terraform with Terragrunt. By breaking infrastructure into reusable modules, injecting providers dynamically, and managing everything through Terragrunt, you get a scalable, clean, and production-ready workflow.

Feel free to explore the repos, try it out yourself, and drop any questions or feedback in the comments!

















Popular posts from this blog

Certification for Kubernetes? I got it covered.

Continuous Integrations Concepts and Demo

Containers and Virtual Machines Explained || Containerization is new virtualization || DevOps