2

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
  • Why did you do a terraform apply and not a terraform init or terraform apply? – 030 Sep 28 '17 at 10:15
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.

  • Could you explain what error message indicates this or is there an issue logged somewhere else, e.g. github? – 030 Sep 28 '17 at 11:21
  • It's expected behaviour, I've edited the answer with what he can expect to see as an error message. – Briansbum Sep 28 '17 at 11:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.