<p>Could you wrap the assignment to value using the <a href="https://www.terraform.io/docs/configuration/functions/try.html" rel="nofollow noreferrer">try type conversion function</a>?</p>

<pre><code>output primary_LB_Subnet {
  value = try(aws_subnet.subnet_loadbalancer[0], null)
}
</code></pre>

CC BY-SA 4.0
2020-03-25T12:14:18.657Z
2020-03-25T12:14:18.657Z
fun4jimmy
<p>In one of our our Terraform modules we have a variable to optionally create a load balancer. As part of this we create some subnets thusly:</p>

<pre><code>resource "aws_subnet" "subnet_loadbalancer" {
  count = var.create_loadbalancer ? length(var.az_names) : 0
  vpc_id            = aws_vpc.vpc_main.id
  cidr_block        = var.lb_subnet_cidrs[count.index]
  availability_zone = var.az_names[count.index]
}
</code></pre>

<p>Now, if we are creating a loadbalancer I want to output the first subnet for later use in other modules:</p>

<pre><code>output primary_LB_Subnet {
  value = aws_subnet.subnet_loadbalancer[0]
}
</code></pre>

<p>This is fine if we create the loadbalancer, but not if <code>var.create_loadbalancer</code> is false. In that case I get the following error:</p>

<pre><code>Error: Invalid index

  on ..\..\modules\vpc\loadbalancer.tf line 19, in output "primary_LB_Subnet":
  19:   value = aws_subnet.subnet_loadbalancer[0]
    |----------------
    | aws_subnet.subnet_loadbalancer is empty tuple

The given key does not identify an element in this collection value.
</code></pre>

<p>I can see why this would be a problem, as we haven't create the subnets, so my immediate thought was to do something like:</p>

<pre><code>output loadbalancer_sg_id {
  value = var.create_loadbalancer ? aws_subnet.subnet_loadbalancer[0] : null
}
</code></pre>

<p>But this doesn't help the issue and I still get the same issue. What is the correct way to address this within Terraform?</p>

CC BY-SA 4.0
2020-03-22T16:55:29.767Z
If at all possible, you should avoid this type of complex logic in terraform. Specifically for this reason ;)
thisguy123
<terraform>
Correct way to handle 'optional' array of resources in Terraform
steeveeet
# Correct way to handle 'optional' array of resources in Terraform

(Asked by: username_0 on 2020-03-19)

In one of our our Terraform modules we have a variable to optionally create a load balancer. As part of this we create some subnets thusly:
resource "aws_subnet" "subnet_loadbalancer" {
  count = var.create_loadbalancer ? length(var.az_names) : 0
  vpc_id            = aws_vpc.vpc_main.id
  cidr_block        = var.lb_subnet_cidrs[count.index]
  availability_zone = var.az_names[count.index]
}

Now, if we are creating a loadbalancer I want to output the first subnet for later use in other modules:
output primary_LB_Subnet {
  value = aws_subnet.subnet_loadbalancer[0]
}

This is fine if we create the loadbalancer, but not if var.create_loadbalancer is false. In that case I get the following error:
Error: Invalid index

  on ..\..\modules\vpc\loadbalancer.tf line 19, in output "primary_LB_Subnet":
  19:   value = aws_subnet.subnet_loadbalancer[0]
    |----------------
    | aws_subnet.subnet_loadbalancer is empty tuple

The given key does not identify an element in this collection value.

I can see why this would be a problem, as we haven't create the subnets, so my immediate thought was to do something like:
output loadbalancer_sg_id {
  value = var.create_loadbalancer ? aws_subnet.subnet_loadbalancer[0] : null
}

But this doesn't help the issue and I still get the same issue. What is the correct way to address this within Terraform?

**Tags:** terraform

> **Comments:**
> **username_1**: If at all possible, you should avoid this type of complex logic in terraform. Specifically for this reason ;)  

---

## 1 answers

### Answer by username_2 on 2020-03-25. Score: 2 | Accepted answer

Could you wrap the assignment to value using the try type conversion function?
output primary_LB_Subnet {
  value = try(aws_subnet.subnet_loadbalancer[0], null)
}


---

