Module output when you don't know which resource will be created

688 views
Skip to first unread message

B Holmes

unread,
Jun 8, 2017, 6:10:40 AM6/8/17
to Terraform
Hi,

I'm writing a module that creates a snapshot-based RDS instance if the snapshot_name variable has a value, otherwise create a plain vanilla Oracle instance.

I'm using the count attribute and conditional replace to pass the with-snapshot resource's count a 1 if there is a snapshot_name, or 0 if snapshot_name is empty.

Likewise I'm using the count attribute and conditional replace to pass the without-snapshot resource's count a 1 if there isn't a snapshot_name, or 0 if there is.

So far, so good, this code is working.  But I want to create an rds_instance_address output that will take either the value of:
aws_db_instance.with_snap.address  or
aws_db_instance.without_snap.address

Things I've tried:
output "rds_instance_address" {
  value = "${aws_db_instance.with_snap.address}"
  value = "${aws_db_instance.without_snap.address}"
}

But when without_snap has no value (because we've provisioned a snapshot-based RDS instead), rds_instance_address has no value.  Same happens with:
output "rds_instance_address" {
   value = "${aws_db_instance.with_snap.address}${aws_db_instance.with_snap.address}"
}

Unf output takes no count attribute, so can't use above boolean trick to assign a 1 if aws_db_instance.with_snap.address has a value, etc.

Is there any way to achieve this?  The reason I need a single output that has one or other value is that I need to output to a text file and if you pass an empty variable to local-exec (that cat's variables to a text file), it errors: "local-exec provisioner command must be a non-empty string".

Many thanks

Lowe Schmidt

unread,
Jun 8, 2017, 7:47:37 AM6/8/17
to terrafo...@googlegroups.com
Have a look at conditionals https://www.terraform.io/docs/configuration/interpolation.html#conditionals

You should be able to do something like

output "rds_instance_address" {
  value = "${aws_db_instance.with_snap.address ? aws_db_instance.with_snap.address :  aws_db_instance.without_snap.address} "
}

--
Lowe Schmidt | +46 723 867 157

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-tool+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/d442d8e6-9112-463b-8846-dd953a66aa45%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

B Holmes

unread,
Jun 8, 2017, 10:51:41 AM6/8/17
to Terraform
output "rds_instance_address" {
  value = "${aws_db_instance.with_snap.address ? aws_db_instance.with_snap.address :  aws_db_instance.without_snap.address} "
}

Doesn't work.  If either trueval or falseval don't exist, it outputs nothing.  Can verify this using a simple example:
module "eips" {
  source = "poth/to/module/eips"
  create_an_eip1 = "true"
}

output "eip_pub_ip" {
  value = "${module.eips.eip_pub_ip}"
 }


where module eips contains:
variable "create_an_eip1" {}

resource "aws_eip" "eip1" {
  count = "${var.create_an_eip1 ? 1 : 0 }"
}

resource "aws_eip" "eip2" {
  count = "${var.create_an_eip1 ? 0 : 1 }"
}

output "eip_pub_ip" {
  value = "${var.create_an_eip1 ? aws_eip.eip1.public_ip : aws_eip.eip2.public_ip }"
}

eip1 is successfully created if create_an_eip1 is true, or eip2 is successfully created if create_an_eip1 is false.  But nothing is output either way.
Reply all
Reply to author
Forward
0 new messages