patternterraformMinor
Reusable module in Terraform
Viewed 0 times
reusablemoduleterraform
Problem
I want to get an
content of module_apps
content of module_init
Trying to run
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_deloySolution
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
To get what you need you'll need to have something like the following:
module_apps/module_apps.tf
module_init.tf
This way, if you call
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.Code Snippets
resource "aws_autoscaling_group" "example" {
name = "${var.asg_name}"
}variable "asg_name" { default = "asg-ad" }
module "module_init" {
source = "../module_apps"
asg_name = "${var.asg_name}"
}
output "asg_name" {
value = "${var.asg_name}"
}Context
StackExchange DevOps Q#2185, answer score: 1
Revisions (0)
No revisions yet.