title: Terraform interpolation of locals map with key defined in a variable
body: <h2>Terraform interpolation of locals map with key defined in a variable</h2>
  <p>Objective:
  Define preset sizing labels in variable, provisioning of resources uses preset values from locals map.</p>
  <pre><code>
  var &quot;define_size&quot; {
    description = &quot;Select either small, medium, large&quot;
    default = &quot;large&quot;
  }
  
  
  locals {
  
    small = {
      volume_gb = 1
      volume_count = 1
    }
  
    medium = {
      volume_gb = 20
      volume_count = 5
    }
  
    large = {
      volume_gb = 500
      volume_count = 10
    }
  
  }
  
  
  resource &quot;aws_ebs_volume&quot; &quot;example&quot; {
    availability_zone = var.availability_zone
    size              = ??????
  }
  </code></pre>
  <p><strong>Attempts:</strong></p>
  <ul>
  <li><code>size = local.$var.define_size.volume_gb</code>. Obvious bad syntax results in &quot;Error: Invalid character.&quot; and &quot;Error: Invalid attribute name&quot; referring to the $ character.</li>
  <li><code>size = local.${var.define_size}.volume_gb</code>. Obvious bad syntax results in &quot;Error: Invalid character.&quot; and &quot;Error: Invalid attribute name&quot; referring to the $ character.</li>
  <li><code>size = &quot;${local[var.define_size].volume_gb}&quot;</code>. &quot;Error: Invalid reference. A reference to a resource type must be followed by at least one attribute access, specifying the resource name.&quot;</li>
  <li><code>size = tostring(&quot;local.${var.define_size}.volume_gb&quot;)</code> this renders correctly but as a string and not a resource reference <code>&quot;local.large.volume_gb&quot;</code></li>
  <li><code>format(&quot;%#v&quot;,tostring(&quot;local.${var.define_size}.volume_gb&quot;))</code> this renders partially correctly but as string with escape characters and not resource <code>&quot;\&quot;local.large.volume_gb\&quot;&quot;</code></li>
  </ul>