<p>I have a terraform code as given below</p>
<pre><code>   locals {
    file_path = format(&quot;%s-%s&quot;, var.test1, var.test2)
    test_decode  =  yamldecode((data.github_repository_file.test.content))
   }
    data &quot;github_repository_file&quot; &quot;test&quot; {
     repository = &quot;test-repo&quot;
     branch     = &quot;develop&quot;
    file       = &quot;${local.file_path}/local/test.yaml&quot;
    }
  test_encode = ${yamlencode(local.test_decode.spec.names)}
</code></pre>
<p>This is working fine when a &quot;<strong>.spec.names</strong>&quot; attribute present in the test.yaml file. Since we are selecting the test.yaml based on local.file_path some times attribute .spec.names might not present in the test.yaml and the plan failing with  <strong>&quot;Error: Unsupported attribute&quot;</strong>. How to check &quot;<strong>.spec.names</strong>&quot; attribute  present in the test.yaml?</p>
<p>Updating the question to add yaml example</p>
<p><strong>Yaml with names attribute</strong></p>
<pre><code>apiVersion: helm.toolkit.gitops.io/v2beta1
kind: HelmRelease
metadata:
  name: &quot;test&quot;
  namespace: &quot;test-system&quot;
spec:
  chart:
    spec:
      chart: &quot;test-environment&quot;
      version: &quot;0.1.10&quot;
  names:
    key1: &quot;value1&quot;
    key2: &quot;value2&quot;
    key3: &quot;value3&quot;
    key4: &quot;value4&quot;
</code></pre>
<p><strong>YAML without names attribute</strong></p>
<pre><code>apiVersion: helm.toolkit.gitops.io/v2beta1
kind: HelmRelease
metadata:
  name: &quot;test&quot;
  namespace: &quot;test-system&quot;
spec:
  chart:
    spec:
      chart: &quot;test-environment&quot;
      version: &quot;0.1.10&quot;
</code></pre>

<p>You can use <a href="https://developer.hashicorp.com/terraform/language/functions/try" rel="nofollow noreferrer">try</a>:</p>
<pre><code>  test_encode = yamlencode(try(local.test_decode.spec.names, &quot;some_default_value&quot;))
</code></pre>
