I have inherited a code base which deploys terraform modules via terragrunt. I need to define the dependencies between them so that I can run the whole stack at one, and all the modules will be deployed in order.
In order to achieve this I have defined 3 sets of resources that need to be applied. I will cd into it and run terragrunt -run-all
on them in sequence.
My directory looks like this:
├── environments/
│ ├── _env/
| | |-- loganalytics.hcl (see code example 1)
| | |-- resourcegroups.hcl (see code example 2)
│ ├── uva-test/
| | |-- basis/ (terragrunt -run-all is started here)
| | | |-- resourcegroups/
| | | | |-- terragrunt.hcl (see code example 3)
| | | |-- loganalytics/
| | | |-- terragrunt.hcl (see code example 4)
| | |-- env.hcl (contains locals)
| | |-- resourcegroups.hcl (contains locals)
| | |-- loganalytics.hcl (contains locals)
│ └── terragrunt.hcl (main terragrunt file with provider definition, remote state ref etc)
├── modules/
│ ├── loganalytics/
│ ├── resourcegroup/
Example 1 loganalytics.hcl:
terraform {
source = find_in_parent_folders("modules/loganalytics")
}
locals {
env_vars = read_terragrunt_config(find_in_parent_folders("/uva-test/env.hcl"))
}
dependency "resource_group" {
config_path = "${get_parent_terragrunt_dir()}/resourcegroups.hcl"
skip_outputs = true
}
inputs = {
azure_subscription_id = local.env_vars.locals.azure_subscription_id
resource_group_name = local.env_vars.locals.resource_group_name
log_analytics_workspace_name = local.env_vars.locals.log_analytics_workspace_name
log_analytics_workspace_name_audit = local.env_vars.locals.log_analytics_workspace_name_audit
}
Example 2 resourcegroups.hcl:
terraform {
source = find_in_parent_folders("modules/resourcegroup")
}
locals {
env_vars = read_terragrunt_config(find_in_parent_folders("/uva-test/env.hcl"))
resourcegroups = read_terragrunt_config(find_in_parent_folders("/uva-test/resourcegroups.hcl"))
}
inputs = {
resourcegroups = local.resourcegroups.locals.resourcegroups
azure_subscription_id = local.env_vars.locals.azure_subscription_id
}
Example 3 resourcegroups terragrunt.hcl:
include "root" {
path = find_in_parent_folders()
}
include "env" {
path = "../../../_env/resourcegroups.hcl"
}
Example 4 loganalytics terragrunt.hcl:
include "root" {
path = find_in_parent_folders()
}
include "env" {
path = "../../../_env/loganalytics.hcl"
}
I have 2 questions regarding this code. Is this setup correct or am I missing best practices and is this the correct way of referencing the various files?
Secondly, when running the command it asks to resolve external dependencies. For some reason it takes the _env
folder as an external dependecy, why does this occur?
Because this dependency is showing up everywhere, there is a cycle error like follows:
ERRO[0004] Found a dependency cycle between modules: /home/berend/werk/uva/infra-integratiehub/environments/_env -> /home/berend/werk/uva/infra-integratiehub/environments/_env
ERRO[0004] Unable to determine underlying exit code, so Terragrunt will exit with error code 1
I already fixed one dependency error because in the end, both dependencies were referencing the same module. But is seems to me that this is caused because all modules have the external dependency on /_env