Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support use cases with conditional logic #1604

Closed
phinze opened this issue Apr 20, 2015 · 167 comments
Closed

Support use cases with conditional logic #1604

phinze opened this issue Apr 20, 2015 · 167 comments

Comments

@phinze
Copy link
Contributor

phinze commented Apr 20, 2015

It's been important from the beginning that Terraform's configuration language is declarative, which has meant that the core team has intentionally avoided adding flow-control statements like conditionals and loops to the language.

But in the real world, there are still plenty of perfectly reasonable scenarios that are difficult to express in the current version of Terraform without copious amounts of duplication because of the lack of conditionals. We'd like Terraform to support these use cases one way or another.

I'm opening this issue to collect some real-world example where, as a config author, it seems like an if statement would really make things easier.

Using these examples, we'll play around with different ideas to improve the tools Terraform provides to the config author in these scenarios.

So please feel free to chime in with some specific examples - ideally with with blocks of Terraform configuration included. If you've got ideas for syntax or config language features that could form a solution, those are welcome here too.

(No need to respond with just "+1" / :+1: on this thread, since it's an issue we're already aware is important.)

@chrisferry
Copy link

Here are 2 examples: https://gist.github.com/chrisferry/780140d709bfad51038c
The RDS and ELB modules have minor differences. SSL cert or no. IOPs or no.

@ketzacoatl
Copy link
Contributor

OMG, I could rant on about this issue for a long while. One litany of clear and concise use cases are found in implementing a concept as a terraform module. There are even community modules which exemplify this.. two modules for essentially the same thing, one provides an ELB, one does not.

I tend to want to write terraform source as I do with Saltstack: as a giant jinja template. This affords me a whole lot of flexibility while ensuring the application (Salt) ends up with a machine-readable format. Terraform sort of has this type of pre-processing (with interpolation), but Saltstack's implementation leverages the concept of a pluggable renderer system.. this pre-processor renders the template to give to salt for processing. The renderer can be jinja, mako, or any one of a few different systems (made available as modules). The user/developer experience has been exceptional, and I am thankful for the power it lends, while still providing a declarative system. In contrast, using terraform has felt cumbersome and restrictive in the expression of one's needs (especially when I have gone to encapsulate a working POC into a module).

Thank you for opening this dicussion!

@apparentlymart
Copy link
Contributor

This is a bit of a stretch on the topic of this issue, but a couple of times I've found myself wishing for an iteration construct to allow me to create a set of resources that each map one-to-one to an item in a list.

I've found and then promptly forgotten a number of examples (having dismissed them as impossible), but one that stayed in my mind was giving EC2 instances more memorable local hostnames and then creating Route53 records for each of them.

resource "aws_instance" "app_server" {
    # (...)

    count = 5

    provisioner "remote-exec" {
        inline = [
            "set-hostname-somehow appname-${join(\"-\", split(\".\", self.private_ip))}"
        ]
    }
}

foreach "${aws_instance.app_server.*}" {
    resource "aws_route53_record" "app_server-${item.private_ip}" {
        zone_id = "${something_defined_elsewhere}"
        name = "appname-${join(\"-\", split(\".\", self.private_ip))}.mydomain.com"
        type = "A"
        records = ["${item.private_ip}"]
    }
}

In my imagination, this creates a set of resources named things like aws_route53_record.app_server-10.0.0.1 which then get created/destroyed as you'd expect when the corresponding instances come and go. In case it wasn't obvious, item here is imagined as the iteration variable.

While I was sketching this out I also came up with an alternative formulation that might end up leading to a simpler mental model:

resource "aws_instance" "app_server" {
    # (...)

    count = 5

    provisioner "remote-exec" {
        inline = [
            "set-hostname-somehow appname-${join(\"-\", split(\".\", self.private_ip))}"
        ]
    }

    child_resource "aws_route53_record" "hostname" {
        zone_id = "${something_defined_elsewhere}"
        name = "appname-${join(\"-\", split(\".\", parent.private_ip))}.mydomain.com"
        type = "A"
        records = ["${parent.private_ip}"]
    }
}

