terragrunt cannot access module outputs

3.3k Views Asked by At

I'm struggling with Terragrunt (I'm still quite new). I can describe my problem even using pure Terragrunt repo examples:

Looking here(https://github.com/gruntwork-io/terragrunt-infrastructure-live-example/tree/master/prod/us-east-1/prod/webserver-cluster) we can see terragrunt.hcl that imports a module asg-elb-service taken from particular URL (also terragrunt example)

Now my point is that everything is fine untill module solves all my needs. But using mentioned example let's say that I want to add something on top of this module (e.g listener rule for ALB or anything) - then I would like to rely on module outputs and as we can check "used" module exposes those: outputs (https://github.com/gruntwork-io/terragrunt-infrastructure-modules-example/blob/master/asg-elb-service/outputs.tf)

But how even if I add tf file inside my structure - continuing my example, it would be something like: potentialy a new file

I'm just not able to anyhow "interpolate" and get access to those outputs from module :(

1

There are 1 best solutions below

0
On

terragrunt is a thin wrapper that just provides some extra tools for configuration. terragrunt is used to make management of multiple terraform modules easier, it takes care about remote state and so on. But it does not extend terraform modules by adding some functionality on top of it.

Coming back to your example, common approach is to create a new terraform module, probably on top of the existing one and add missing functionality there. You should consider terraform module as a function that does particular job on a certain level of abstraction. With that said, it's completely valid to create modules that use another modules. Consider following example: you need to provision an infrastructure that can send Slack notifications if AWS CloudWatch alarm is triggered. To simplify it a little bit, let's imagine, that Alarm is already created. The missing part is a Lambda function that will send notification, SNS topic that will trigger Lambda function.

This is something, that can be created using terraform module, but under the hood it will most probably rely on another terraform modules (one that provisions Lambda and another one that provisions SNS topic). Those "internal" modules are on another level of abstraction and you still can reuse them in other cases individually. Pseudo code might look like this:

module "sns_topic" {
  source = "git::https://github.com/..."

  name  = "trigger_lambda_to_send_notification_to_slack"
}

module "labmda_function" {
  source = "git::https://github.com/..."
  
  name   = "SendMessageToSlack"
  ...
}

# Invoke Lambda by SNS
resource "aws_sns_topic_subscription" "sns_subscriptions" {
  endpoint      = module.labmda_function.lambda_endpoint # this is how you reference module output
  protocol      = "lambda"
  topic_arn     = module.sns_topic.sns_topic_arn
}

And then, you can simply use this module in terragrunt.