Meet101
Meet101

Reputation: 801

is it possible to call multiple terraform modules using terragrunt

I'm trying to call multiple modules from terragrunt. I understand that currently, terragrunt doesn't support multiple sources and we can only call one module at a time. So, I created a main.tf file to frontend multiple modules.

# main.tf 

module "foo" {
   source = "../modules/vpc"
}


module "bar" {
   source = "../modules/s3"
}

Inside terragrunt calling main.tf as a source, thinking that will call module foo and module bar.

# terragrunt.hcl

terraform {
  source = "./main.tf

  }


inputs {
}

Is this possible using terragrunt? I need to group multiple terraform modules at once.

Upvotes: 11

Views: 27288

Answers (2)

melcuturbat
melcuturbat

Reputation: 23

I have the following structure.

project    
│
└───live
│   │
│   └───dev
│       │   terragrunt.hcl
│       │   
│       └───aws-ec2
|       |      terragrunt.hcl   
|       └───aws-rds
|              terragrunt.hcl
│   
└───modules
    │   
    └───ec2
    │      files.tf
    └───rds
           files.tf

In dev I am running terragrunt plan-all (which is deprecated, but works if you do not mind the errors caused by dependencies). The latest cli option is terragrunt run-all (command), but I think you need a terraform file in addition to your terragrunt file.

More info can be found here:

Upvotes: 2

Casey
Casey

Reputation: 6326

In short, yes, the two files snippets you've posted would work.

terragrunt doesn't support multiple sources and we can only call one module at a time.

Longer answer: It's useful to think of the terraform { ... } block in your terragrunt.hcl as a pointer to a "root terraform module". This root module is just any other terraform module, but is special because it is at the root, or the top, of all your terraform config.

So terragrunt only supports one root module, but that root module can use as many additional modules as you need.

The power terragrunt gives us is the ability to re-use these root modules. In vanilla terraform you cannot re-use this root modules.

In your example the terragrunt file is pointing to a root module in the same file (./main.tf). This works just fine, but because we use terragrunt to keep things DRY we would normally put this root module in a different directory, perhaps even in a git repo and reference it appropriately in the terragrunt.hcl file

Here is a quick diagram:

         +-------------------------------+
         |       /my/root-module         |
         |                               |
         |           main.tf             |
         |                               |
         +--------------+----------------+
+------------------+    |    +----------------+
|  /my/modules/vpc |    |    | /my/modules/s3 |
|                  |    |    |                |
|   module "foo"   <----+---->  module "bar"  |
|                  |         |                |
|                  |         |                |
+------------------+         +----------------+

Not shown is the terragrunt.hcl file that would point to /my/root-module, this can be somewhere else on disk or in git.

Upvotes: 15

Related Questions