Terragrunt defining a value in .hcl that is to be used by another file

195 Views Asked by At

I am trying to create a terragrunt structure (I am a bit of a newbie) for a few "apps", here is how my structure looks like:

├── apps
│   ├── app3
│   │   ├── dev
│   │   │   ├── backend.tf
│   │   │   ├── provider.tf
│   │   │   └── terragrunt.hcl
│   │   └── terragrunt.hcl
│   ├── core-api
│   │   ├── dev
│   │   │   ├── backend.tf
│   │   │   ├── provider.tf
│   │   │   └── terragrunt.hcl
│   │   └── terragrunt.hcl
│   └── terragrunt.hcl
├── envrc
└── terragrunt-configs
    ├── config.hcl
    └── envs
        ├── default.yaml
        ├── dev
        │   └── default.yaml
        └── staging
            └── default.yaml

I run terragrunt from each app/env folder, for instance apps/app3/dev. Now, on one app terragrun I need to define a value that is to be used by another file. I say value, because I do not mind if this is a valiable, locals or other things. Here is how my base file looks like in apps/app3/dev/terragrunt.hcl:

include "config" {
  path = find_in_parent_folders("/terragrunt-configs/config.hcl")
}

include "thisappcommon" {
  path = "../terragrunt.hcl"
  merge_strategy = "deep"
}

include "allappscommon" {
  path = "../../terragrunt.hcl"
  merge_strategy = "deep"
}

inputs = {
  datadog_monitors = {}
}

I have tried to add a locals here witht he variable I want to use somewher else. something like locals { service = "app3-x-y" }. then call it from another file like this this is another file in apps/terragrunt.hcl:

terraform {
  source = "git::[email protected]:some-repo//monitors"
}

locals {
  env_dir = "${basename(path_relative_to_include())}"
  location_dir = "${basename(dirname(path_relative_to_include()))}"
  extra_atlantis_dependencies = [
    "${get_parent_terragrunt_dir()}/../terragrunt-configs/envs/default.yaml",
    "${get_parent_terragrunt_dir()}/../terragrunt-configs/envs/${local.env_dir}/default.yaml",
  ]

  env_vars = merge(
    yamldecode(file(local.extra_atlantis_dependencies[0])),
    yamldecode(file(local.extra_atlantis_dependencies[1])),
  )
}

inputs = {
  datadog_monitors = {
    "monitor1" = {
      name                     = "[kubernetes] Monitor Kubernetes Pods Restarting for app ${local.location_dir} on env ${local.env_vars.short_env}"
      type                     = "query alert"
      query                    = "change(sum(last_5m),last_5m):exclude_null(avg:kubernetes.containers.restarts{*} by {kube_cluster_name,pod_name}) > 5"
(...)
      include_tags             = true
      // tags
      tags = {
        app         = "${local.location_dir}"
        type        = "daemon"
        env         = "${local.env_vars.short_env}"
        env_name    = "${local.env_vars.environment}"
        integration = "kubernetes"
        // for instance here I would like to have a service TAG coming from the other file. 
    //something like service = "${local.service}". but it does not work of course.  
        service     = "${local.service}"
      }
      thresholds = {
        warning           = 3
        critical          = 5
      }
      // Optional parameters
(...)
    }
  }
}

I tried to add it in input in one file and use in the other and a few other things.

Any ideas if is possible to create this variable/local/value in one terragrunt.hcl and use in the other and how?

Edit, the error is some for of "Unsupported attribute; This object does not have an attribute named "service"."

0

There are 0 best solutions below