In this formulation, rather than generically supporting iteration over all lists we can just create a family of child resources for each "parent" resource. This feels conceptually similar to how per-instance provisioners work. I'm imagining that the child resource would interpolate like ${aws_instance.app_server.0.children.hostname.records}, or indeed ${aws_instance.app_server.*.children.hostname.name} to get all of the created hostnames.

@bgeesaman
Copy link

When your environment always looks the same (e.g. for a long standing/running app), the declarative language of terraform is expressive enough for most needs. However, my use case (frequent spin ups/tear downs of AWS VPCs of a similar general structure but with plenty of instance/subnet variation) means that terraform is not the "start" of my pipeline. I need to combine some configuration, logic, and templates on the fly each time to define my desired environment before terraform can ingest it.

Right now, I'm planning on writing something custom (rake and erb?) to generate the needed terraform json and go from there because I don't see template-esque logic as a job for terraform's declarative config language, It already can use json as an incoming interchange format, so wouldn't it be more versatile to just leverage any of the existing template renderers out there as @ketzacoatl described? It would keep logic out of the configuration and keep the terraform language simple/clean.

Appreciate this discussion and I'm open to learning about a better way to solve these kinds of problems.

@bgeesaman
Copy link

And I just realized why my suggested approach is flawed in some cases. The power of terraform is using derived data at runtime as variables elsewhere. If you render separately ahead of time in some cases, you lose that. The count=5 is an example of something you can't pre-render and then reference easily. Gah, sorry.

@ketzacoatl
Copy link
Contributor

The power in terraform, IMHO, is that we have the flexibility to choose how much we do before , in TF, and after TF runs. In most cases, you need to start working with some wrapper to create the JSON you want, when the existing interpolation syntax won't get you what you want. I personally, have avoided this as I would rather keep the before limited to a CI / admin who defines details in the terraform.tfvars file to pass to TF when it runs. At the same time, this is also where the conditional logic and other interpolation/syntacitic sugar comes in (but is in some cases missing).

@pmoust
Copy link
Contributor

pmoust commented Apr 28, 2015

Last December in AMS Dockercon I had asked @mitchellh if there would be any plans to add such logic control in Terraform DSL, he explained his view on keeping Terraform as simple as possible maybe adding a little algebraic functionality (which has already been merged) and standard string operations.

I am glad there are second thoughts on this, but it is a decision that needs a lot of input and real world justification, so thatnks @phinze for bringing this up.

I have two real world scenarios I 've faced where an if statement would have come in handy in Terraform:

  1. IaC of MongoDB replica sets using AWS ASG terraform module.
    Currently, if I am not wildly mistaken, 10gen does not provide a simple enough recipe to automate this through Cloudformation, the solutions are just nasty and involve adding shell script in the Cloudformation map.
    Using Terraform I would like to call a provisioner on a single node to initialise the replicaSet and obtain master status from just the first node in the ASG, while a remote file is called via cloud-init on each ASG member to rs.add() itself by connectiong to the master node.
    Right now I 've resorted in a nasty script to get the job done along with a dummy resource to invoke a provisioner.
    I am sure there must be a more elegant way, irregardless if would greatly mitigate the nastiness in my setup
  2. Legacy app EC2/OpenStack webserver fleet that requires just one node to be running crons. While this setup is fundementaly flawed for obvious reason, it is still very common use-case scenario and I 've come across this. An if would be nice to instruct a remote-exec provisioner to work its magic on just one node. Right now I just create a dummy resource that depends on .0.id so that it gets provisioned from there. Ugly. There are ways around it, but still...

@pmoust
Copy link
Contributor

pmoust commented Apr 28, 2015

