terraformterragrunt

Can I create variables in terragrunt based on the dependencies outputs?


Let us assume we have a terragrunt.hcl file that looks like this

terraform {
  source = "${dirname(find_in_parent_folders(".root"))}/terraform/modules/cloudwatch-alarms"
}

dependency "ec2-instance" {
  config_path = "../instance"
}

include "root" {
  path   = find_in_parent_folders()
  expose = true
}

locals {
  alerts_sns_topic_arn = "arn:aws:sns:eu-central-1:secret:alerts"
}

inputs = {
  alarms = {
    "high-cpu-notify" = {
      alarm_name          = "awsec2-${dependency.ec2-instance.outputs.tags_all["Name"]}-CPUUtilization-Notify"
      namespace           = "AWS/EC2"
      metric_name         = "CPUUtilization"
      comparison_operator = "GreaterThanThreshold"
      evaluation_periods  = "1"
      period              = "300"
      statistic           = "Average"
      threshold           = "20"
      alarm_actions = [
        local.alerts_sns_topic_arn,
      ]

      dimensions = {
        InstanceId = dependency.ec2-instance.outputs.id
      }
    },
    "cpu-very-high-notify" = {
      alarm_name          = "awsec2-${dependency.ec2-instance.outputs.tags_all["Name"]}-CPUUtilization-Restart"
      namespace           = "AWS/EC2"
      metric_name         = "CPUUtilization"
      comparison_operator = "GreaterThanOrEqualToThreshold"
      evaluation_periods  = "1"
      period              = "300"
      statistic           = "Average"
      threshold           = "60"
      alarm_actions = [
        local.alerts_sns_topic_arn,        
      ]

      dimensions = {
        InstanceId = dependency.ec2-instance.outputs.id
      }
    },
  }
}

I am creating 2 cloudwatch alerts with a custom terraform module that I created.

I am reusing this variable ${dependency.ec2-instance.outputs.tags_all["Name"]} coming from the dependency in the 2 alarms for defining the alarm_name input.

My question is:

Is it possible to create a variable in terragrunt that uses the dependencies outputs and reuse it while defining the inputs ?

I tried the following without success

  1. Defining a new variable into the locals like identifier = "${dependency.ec2-instance.outputs.tags_all["Name"]}-someotherstuff" This gave error that you cannot use dependencies inside locals block
  2. Doing the same thing as above but within the inputs block and then trying to use that variable like ${inputs.identifier}. This didn't work and got error no variable inputs exist

Solution

  • There is a workaround that I have seen previously. You can have a file deps.hcl.

    dependency "ec2-instance" {
      config_path = "../instance"
    }
    

    Then in your terragrunt.hcl:

    terraform {
      source = "${dirname(find_in_parent_folders(".root"))}/terraform/modules/cloudwatch-alarms"
    }
        
    dependency "ec2-instance" {
      config_path = "../instance"
    }
        
    include "root" {
      path   = find_in_parent_folders()
      expose = true
    }
        
    locals {
      alerts_sns_topic_arn = "arn:aws:sns:eu-central-1:secret:alerts"
      deps = read_terragrunt_config(find_in_parent_folders("deps.hcl"))
      identifier = local.deps.dependency.ec2-instance.outputs.tags_all["Name"]
    }