santosh.a
santosh.a

Reputation: 535

need clarity on advantages of terragrunt instead of tfvars file

I'm new to terragrunt and terraform. Right now i'm little confused about why to use terragrunt instead of tfvars file.

My scenario is for one of the application we create multiple resources like webapps, azure sql, storage accounts. This application has multiple environments like dev, qa, prod and also it is deployed in multiple regions like weu, eus, wus2. I tried to parameterise this with tfvars files.

My directory structure will look like:

app1/storageaccount.tf
    /webapp.tf
    /sqldb.tf 
    /main.tf
    /variables.tf
    /dev.tfvars
    /prod-weu.tfvars
    /dev-weu.tfvars
    /qa-eus.tfvars
    .......

I use tfvars file to pass environment, location variables etc. I can even keep state file names, subscription names as variables. So here i'm trying to understand how terragrunt will be more helpful instead of tfvars. I can have only 1 tfvar file for each environment/region instead of multiple hcl files which are created for terragrunt.

Could you please shed some light here?

thanks, Santosh

Upvotes: 2

Views: 1553

Answers (1)

nnsense
nnsense

Reputation: 1636

The short answer is "that's not how terragrunt works", terragrunt radically changes the way you deploy terraform, adding more functionalities and, more importantly, simplifying your code.

Let's start from your deployment:

Even if you have just a bunch of terraform files deploying the exact same thing in different environments or regions, you will need to swap backend.tf files for each deployment, because terraform isn't allowing to use variables in there, which makes your environment dangerously exposed to a wrong apply and here's the first point: Terragrunt allows to configure your backend dynamically, not only with simple variables, but using additional functions provided by terragrunt (eg, your bucket can be set as: "mycorp-${get_aws_account_id()}-tfstate" resulting in mycorp-123456892123-tfstate).

Another cool buil-in function is run_cmd() which, used with a makefile, can allow you to automate the variables setting with any script. I had devs running test deployments, and I've added a variable set to current datetime which was setting a "CreationDatetime" tag..

Then, since it's quite rare to have an infrastructure that small forever, one day someone will need to add something slightly different in one of the environments. Which is where you'll start using something like this to deploy only in test, for example..

count = var.test_env == 1 ? 1 : 0

That is just the beginning, it will happen more and more, prod and staging might survive, dev/test/etc will start drifting every month a bit more, and it usually leads to a very difficult to understand code, with a lot of conditions based on your .rfvar file, really difficult to manage.

So, how can terragrunt help with this, which tfvars can't do?

With terragrunt, you don't use .tf files into your deployment at all. You translate anything that's not a third party module into a module and then you reference the modules into your terragrunt.hcl files in each folder.

Yes, you're going to have multiple folders, that's true, but each folder will usually have just one file, the terragrunt.hcl, setting the inputs for the modules, the modules to deploy, etc.

As you can see, your code will not change, you will just add resources as "placeholders" in each directory. You will be able to reuse the same code and add changes for each environment, without a single change in terraform.

This doesn't mean you can't use terragrunt with tf files, I did that because I needed terragrunt's ability to set the outputs as inputs into a second deployment.

This is keeping your code "DRY", as they say. Plus, you'll get more features, to name a few:

  • Terragrunt adds native support to secrets encrypted with sops
  • It allows to retry a failed deployment
  • you can run commands before or after the deployment
  • etc

I would advise to read this great blog post which is exactly about this

Upvotes: 3

Related Questions