title: Terraform 0.11: conditional vpc_config in aws_lambda_function
body: <p>I am trying to deploy a Lambda function with a conditional on the <code>vpc_config</code> block. I have tried using count inside <code>vpc_config</code> but it is not possible to use a count statement there. Per the documentation for the resource, if you pass in empty lists as follows, the <code>vpc_config</code> portion is ignored:</p>
  
  <pre><code>  vpc_config {
      subnet_ids = []
      security_group_ids = []
    }
  </code></pre>
  
  <p>I can confirm that this does work as expected (no VPC configuration is attempted). </p>
  
  <p>I have therefore tried using conditionals for <code>subnet_ids</code> and <code>security_group_ids</code> - something along the lines of <code>var.vpc_function ? var.subnet_ids : []</code> - but you cannot pass in a list in a conditional statement. The closest I have got to a solution is the below hacky number, which joins then splits the lists:</p>
  
  <pre><code>  resource "aws_lambda_function" "lambda_function" {
    ...
    vpc_config {
      subnet_ids = ["${split(",", var.vpc_function ? join(",", var.subnet_ids) : join(",", var.empty_array))}"]
      security_group_ids = ["${split(",", var.vpc_function ? join(",", aws_security_group.lambda_security_group.*.id) : join(",", var.empty_array))}"]
    }
  
  variable "vpc_function" {
    default = "false"
  }
  variable "subnet_ids" {
    type = "list"
    default = ["subnet-a", "subnet-b"]
  }
  variable "empty_array" {
    type = "list"
    default = []
  }
  </code></pre>
  
  <p>This applies fine the first time both where <code>vpc_function</code> is true or false, which is great, but where <code>vpc_function</code> is false, terraform always sees the above as a change on each reapply:</p>
  
  <pre><code>  vpc_config.#:                      "0" =&gt; "1"
    vpc_config.0.security_group_ids.#: "0" =&gt; "1"
    vpc_config.0.security_group_ids.0: "" =&gt; ""
    vpc_config.0.subnet_ids.#:         "0" =&gt; "1"
    vpc_config.0.subnet_ids.0:         "" =&gt; ""
  </code></pre>
  
  <p>I guess the problem is that my joined-then-split list is not seen as [], but rather as a list with some (blank) content. </p>
  
  <p>Does anyone else have a solution for this? I have tried to use Terraform 0.12 as well but couldn't figure it out. Maybe someone has done something similar in Terraform 0.12 and could lend some pointers? </p>