title: How can I make Terraform replace a null value with a default value?
body: <p>The Terraform documentation indicates this should already be happening:</p>
  <p><a href="https://www.terraform.io/docs/language/expressions/types.html" rel="noreferrer">https://www.terraform.io/docs/language/expressions/types.html</a></p>
  <blockquote>
  <p>null: a value that represents absence or omission. If you set an argument of a resource or module to null, Terraform behaves as though you had completely omitted it — it will use the argument's default value if it has one, or raise an error if the argument is mandatory.</p>
  </blockquote>
  <p>I'm calling a module &quot;foo&quot; that has the following variable file:</p>
  <pre><code>variable &quot;bar&quot; {
    type    = string
    default = &quot;HelloWorld&quot;
  }
  </code></pre>
  <p><em>Example 1</em></p>
  <p>When I call it using this code:</p>
  <pre><code>module &quot;foo&quot; {
    source = &quot;../modules/foo&quot;
    bar = null
  }
  </code></pre>
  <p>The result is an error.  Invalid value for &quot;str&quot; parameter: argument must not be null.  Trigger when bar is being used.</p>
  <p><em>Example 2</em></p>
  <p>When I call it using this code (omitting it, rather than nulling it):</p>
  <pre><code>module &quot;foo&quot; {
    source = &quot;../modules/foo&quot;
    # bar = null
  }
  </code></pre>
  <p>The result is that it works.  The &quot;bar&quot; variable is defaulted to &quot;HelloWorld&quot;.</p>
  <p>This appears to be a bug In Terraform that someone else also raised but wasn't resolved.
  <a href="https://github.com/hashicorp/terraform/issues/27730" rel="noreferrer">https://github.com/hashicorp/terraform/issues/27730</a></p>
  <p><strong>Does anyone know a solution or a work around?</strong></p>
  <p>Version information:</p>
  <pre><code>Terraform v1.0.5
  on linux_amd64
  + provider registry.terraform.io/hashicorp/google v3.51.0
  + provider registry.terraform.io/hashicorp/null v3.1.0
  + provider registry.terraform.io/hashicorp/random v3.1.0
  + provider registry.terraform.io/hashicorp/time v0.7.2
  </code></pre>
  <p><strong>Workaround</strong></p>
  <p>Based on @Matt Schuchard's comment and some research there's a ugly solution using the conditional check:</p>
  <pre><code>variable &quot;foo&quot; {
    type    = string
    default = &quot;HelloWorld&quot;
  }
  locals {
    foo = var.foo == null ? &quot;HelloWorld&quot; : var.foo
  }
  </code></pre>
  <p><strong>Why</strong></p>
  <p>My use case is an attempt to avoid duplicated code.  I have 2 very similar modules, one being a subset of the other.  The solution I'm using is to put the modules in sequence calling each, i.e. a grandparent, parent and child.</p>
  <p>I want to have the variables available to the &quot;grandparent&quot; but if they're omitted then the module below &quot;child&quot; should set them using a default value, e.g. &quot;HelloWorld&quot;.  But to exposed those variables all the way through the family line I have to include them in all modules and in the high modules (grandparent and parent) I want to default them to null, allowing them to be optional but also still causing them to be set to a default in the &quot;child&quot; further down the line.</p>
  <p>...I think I need a diagram.</p>