Other scenario,

  1. CoreOS fleet in an AWS ASG, should a terraform run detects an even number behind ASG, deregister one etcd server from the etcd cluster to enable sensible quorum. This touches the surface of current issue raised by @phinze as such functionality (run provisioners on ASG cluster nodes) is not implemented in Terraform, and it more-or-less defy the purpose of exact clones in ASG. Still it is something I have personally come across and I manage externally.

@tayzlor
Copy link

tayzlor commented May 7, 2015

A simple scenario -

Optionally use Atlas artifacts to deploy infrastructure - and fallback to just AMI strings (if not using atlas).

Something like -

if ${var.atlas_enabled} {
  resource "atlas_artifact" "machine" {
    name = "${var.atlas_artifact.name}"
    type = "aws.ami"
  }
}

then in a resource 

resource "aws_instance" "machine" {
  if ${var.atlas_enabled} {
     ami = "${atlas_artifact.machine.id}"
  } else {
     ami = "${var.ami_string}"
  }
}

@mzupan
Copy link
Contributor

mzupan commented May 17, 2015

👍

I like the idea of even a simple

if ${var.atlas_enabled} {

}

There are lots of times where I'm building the same infrastructure for dev/stage/prod but don't need things in dev/stage as are needed in prod.

@franklinwise
Copy link

Even simpler, doesn't break the current syntax and prevents complexity (Inspired by Ansible):

resource "aws_instance" "machine" {
  when = "${var.atlas_enabled}"
  ami = "${atlas_artifact.machine.id}"
}

resource "aws_instance" "machine" {
  when = "not ${var.atlas_enabled}"
  ami = "${var.ami_string}"
}

@phinze
Copy link
Contributor Author

phinze commented May 27, 2015

@franklinwise that's pretty nice!

I like that it preserves the declarative syntax.

@knuckolls
Copy link
Contributor

Preserving the declarative syntax seems like priority number one in my opinion. I like the 'when' solution. 👍

@ketzacoatl
Copy link
Contributor

Yes, I too like this suggested syntax.. when, and the use of not here, makes a lot of sense.

@rafikk
Copy link

rafikk commented May 28, 2015

This seems inadequate as it only supports the conditional creation of resources. In my usage, I've found myself wanting conditional expressions (not statements) several times.

The use case has been creating heterogenous groups of ec2 instances. I.e., I'd like to create 12 instances where the first 4 are of type m3.large and the remaining are c4.xlarge. I've hacked around this for the time being by using a lookup table and creating an entry for each index, but it's pretty nasty.

For clarification, what I'm looking for is something like:

instance_type = "${if count.index > 3 then c4.xlarge else m3.large}"

@ketzacoatl
Copy link
Contributor

It might make sense to consider these use cases separately, if only for the goal of getting the simpler implemented faster than the more complicated and nuanced case. when / not as a field on all resources is a fantastic start. Figuring out the rest seems to be too much to figure out in the immediate.. why block one for the other?

@bortels
Copy link

bortels commented May 30, 2015

Expanding on the referenced aws spot instances - I'd like to be able to express "spin up N instances in at least M availability zones, bidding the current bid price * X, But fallback to on-demand for any az where the bid price is > Y, or spot instance is unavailable". That seems complex (I have implemented it via a custom ruby script now), so maybe it's less a "we need conditionals" argument and more a "it would be nice if the aws provider abstracted instance types and did the "right thing").

Maybe the whole conditional thing could be handled by some sort of "call to external" hook to work the logic? It's a cop-out, in a way, but perhaps the most flexible in the end. If the suggested "when" could call an arbitrary script, with current context, you could kinda wedge in anything you needed as a shim, in only the spots where declarative is problematic. Can terraform do external calls like that already? I came into this from the side googling for spot instance solutions and seeing if terraform had support, so I'm not super familiar with current functionality there.

@rossedman
Copy link

👍 Really like @franklinwise suggestion. I think this is the way to go if control statements are added.

@RJSzynal
Copy link

