How can I keep a "local" tf module next to/close to the terragrunt.hcl?
Hi, I'm fairly new to terragrunt, so apologies if I'm asking something with an obvious answer. I'm confused by this section in the [example tutorial](https://terragrunt.gruntwork.io/docs/getting-started/quick-start/): ``` stage ├── terragrunt.hcl ├── frontend-app │ ├── main.tf │ └── terragrunt.hcl └── mysql ├── main.tf └── terragrunt.hcl ``` This seems to hint at a capability what I want, but I can't get it to work. I have some terraform modules that are fairly generic and shared over some terragrunt directories. I have them on the same filesystem (not in git). It looks more or less like this: ``` ├── terraform │ └── modules │ ├── application-cache │ │ ├── main.tf │ ├── centralized-alarms │ │ ├── main.tf ... └── terragrunt ├── prodapp │ ├── account.yaml │ ├── appcache │ │ ├── local │ │ │ ├── main.tf │ │ └── terragrunt.hcl ``` For one of terragrunt.hcl files, I need a little bit of local customization in how a shared module (`application-cache`) is called. Specifically, one of the parameters that I just provide manually in the testing environment, instead needs to be read from a cloudformation output. So I have a little bit of local, environment-specific customization around invoking the shared/generic module. My problem is I cannot figure out how to achieve this. I don't want to create another module in the "shared" modules directory which is specifically only serves as an "entry point" module for one specific environment. I'd rather keep the terraform code "close" to the terragrunt.hcl it applies to. I've tried to have the `main.tf` in the same folder as the `terragrunt.hcl` file like the example tutorial, which seems to imply that I must not have a `terraform { source ... }` block in `terragrunt.hcl`. But then the locals are not passed correctly to the variables I declare in the `main.tf` using the `input` block. When I run `terragrunt apply` it asks for the value of a variable that is declared in the `main.tf` and specified in the `terragrunt.hcl` file's `inputs`. I've also tried having a `./local` directory with `./local/main.tf`, attempting to treat it as a terraform module. In the `terragrunt.hcl` file I then specify `terraform { source = ".//local" }` bit this fails with paths to the shared terraform modules not being resolved correctly. ``` | Error: Unreadable module directory │ │ Unable to evaluate directory symlink: lstat ../../../../terraform: no such │ file or directory ``` Really my question is, if I have shared terraform modules, and my environments have slight dissimilarities in how those shared modules are invoked, how must I structure the terragrunt repository to accomplish this? --- <ins datetime="2022-09-19T11:55:36Z"> <p><a href="https://support.gruntwork.io/hc/requests/109257">Tracked in ticket #109257</a></p> </ins>
Hi, AFAIK, it depends on how big are dissimilarities: * can be used `dependency` block and outputs passed to `application-cache` - but it will require to extract terraform code as separated module and reference it in Terragrunt * can be used `generate` block to put custom code in the module directory before the invocation of Terraform References: https://terragrunt.gruntwork.io/docs/reference/config-blocks-and-attributes/#dependency https://terragrunt.gruntwork.io/docs/reference/config-blocks-and-attributes/#generate I also created some examples using `generate` and `dependency` in https://github.com/denis256/terragrunt-tests/tree/master/discussion-559 I hope this helps