Answers: <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>
  
Answers: CC BY-SA 3.0
Answers: 2017-09-28T11:21:32.570Z
Answers: Could you explain what error message indicates this or is there an issue logged somewhere else, e.g. github?
Answers: 030
Answers: CC BY-SA 3.0
Answers: 2017-09-28T11:25:39.700Z
Answers: It's expected behaviour, I've edited the answer with what he can expect to see as an error message.
Answers: Briansbum
Answers: CC BY-SA 3.0
Answers: 2017-09-28T10:56:06.090Z
Answers: 2017-09-28T11:40:30.563Z
Answers: 2017-09-28T11:40:30.563Z
Answers: Briansbum
Body: <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>
  
Comments: CC BY-SA 3.0
Comments: 2017-09-28T10:15:33.870Z
Comments: Why did you do a `terraform apply` and not a `terraform init` or `terraform apply`?
Comments: 030
Tags: <amazon-web-services><terraform>
Title: Reusable module in Terraform
UserDisplayName: fahri
ThreadText: # 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.  
  
  ---
  
  