# Accessing variable from another module

**URL:** <https://discuss.hashicorp.com/t/accessing-variable-from-another-module/34515>\
**Category:** Terraform\
**Created:** [January 20, 2022, 12:33pm UTC](https://discuss.hashicorp.com/t/accessing-variable-from-another-module/34515 "2022-01-20T12:33:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![umuhammad97](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/umuhammad97/32/25206_2.png) [@umuhammad97](https://discuss.hashicorp.com/u/umuhammad97)\
**Post date:** [January 20, 2022, 12:33pm UTC](https://discuss.hashicorp.com/t/accessing-variable-from-another-module/34515/1 "2022-01-20T12:33:27Z")

</div>

I’m using a module to create aws sns resources, another module to create aws cloudwatch alarm resources, and trying to to access the value of the sns topic arn of the former from the latter.

In modules/sns/outputs.tf I’ve output the sns topic arn that the module creates:

```auto
output "sns_topic_arn" {
    value = aws_sns_topic.sns_topic.arn
}

```

Then in modules/cloudwatch-alarms/main.tf I’ve called a variable declared as “sns\_topic\_arn”:  
`alarm_actions = [var.sns_topic_arn]`

And finally, in the tf file where I’m calling cloudwatch-alarms module (located in a repository where I’m also calling the sns module), I’ve tried to pass in a value for sns\_topic\_arn by attempting to access the output of modules/sns:  
`sns_topic_arn = "${module.sns.sns_topic_arn}"`

But in doing so I get the following error:

```auto
Unsupported attribute.
This object (module.sns) does not have an attribute named "sns_topic_arn".

```

I’m wondering why I’m unable to access the output value from modules/sns/outputs.tf, or if there’s another way of accessing that value from the repo where I’m calling the cloudwatch-alarms module?

---

<div class="post-metadata">

**Author:** ![rbharadwaj042](https://avatars.discourse-cdn.com/v4/letter/r/b4bc9f/32.png) [@rbharadwaj042](https://discuss.hashicorp.com/u/rbharadwaj042)\
**Post date:** [April 15, 2024, 3:49am UTC](https://discuss.hashicorp.com/t/accessing-variable-from-another-module/34515/2 "2024-04-15T03:49:08Z")

</div>

Is there an answer for this?

---

<div class="post-metadata">

**Author:** ![jlj7](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/jlj7/32/18330_2.png) [@jlj7](https://discuss.hashicorp.com/u/jlj7)\
**Post date:** [April 15, 2024, 10:18am UTC](https://discuss.hashicorp.com/t/accessing-variable-from-another-module/34515/3 "2024-04-15T10:18:22Z")

</div>

H’m, it’s tough to guess, given the lack of detail in the original post… Maybe some sort of name clash, or to do with how the modules are being called? I’m not sure the interpolation is required either, but I don’t think that would cause the quoted error. 🤔

Can you share some details about your problem? Maybe we can help.

---

<div class="post-metadata">

**Author:** ![rbharadwaj042](https://avatars.discourse-cdn.com/v4/letter/r/b4bc9f/32.png) [@rbharadwaj042](https://discuss.hashicorp.com/u/rbharadwaj042)\
**Post date:** [April 15, 2024, 11:49am UTC](https://discuss.hashicorp.com/t/accessing-variable-from-another-module/34515/4 "2024-04-15T11:49:56Z")

</div>

I have 2 modules, calling the same sns\_topic from cloudwatch. But, I am using for\_each in the root’s main.tf as I have multiple clusters.

# **main.tf**

```auto
locals {
  cluster_configurations = {
    for key, cluster in var.rds_clusters : key => {
      cluster_identifier = cluster.cluster_identifier
      engine_mode = cluster.engine_mode
      sns_set_topic_name = "${cluster.cluster_identifier}-${var.rds_set_environment_name}-monitoring-topic"
    }
  }
}

# SNS topic
module "sns_topic" {
  source = "../../modules/sns_topic"
  
  for_each = local.cluster_configurations
  
  sns_set_topic_name = each.value.sns_set_topic_name

}

# Cloudwatch alarms
module "cloudwatch_alarms" {
  source = "../../modules/cloudwatch_monitoring_rds"

  for_each = local.cluster_configurations

  rds_set_cluster_identifier = "auroracluster1" //To be updated
  rds_set_engine_mode = "auroracluster1" //To be updated
  rds_set_environment_name = "auroracluster1" //To be updated
  #set_rds_topic_arn = "monitoring" ---------> This works
  set_rds_topic_arn = module.sns_topic.set_rds_topic_arn --------> This doesn't work
}

```

**I want to know how to call that module.**  
Error:  
╷  
│ Error: Unsupported attribute  
│  
│ on main.tf line 37, in module “cloudwatch\_alarms”:  
│ 37: set\_rds\_topic\_arn = module.sns\_topic.set\_rds\_topic\_arn[0]  
│ ├────────────────  
**│ │ module.sns\_topic is object with 2 attributes**  
│  
**│ This object does not have an attribute named “set\_rds\_topic\_arn”.**  
╵

---

<div class="post-metadata">

**Author:** ![apparentlymart](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/apparentlymart/32/702_2.png) [@apparentlymart](https://discuss.hashicorp.com/u/apparentlymart)\
**Post date:** [April 15, 2024, 3:13pm UTC](https://discuss.hashicorp.com/t/accessing-variable-from-another-module/34515/7 "2024-04-15T15:13:24Z")

</div>

Hi @rbharadwaj042,

Please start a new topic for your new question. Although it might be related to the earlier question in this topic, questions posted as their own topics tend to be more visible to forum visitors and thus more likely to get a useful answer.

Thanks!

---

<div class="post-metadata">

**Author:** ![apparentlymart](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/apparentlymart/32/702_2.png) [@apparentlymart](https://discuss.hashicorp.com/u/apparentlymart)\
**Post date:** [April 29, 2024, 3:13pm UTC](https://discuss.hashicorp.com/t/accessing-variable-from-another-module/34515/8 "2024-04-29T15:13:44Z")

</div>

Closed on April 29, 2024, 3:13pm UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
