<p>If it doesn't outright fail you can expect to see a cycle warning where you've created a loop in Terraform and it's trying to fulfil your request for <code>asg-name</code> before the asg has been created.</p>

<p>To get what you need you'll need to have something like the following:</p>

<h1><strong>module_apps/module_apps.tf</strong></h1>

<pre><code>resource "aws_autoscaling_group" "example" {
  name = "${var.asg_name}"
}
</code></pre>

<h1><strong>module_init.tf</strong></h1>

<pre><code>variable "asg_name" { default = "asg-ad" }

module "module_init" {
  source = "../module_apps"

  asg_name = "${var.asg_name}"
}

output "asg_name" {
  value = "${var.asg_name}"
}
</code></pre>

<p>This way, if you call <code>module_init</code> somewhere else you'll have <code>asg_name</code> available to you. Always be careful of trying to use an output of a module to create the module, Terraform doesn't handle loops like that well and the errors can be confusing to diagnose.</p>

CC BY-SA 3.0
2017-09-28T11:21:32.570Z
Could you explain what error message indicates this or is there an issue logged somewhere else, e.g. github?
030
CC BY-SA 3.0
2017-09-28T11:25:39.700Z
It's expected behaviour, I've edited the answer with what he can expect to see as an error message.
Briansbum
CC BY-SA 3.0
2017-09-28T10:56:06.090Z
2017-09-28T11:40:30.563Z
2017-09-28T11:40:30.563Z
Briansbum
<p>I want to get an <code>autoscaling group name</code> variable in <code>module_apps</code> and use it in other modules - for example <code>module_init</code>. However when I try to run <code>terraform plan</code> it does not work.</p>

<h1>content of module_apps</h1>

<pre><code>resource "aws_autoscaling_group" "example" 
{
  name = "asg-ad"
}

output "asg_name" 
{
  value = "${aws_autoscaling_group.example.name}"
}
</code></pre>

<h1>content of module_init</h1>

<pre><code>module "module_init" 
{
  source = "../module_apps"
  asg_name = "${module.module_init.asg_name}"
}
</code></pre>

<p>Trying to run <code>terraform plan</code> seems that it can't get the source module:</p>

<pre><code>o:Get: file:///home/user/terraform/application/module_apps
o:Get: file:///home/user//terraform/application/module_init
o:Get: file:///home/user//terraform/application/module_elb
o:Get: file:///home/user//terraform/application/module_deloy
o:Get: file:///home/user/terraform/application/module_apps
o:Get: file:///home/user//terraform/application/module_init
o:Get: file:///home/user//terraform/application/module_elb
o:Get: file:///home/user//terraform/application/module_deloy
o:Get: file:///home/user//terraform/application/module_init
o:Get: file:///home/user//terraform/application/module_elb
o:Get: file:///home/user//terraform/application/module_deloy
o:Get: file:///home/user/terraform/application/module_apps
o:Get: file:///home/user//terraform/application/module_init
o:Get: file:///home/user//terraform/application/module_elb
o:Get: file:///home/user//terraform/application/module_deloy
o:Get: file:///home/user//terraform/application/module_init
o:Get: file:///home/user//terraform/application/module_elb
o:Get: file:///home/user//terraform/application/module_deloy
o:Get: file:///home/user/terraform/application/module_apps
o:Get: file:///home/user//terraform/application/module_init
o:Get: file:///home/user//terraform/application/module_elb
o:Get: file:///home/user//terraform/application/module_deloy
</code></pre>

CC BY-SA 3.0
2017-09-28T10:15:33.870Z
Why did you do a `terraform apply` and not a `terraform init` or `terraform apply`?
030
<amazon-web-services><terraform>
Reusable module in Terraform
fahri
# Reusable module in Terraform

(Asked by: username_0 on 2017-09-28)

I want to get an autoscaling group name variable in module_apps and use it in other modules - for example module_init. However when I try to run terraform plan it does not work.
content of module_apps
resource "aws_autoscaling_group" "example" 
{
  name = "asg-ad"
}

output "asg_name" 
{
  value = "${aws_autoscaling_group.example.name}"
}

content of module_init
module "module_init" 
{
  source = "../module_apps"
  asg_name = "${module.module_init.asg_name}"
}

Trying to run terraform plan seems that it can't get the source module:
o:Get: file:///home/user/terraform/application/module_apps
o:Get: file:///home/user//terraform/application/module_init
o:Get: file:///home/user//terraform/application/module_elb
o:Get: file:///home/user//terraform/application/module_deloy
o:Get: file:///home/user/terraform/application/module_apps
o:Get: file:///home/user//terraform/application/module_init
o:Get: file:///home/user//terraform/application/module_elb
o:Get: file:///home/user//terraform/application/module_deloy
o:Get: file:///home/user//terraform/application/module_init
o:Get: file:///home/user//terraform/application/module_elb
o:Get: file:///home/user//terraform/application/module_deloy
o:Get: file:///home/user/terraform/application/module_apps
o:Get: file:///home/user//terraform/application/module_init
o:Get: file:///home/user//terraform/application/module_elb
o:Get: file:///home/user//terraform/application/module_deloy
o:Get: file:///home/user//terraform/application/module_init
o:Get: file:///home/user//terraform/application/module_elb
o:Get: file:///home/user//terraform/application/module_deloy
o:Get: file:///home/user/terraform/application/module_apps
o:Get: file:///home/user//terraform/application/module_init
o:Get: file:///home/user//terraform/application/module_elb
o:Get: file:///home/user//terraform/application/module_deloy

**Tags:** amazon-web-services, terraform

> **Comments:**
> **username_1**: Why did you do a `terraform apply` and not a `terraform init` or `terraform apply`?  

---

## 1 answers

### Answer by username_2 on 2017-09-28. Score: 1

If it doesn't outright fail you can expect to see a cycle warning where you've created a loop in Terraform and it's trying to fulfil your request for asg-name before the asg has been created.
To get what you need you'll need to have something like the following:
module_apps/module_apps.tf
resource "aws_autoscaling_group" "example" {
  name = "${var.asg_name}"
}

module_init.tf
variable "asg_name" { default = "asg-ad" }

module "module_init" {
  source = "../module_apps"

  asg_name = "${var.asg_name}"
}

output "asg_name" {
  value = "${var.asg_name}"
}

This way, if you call module_init somewhere else you'll have asg_name available to you. Always be careful of trying to use an output of a module to create the module, Terraform doesn't handle loops like that well and the errors can be confusing to diagnose.

> **Comments:**
> **username_1**: Could you explain what error message indicates this or is there an issue logged somewhere else, e.g. github?  
> **username_2**: It's expected behaviour, I've edited the answer with what he can expect to see as an error message.  

---