Just to add another use case similar to rafikk's example.
I'm using terraform to bring up a count of 'container instances' or 'nodes' in an AWS ECS cluster with consul running on each one. I want the first three to be run as servers but the rest to be agents.
Currently I have to duplicate the resource block which isn't very neat and any changes have to be done twice which allows too much room for human error.

It would be more flexible if I could do the following:
user_data = "${if count.index < 4 then SERVER_SCRIPT else AGENT_SCRIPT}"

@rafikk
Copy link

rafikk commented Jun 16, 2015

@RJSzynal We have the same thing in our configuration. I really think support conditional logic inside of interpolation blocks is a must.

@thegedge
Copy link
Contributor

tl;dr my five cents is that I'm for the high-level idea of better conditional / looping support, but would like to stick to keeping it declarative.

I like the child resource proposal by @apparentlymart (far more than the foreach, which feels too imperative). That being said, for that specific example I feel like the resource isn't really a child. What may be better is a top-level "group" construct, which supports count-style looping.

I'm also +1 for some basic conditional structure that gets interpolated. I don't like the imperative style "if then else" that's been recommended. I'd rather stick with a more procedural style thing that we already have, e.g., user_data = "${cond(var.use_foo, "foo", "bar")}", or perhaps some basic conditions user_data = "${cond(var.count = 1, "foo", "bar")}"

I agree with @rafikk with the lookup table being too verbose:

variable "lookup_table" {
  default = {
    "true" = "foo"
    "false" = "bar"
  }
}

variable "use_foo" {}

resource "aws_instance" "my_instance" {
  ami = "${lookup(var.lookup_table, var.use_foo)}"
  ...
}

I'm also not fond of the if block surrounding a resource, as things start looking too imperative. A better approach to stick with the declarative format would be something like:

resource "aws_instance" "my_instance" {
  ignore = "${var.disabled}"
}

I think setting count = 0 essentially does this, so this may be redundant. Sorry if I've repeated any ideas that may have already been expressed in earlier comments!

@hartfordfive
Copy link

👍

@apparentlymart
Copy link
Contributor

@thegedge sorry I just noticed your response to my earlier example even though you posted it a while back.

The special thing I was imagining for "child resources" is that they'd always have an implied dependency on their parent, so if you delete the instance then that always deletes the record along with it... perhaps "child" is the wrong word, but I was going for "this thing only exists to support the thing it's nested inside".

@thegedge
Copy link
Contributor

@apparentlymart Ah yes, that would be nice. Could that also be solved with a "also destroy dependent resources" flag to terraform destroy? Maybe modeling these things as being intimately connected (i.e., an atomic unit) would be useful in other, not immediately obvious ways too.

Anyways, I'm also going to add in an example from my team, since I didn't do that in my last comment and that's what @phinze was interested in seeing! We want our app developers to build things on top of a terraformed "cloud", but we want them to be able to this with a minimal configuration that doesn't require much/any terraform knowledge.

Many of our apps follow a similar recipe: rails app that sometimes needs redis, memcache, an S3 bucket, a place to run the rails app, and/or a database, maybe some other things. We'd like to construct a module that allows our app developers to conditionally select what they need for their app. It would look something like this from the app developer's perspective:

## /my_app/main.tf
module "my_app" {
  source = "../app_template"
  rails_server = "puma"
  background_jobs = "resque"
  memcache = true
}

## /app_template/main.tf
...
resource "aws_elasticache_cluster" "memcache" {
  ignore = "${!var.memcache}"
  ...
}

resource "aws_elasticache_cluster" "redis" {
  ignore = "${var.background_jobs != "resque"}"
  ...
}
...

@aavileli
Copy link

awaiting for TF .8

@ryno75
Copy link
Contributor

ryno75 commented Dec 15, 2016

IT'S HERE! YAHOOOOOOOOOOOOOOOOOOO!!!

@stack72
Copy link
Contributor

stack72 commented Dec 18, 2016

Closing this as it has landed in Terraform 0.8.x :)

