Terraform conditional data sources

Asked by At

I am trying to build a module that would have to create a file with different values based on a conditional which once generated, would be used in another module.

An example is as follows:

If myVar = x then use set 'a' of variables, else if myVar = y, use set 'b' of variables otherwise if myVar = z, use set 'c' of variables.

I have tried to do something like below, but no luck.

data "ignition_file" "instance" {
  path       = "/instance.env"
  count = "${var.myVar}"

content {
  content = "${data.template_file.instance.rendered}"

Also, I have tried using:

count = "${var.myVar == "a" ? 1 : 0}"

but no luck either.

I cam across this https://blog.gruntwork.io/terraform-tips-tricks-loops-if-statements-and-gotchas-f739bbae55f9 where an example is as follows:

data "template_file" "user_data_shell" {
  count = "${var.use_shell_script_user_data}"
  template = <<-EOF
data "template_file" "user_data_cloud" {
  count = "${1 - var.use_shell_script_user_data}"
  template = <<-EOF
                - run-microservice.sh

However, this will result in in having 3 outputs which will make it difficult for me to pass it to the other module.

Is there a way of using Terraform 0.11 in achieving this?

0 Answers