<p>I am running into an issue where <code>terraform plan</code> recreates resources that don't need to be recreated every run. This is an issue because some of the steps depend on those resources being available, and since they are recreated with each run, the script fails to complete.</p>
<p>My setup is Github Actions, Linode LKE, Terraform Cloud.</p>
<p>My main.tf file looks like this:</p>
<pre><code>terraform {
  required_providers {
    linode = {
      source  = &quot;linode/linode&quot;
      version = &quot;=1.16.0&quot;
    }
    helm = {
      source = &quot;hashicorp/helm&quot;
      version = &quot;=2.1.0&quot;
    }
  }
  backend &quot;remote&quot; {
    hostname      = &quot;app.terraform.io&quot;
    organization  = &quot;MY-ORG-HERE&quot;
    workspaces {
      name = &quot;MY-WORKSPACE-HERE&quot;
    }    
  }
}

provider &quot;linode&quot; {
}

provider &quot;helm&quot; {
  debug   = true
  kubernetes {
    config_path = &quot;${local_file.kubeconfig.filename}&quot;
  }
}

resource &quot;linode_lke_cluster&quot; &quot;lke_cluster&quot; {
    label       = &quot;MY-LABEL-HERE&quot;
    k8s_version = &quot;1.21&quot;
    region      = &quot;us-central&quot;

    pool {
        type  = &quot;g6-standard-2&quot;
        count = 3
    }
}
</code></pre>
<p>and my outputs.tf file</p>
<pre><code>resource &quot;local_file&quot; &quot;kubeconfig&quot; {
  depends_on   = [linode_lke_cluster.lke_cluster]
  filename     = &quot;kube-config&quot;
  # filename     = &quot;${path.cwd}/kubeconfig&quot;
  content      = base64decode(linode_lke_cluster.lke_cluster.kubeconfig)
}

resource &quot;helm_release&quot; &quot;ingress-nginx&quot; {
  # depends_on   = [local_file.kubeconfig]
  depends_on = [linode_lke_cluster.lke_cluster, local_file.kubeconfig]
  name       = &quot;ingress&quot;
  repository = &quot;https://kubernetes.github.io/ingress-nginx&quot;
  chart      = &quot;ingress-nginx&quot;
}

resource &quot;null_resource&quot; &quot;custom&quot; {
  depends_on   = [helm_release.ingress-nginx]
  # change trigger to run every time
  triggers = {
    build_number = &quot;${timestamp()}&quot;
  }

  # download kubectl
  provisioner &quot;local-exec&quot; {
    command = &quot;curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl &amp;&amp; chmod +x kubectl&quot;
  }

  # apply changes
  provisioner &quot;local-exec&quot; {
    command = &quot;./kubectl apply -f ./k8s/ --kubeconfig ${local_file.kubeconfig.filename}&quot;
  }
}
</code></pre>
<p>In Github Actions, I'm running these steps:</p>
<pre><code>jobs:
  init-terraform:
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: ./terraform
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
        with:
          ref: 'privatebeta-kubes'
      - name: Setup Terraform
        uses: hashicorp/setup-terraform@v1
        with:
          cli_config_credentials_token: ${{ secrets.TERRAFORM_API_TOKEN }}
      - name: Terraform Init
        run: terraform init
      - name: Terraform Format Check
        run: terraform fmt -check -v
      - name: List terraform state
        run: terraform state list
      - name: Terraform Plan
        run: terraform plan
        id: plan
        env:
          LINODE_TOKEN: ${{ secrets.LINODE_TOKEN }}
</code></pre>
<p>When I look at the results of <code>terraform state list</code> I can see my resources:</p>
<pre><code>Run terraform state list
  terraform state list
  shell: /usr/bin/bash -e {0}
  env:
    TERRAFORM_CLI_PATH: /home/runner/work/_temp/3f9749b8-515b-4cb4-8053-1a6318496321
/home/runner/work/_temp/3f9749b8-515b-4cb4-8053-1a6318496321/terraform-bin state list
helm_release.ingress-nginx
linode_lke_cluster.lke_cluster
local_file.kubeconfig
null_resource.custom
</code></pre>
<p>But my <code>terraform plan</code> fails and the issue seems to stem from the fact that those resources try to get recreated.</p>
<pre><code>Run terraform plan
  terraform plan
  shell: /usr/bin/bash -e {0}
  env:
    TERRAFORM_CLI_PATH: /home/runner/work/_temp/3f9749b8-515b-4cb4-8053-1a6318496321
    LINODE_TOKEN: ***
/home/runner/work/_temp/3f9749b8-515b-4cb4-8053-1a6318496321/terraform-bin plan
Running plan in the remote backend. Output will stream here. Pressing Ctrl-C
will stop streaming the logs, but will not stop the plan running remotely.

Preparing the remote plan...
Waiting for the plan to start...

Terraform v1.0.2
on linux_amd64
Configuring remote state backend...
Initializing Terraform configuration...
linode_lke_cluster.lke_cluster: Refreshing state... [id=31946]
local_file.kubeconfig: Refreshing state... [id=fbb5520298c7c824a8069397ef179e1bc971adde]
helm_release.ingress-nginx: Refreshing state... [id=ingress]
╷
│ Error: Kubernetes cluster unreachable: stat kube-config: no such file or directory
│ 
│   with helm_release.ingress-nginx,
│   on outputs.tf line 8, in resource &quot;helm_release&quot; &quot;ingress-nginx&quot;:
│    8: resource &quot;helm_release&quot; &quot;ingress-nginx&quot; {
</code></pre>
<p>Is there a way to tell terraform it doesn't need to recreate those resources?</p>

shadyhill
<p>Regarding the actual error shown, <strong>Error: Kubernetes cluster unreachable: stat kibe-config: no such file or directory</strong>... which is referencing your outputs file... I found this which could help with your specific error: <a href="https://github.com/hashicorp/terraform-provider-helm/issues/418" rel="nofollow noreferrer">https://github.com/hashicorp/terraform-provider-helm/issues/418</a></p>
<p>1 other thing looks strange to me. Why does your outputs.tf refer to 'resources' &amp; not 'outputs'. Shouldn't your outputs.tf look like this?</p>
<pre><code>output &quot;local_file_kubeconfig&quot; {
  value = &quot;reference.to.resource&quot;
}
</code></pre>
<p>Also I see your state file / backend config looks like it's properly configured.</p>
<p>I recommend logging into your terraform cloud account to verify that the workspace is indeed there, as expected. It's the state file that tells terraform not to re-create the resources it manages.</p>
<p>If the resources are already there and terraform is trying to re-create them, that could indicate that those resources were created prior to using terraform or possibly within another terraform cloud workspace or plan.</p>
<p>Did you end up renaming your backend workspace at any point with this plan? I'm referring to your main.tf file, this part where it says <strong>MY-WORKSPACE-HERE</strong> :</p>
<pre><code>terraform {
  required_providers {
    linode = {
      source  = &quot;linode/linode&quot;
      version = &quot;=1.16.0&quot;
    }
    helm = {
      source = &quot;hashicorp/helm&quot;
      version = &quot;=2.1.0&quot;
    }
  }
  backend &quot;remote&quot; {
    hostname      = &quot;app.terraform.io&quot;
    organization  = &quot;MY-ORG-HERE&quot;
    workspaces {
      name = &quot;MY-WORKSPACE-HERE&quot;
    }    
  }
}
</code></pre>
<p>Unfortunately I am not a kurbenetes expert, so possibly more help can be used there.</p>