@stack72 stack72 closed this as completed Dec 18, 2016
@tmatilai
Copy link

@stack72 I assume you refer to the new ternary operator? While that covers many of the use cases, it won't help with optionally specifying attributes, or "sub hashes" inside the resources. For example optionally declaring multiple listeners in an ELB. Would be nice to have an issue for tracking those, too.

But great work, the ternary operator for sure makes things easier especially in generic modules!

@apparentlymart
Copy link
Contributor

Given the age and large potential scope of this issue, I guess it makes sense for us to close it out and capture some more-specific use-cases in other issues, since this one was originally motivated with just collecting information on patterns people were using, and didn't have a tight enough scope that it would ever likely be closed by any real implementation work.

The trick there, of course, is that we previously intentionally consolidated several issues describing other use-cases over here, so there's a bunch of closed issues linked from here that capture some real use-cases we presumably don't want to "lose".

FWIW though, I understand that improved conditional stuff is still on the radar even if this particular issue is closed.

@oillio
Copy link

oillio commented Jan 20, 2017

@tmatilai - Would this feature request solve your issue? #7034

@tmatilai
Copy link

@oillio yeah, as far as I understand, that would cover the sub-resource/block case!

Even after that one use case would be conditional individual attributes, although I haven't personally been so much in need of those compared to sub-resources.

@aaroncaito
Copy link

Trying to get consistent output of a module using count to dictate the version of a resource to create.

module code:

resource "aws_s3_bucket" "lc_enabled" {
  count = "${var.lifecycle_enabled ? 1 : 0}"
  bucket  = "${var.bucket}"
  acl = "${var.acl}"
  lifecycle_rule {
      id = "log"
      prefix = "log/"
      enabled = true
      transition {
          days = 30
          storage_class = "STANDARD_IA"
      }
      transition {
          days = 60
          storage_class = "GLACIER"
      }
      expiration {
          days = 90
      }
  }
  versioning {
    enabled = "${var.versioning}"
  }
}
resource "aws_s3_bucket" "lc_disabled" {
  count = "${var.lifecycle_enabled ? 0 : 1 }"
  bucket  = "${var.bucket}"
  acl = "${var.acl}"
  versioning {
    enabled = "${var.versioning}"
  }
}

failing output code:

output "bucket.name" {
  value = "${var.lifecycle_enabled == 1 ? aws_s3_bucket.lc_enabled.id : aws_s3_bucket.lc_disabled.id}"
}

Apparently if any ternary values don't exist, the entire ternary will fail silently. the output above does not display at all. However if I'm using lifecycle_enabled = false and change the output to:

output "bucket.name" {
  value = "${var.lifecycle_enabled == 1 ? "something" : aws_s3_bucket.lc_disabled.id}"
}

Here because all these values exist the conditional parses correctly. Is this expected behavior? Ultimately I'm looking for a way to consolidate the output so if I'm overlooking a more direct method that'd be great.

@apparentlymart
Copy link
Contributor

Hi @aaroncaito!

That issue is being tracked over in hashicorp/hil#50. It's something I'd like to fix before too long since I know it's annoying and makes the conditional operator not as useful as it could otherwise be. It requires some adjustment to how the interpolation syntax evaluator works, so it's not a quick fix but something that is on my radar to take a look at.

@aaroncaito
Copy link

aaroncaito commented Feb 26, 2017

thanks @apparentlymart in this case it'd be even better if we didn't need unique resource names when only one will be created. my output "thing.attribute" {value = "resource.SHARED_NAME.attribute"}. with no conditional eval required. I'll track that other issue as well as that will help other use cases.

@pdecat
Copy link
Contributor

pdecat commented Mar 2, 2017

My workaround for the above S3 with LC enabled/disabled use case:

output "bucket.name" {
  value = "${coalesce(join("", aws_s3_bucket.lc_enabled.*.id), join("", aws_s3_bucket.lc_disabled.*.id))}"
}

Edit: the coalesce is superfluous:

output "bucket.name" {
  value = "${join("", aws_s3_bucket.lc_enabled.*.id, aws_s3_bucket.lc_disabled.*.id)}"
}

However, this usage of join() with multiple lists is not documented, so I'm not sure it is a good idea:

join(delim, list) - Joins the list with the delimiter for a resultant string.

https://www.terraform.io/docs/configuration/interpolation.html#join_delim_list_

@tamsky
Copy link
Contributor

tamsky commented Mar 9, 2017

@pdecat your workaround is similar to another workaround I saw recently:
#12453 (comment)

using join to turn the lists into strings to get past the type check

@gerph
Copy link

gerph commented Sep 12, 2017

I have a case where I want to be able to define the AWS instance type in a variable, and - along side it - the size of the root volume. Since on AWS only EBS volumes can have a size, this becomes a problem. If I specify the instance type as a type that doesn't have EBS backed storage then no value will work for the volume_size, and AWS will always complain that you cannot do that.

That is...

resource "aws_instance" "foo" {
  ...
  root_block_device {
    volume_size = "0"
  }
}

(applies to both aws_instance, and aws_opsworks_instance, although it's strange that they don't share the code)

My workaround? Change the terraform code so that if the parameters within root_block_device are ineffective (ie 0 in the case of the size) the block device isn't added to the definition, and AWS is happy. It's not great, but recompiling the tool isn't a big deal as I've already got a number of patches applied.

@glasser
Copy link
Contributor

glasser commented Sep 12, 2017

@gerph I am likely to do a similar hack to allow a single aws_launch_configuration to either have name or name_prefix — ie, so that normally my LCs can have autogenerated names but I can pin to an old name for a rollback if necessary.

@cloudtriquetra
Copy link

I have two variables lob and role. As of now it is like this:

target = "${var.role == "hzc" ? "TCP:11410" : "HTTP:80/"}"

But my requirement is:

if lob==anything and role==hzc then "TCP:11410

else if lob==kolk and role==kata then "HTTPS:443/index.html"

else "TCP:8443"

How to map this in conditional statement?

@ghost
Copy link

ghost commented Oct 4, 2017

@arkaprava-jana I assume by "anything" you mean "is defined" and not the text "anything"? You could nest conditionals to get something like:

target ="${var.role == "hzc" && var.lob != "" ? "TCP:11410" : var.lob == "kolk" && var.role == "kata" ? "HTTPS:443/index.html" : "TCP:8443"}"

@cloudtriquetra
Copy link

Yes you are right. Looking for nested conditional example only.

@armenr
Copy link

armenr commented Oct 26, 2017

I'm trying to get clever with the ternary operator and conditionals, but I think I have the wrong idea about them. I have the following code in an ec2 module I've built on top of the ec2_instance resource.

I have a var I've created called "total_instances".
I've set: count = "${var.total_instances}"

I've tried the following:

Name = "${count.index} = 1 ? {var.instance_name}-${var.environment} : {var.instance_name}${count.index+1}-${var.environment}}"

I've also tried:

Name = "${var.total_instances} = 1 ? {var.instance_name}-${var.environment} : {var.instance_name}${count.index+1}-${var.environment}}"

If the count of instances I pass to the module = 1, then I just want the instance to be tagged as "server-ops", but if the count I pass from my main.tf to the module is greater than 1, I want it to assign "server1-ops" as the name.

Any help or clarification would be very helpful, thank you!

@jaygorrell
Copy link

@armenr I think the problem is yourr syntax... it should be ${condition ? true : false} all in one evaluation block. You may also want to use the format() interpolation to combine strings and variables easier but in this case the simplest is probably to only use the ternary to determine if the count is needed or not:
${var.instance_name}${var.total_instances > 1 ? count.index + 1 : ""}-${var.environment}

A format() example would be:
${var.total_instances > 1 ? format("%s-%02d-%s", var.instance_name, (count.index + 1), var.environment) : format("%s-%s", var.instance_name, var.environment)}

@armenr
Copy link

armenr commented Oct 26, 2017

@jaygorrell - Thanks! I'll fiddle with this and let you know how that works out. Thank you!

@megakoresh
Copy link

megakoresh commented Mar 27, 2018

I don't know if I am late to the party, but I am currently facing similar issues to what is being discussed here:

One use case that does not work with the current implementation is assigning pre-allocated Floating IPs vs allocating them. The scenario is like this:

Production has a fixed amount of servers with pre-allocated IP addresses that need to remain the same all the time.

Testing has the same infrastructure but can have more/less servers and Floating IPs are allocated and released on demand.

(I use openstack as an example here, but I imagine there are quite many parallel scenarios regardless of platform)
The following does not work:

resource "openstack_networking_floatingip_v2" "tf_instance_ips" {
  count = "${length(var.floating_ips) > 0 ? 0 : var.count}"
  pool  = "${var.floating_ip_pool_name}"
}

resource "openstack_compute_floatingip_associate_v2" "tf_instance_ips" {
  count       = "${var.count}"
  floating_ip = "${length(var.floating_ips) > 0 ? element(var.floating_ips, count.index) : element(openstack_networking_floatingip_v2.tf_instance_ips.*.address, count.index)}"
  instance_id = "${element(openstack_compute_instance_v2.tf_instance.*.id, count.index)}"
}

This will throw
element() may not be used with an empty list
because of #11210
However even when that is fixed, it is much easier to read and cleaner to have something like this:

if "${length(var.floating_ips) > 0}" {
  resource "openstack_compute_floatingip_associate_v2" "tf_instance_ips" {
    count       = "${var.count}"
    floating_ip = "${element(var.floating_ips, count.index)}"
    instance_id = "${element(openstack_compute_instance_v2.tf_instance.*.id, count.index)}"
  }
}

if "${length(openstack_networking_floatingip_v2.tf_instance_ips.*.address) > 0}" {
  resource "openstack_networking_floatingip_v2" "tf_instance_ips" {
    count = "${length(var.floating_ips) > 0 ? 0 : var.count}"
    pool  = "${var.floating_ip_pool_name}"
  }
  resource "openstack_compute_floatingip_associate_v2" "tf_instance_ips" {
    count       = "${var.count}"
    floating_ip = "${element(openstack_networking_floatingip_v2.tf_instance_ips.*.address, count.index)}"
    instance_id = "${element(openstack_compute_instance_v2.tf_instance.*.id, count.index)}"
  }
}

PS: The workaround for the above use case is to use coalescelist function, but it is sub-optimal for example in cases where type of the input value changes conditionally or when depending on condition different sets of resources need to be spawned, while remaining resources are not different.

resource "openstack_compute_floatingip_associate_v2" "tf_instance_ips" {
  count       = "${var.count}"
  floating_ip = "${element(coalescelist(var.floating_ips, openstack_networking_floatingip_v2.tf_instance_ips.*.address), count.index)}"
  instance_id = "${element(openstack_compute_instance_v2.tf_instance.*.id, count.index)}"
}

@mthak
Copy link

mthak commented Jun 5, 2018

how do i get something like this in terraform
Properties:
{% if PostgresSnapshot is defined and RDSSnapshot != "" %}
DBSnapshotIdentifier:
Ref: PostgresSnapshot2
{% else %}
DBName: {{ DbName |default('sample', true) }}
MasterUsername:
Ref: MasterU
MasterUserPassword:
Ref: MasterUserPW
{% endif %}

@Theaxiom
Copy link

Theaxiom commented Aug 8, 2018

@mthak Something similar to that will be supported by Terraform 0.12 which will be released later this summer: https://www.hashicorp.com/blog/terraform-0-1-2-preview

@ghost
Copy link

ghost commented Apr 2, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators Apr 2, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests