<p>You have commented out the <code>count</code> check of the <code>aws_eks_cluster.eks</code> data source that would make the data source conditional, depending on the <code>k8s_cluster_type</code> setting. Uncommenting the count argument should make it skip the data source and not complain about it not being found.</p>
<pre><code>data &quot;aws_eks_cluster&quot; &quot;eks&quot; {
  count = var.k8s_cluster_type == &quot;oke&quot; ? 0 : 1
  name  = var.eks_cluster_name
}
</code></pre>

CC BY-SA 4.0
2022-11-15T01:08:43.490Z
2022-11-15T01:08:43.490Z
Oldskool
<p>I want to use same terraform code for both kubernetes clusters oracle (OKE) and AWS (EKS) and install the argocd helm chart on it, I given my code below for your reference, now the problem I am facing now, if I set k8s_cluster_type to &quot;eks&quot;, so everything works fine but when I set the k8s_cluster_type to &quot;oke&quot;, it fails with given below error,</p>
<p>I get this error:-</p>
<pre><code>│ Error: error reading EKS Cluster (oh-appb-01): couldn't find resource
│ 
│   with data.aws_eks_cluster.eks,
│   on main.tf line 137, in data &quot;aws_eks_cluster&quot; &quot;eks&quot;:
│  137: data &quot;aws_eks_cluster&quot; &quot;eks&quot; {
│ 
</code></pre>
<p>What I want to do:-</p>
<p>In case if I give eks_cluster_name (dummy_cluster_name) or eks cluster doesn't exist, still this code should run as same like oke, because if I assign dummy_name oke cluster or oke cluster doens't exist and input variable k8s_cluster_type to &quot;eks&quot; this terraform runs successfully, Could you suggest, how can I use same terraform for both kubernetes cluster to install argocd helm chart</p>
<pre><code>locals {
  argocd_ns = &quot;argocd&quot;


  kubeconfig_str = var.k8s_cluster_type == &quot;oke&quot; ? data.oci_containerengine_cluster_kube_config.k8s.content : data.template_file.temp_kubeconfig_eks.rendered
  kubeconfig = yamldecode( local.kubeconfig_str )

  exec_cli = var.k8s_cluster_type == &quot;oke&quot;? &quot;oci&quot; : &quot;aws&quot;

  cluster_cert = var.k8s_cluster_type == &quot;oke&quot; ? base64decode(local.kubeconfig[&quot;clusters&quot;][0][&quot;cluster&quot;][&quot;certificate-authority-data&quot;]) : base64decode(data.aws_eks_cluster.eks.certificate_authority[0].data)
  cluster_endpoint = var.k8s_cluster_type == &quot;oke&quot; ? local.kubeconfig[&quot;clusters&quot;][0][&quot;cluster&quot;][&quot;server&quot;] : data.aws_eks_cluster.eks.endpoint


  exec_args_oke = [
    local.kubeconfig[&quot;users&quot;][0][&quot;user&quot;][&quot;exec&quot;][&quot;args&quot;][0],
    local.kubeconfig[&quot;users&quot;][0][&quot;user&quot;][&quot;exec&quot;][&quot;args&quot;][1],
    local.kubeconfig[&quot;users&quot;][0][&quot;user&quot;][&quot;exec&quot;][&quot;args&quot;][2],
    local.kubeconfig[&quot;users&quot;][0][&quot;user&quot;][&quot;exec&quot;][&quot;args&quot;][3],
    local.kubeconfig[&quot;users&quot;][0][&quot;user&quot;][&quot;exec&quot;][&quot;args&quot;][4],
    local.kubeconfig[&quot;users&quot;][0][&quot;user&quot;][&quot;exec&quot;][&quot;args&quot;][5],
    local.kubeconfig[&quot;users&quot;][0][&quot;user&quot;][&quot;exec&quot;][&quot;args&quot;][6]
  ]

  exec_args_eks = [&quot;eks&quot;, &quot;get-token&quot;, &quot;--cluster-name&quot;, var.eks_cluster_name]
  exec_args = var.k8s_cluster_type == &quot;oke&quot; ? local.exec_args_oke : local.exec_args_eks



}




data &quot;oci_containerengine_cluster_kube_config&quot; &quot;k8s&quot; {
  #count = var.k8s_cluster_type == &quot;oke&quot; ? 1 : 0
  cluster_id = var.k8s_cluster_id
}


resource &quot;local_file&quot; &quot;temp_kubeconfig&quot; {
  count = var.k8s_cluster_type == &quot;oke&quot; ? 1 : 0
  content  = local.kubeconfig_str
  filename = &quot;${path.module}/kubeconfig_temp&quot;
}

resource &quot;local_file&quot; &quot;temp_kubeconfig_eks&quot; {
  count = var.k8s_cluster_type == &quot;oke&quot; ? 0 : 1
  content  = data.template_file.temp_kubeconfig_eks.rendered
  filename = &quot;${path.module}/kubeconfig_temp&quot;
}


resource &quot;kubernetes_namespace&quot; &quot;argocd&quot; {
  metadata {
    name = local.argocd_ns
  }
}

resource &quot;kubernetes_secret&quot; &quot;root_repo&quot; {
  depends_on = [kubernetes_namespace.argocd]
  metadata {
    name = var.argocd_root_repo.name
    namespace = local.argocd_ns
    labels = {
      &quot;argocd.argoproj.io/secret-type&quot; = &quot;repository&quot;
    }
  }
  data = {
    url = var.argocd_root_repo.url
    name = var.argocd_root_repo.name
    password = var.argocd_root_repo_token
    username = var.argocd_root_repo.name

  }
}


data &quot;template_file&quot; &quot;argocd-helm-values-override&quot; {
  template = file(&quot;${path.module}/templates/argocd-helm-values-override.tpl&quot;)
  vars = {
    argocd_ns = local.argocd_ns
    repo_url = var.argocd_root_repo.url
    repo_path = var.argocd_root_repo.path
    repo_revision = var.argocd_root_repo.revision
  }
}

resource &quot;helm_release&quot; &quot;argocd&quot; {
  depends_on = [kubernetes_namespace.argocd, kubernetes_secret.root_repo ]
  version    = var.argocd_release.chart_version
  name       = var.argocd_release.release_name
  chart      = var.argocd_release.chart_name
  repository = var.argocd_release.chart_repo
  namespace  = local.argocd_ns
  values = [
    data.template_file.argocd-helm-values-override.rendered
  ]
}


data &quot;template_file&quot; &quot;temp_kubeconfig_eks&quot; {
  count = var.k8s_cluster_type == &quot;oke&quot; ? 0 : 1
  template = file(&quot;${path.module}/templates/kubeconfig_eks.tpl&quot;)
  vars = {
    cluster_url = data.aws_eks_cluster.eks.endpoint
    cluster_region = var.region
    cluster_cert_authority_data = data.aws_eks_cluster.eks.certificate_authority.0.data
    cluster_name = var.eks_cluster_name
  }
}

provider &quot;kubernetes&quot; {
  cluster_ca_certificate = local.cluster_cert
  host                   = local.cluster_endpoint
  exec {
    api_version = &quot;client.authentication.k8s.io/v1beta1&quot;
    command     = local.exec_cli
    args        = local.exec_args
  }
}
provider &quot;helm&quot; {
  kubernetes {
    cluster_ca_certificate = local.cluster_cert
    host                   = local.cluster_endpoint
    exec {
      api_version = &quot;client.authentication.k8s.io/v1beta1&quot;
      command     = local.exec_cli
      args        = local.exec_args
    }
  }
}


data &quot;aws_eks_cluster&quot; &quot;eks&quot; {
  #count = var.k8s_cluster_type == &quot;oke&quot; ? 0 : 1
  name = var.eks_cluster_name
}

data &quot;aws_eks_cluster_auth&quot; &quot;eks&quot; {
  #count = var.k8s_cluster_type == &quot;oke&quot; ? 0 : 1
  name = var.eks_cluster_name 
}
</code></pre>
<p>*.tfvars file:-</p>
<pre><code>8s_cluster_id =&quot;ocid1.cluster.oc1.xxx.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&quot;
k8s_cluster_type = &quot;oke&quot;

argocd_root_repo = {
  name = &quot;argocd-xxxx-xxxx-config&quot;,
  url = &quot;https://github.com/xxxxx/xxxx/argocd-xxxx-xxxx-config&quot;,
  path = &quot;clusters/localsand1/apps&quot;,
  revision = &quot;master&quot;
}

region = &quot;us-east-1&quot;
eks_cluster_name = &quot;oh-appb-01&quot;

</code></pre>

<kubernetes><terraform><eks><kubernetes-helm><terragrunt>
How to use same terraform code for both kubernetes clusters Oracle (OKE) and AWS (EKS)?
San
# How to use same terraform code for both kubernetes clusters Oracle (OKE) and AWS (EKS)?

(Asked by: username_0 on 2022-09-18)

I want to use same terraform code for both kubernetes clusters oracle (OKE) and AWS (EKS) and install the argocd helm chart on it, I given my code below for your reference, now the problem I am facing now, if I set k8s_cluster_type to "eks", so everything works fine but when I set the k8s_cluster_type to "oke", it fails with given below error,
I get this error:-
│ Error: error reading EKS Cluster (oh-appb-01): couldn't find resource
│ 
│   with data.aws_eks_cluster.eks,
│   on main.tf line 137, in data "aws_eks_cluster" "eks":
│  137: data "aws_eks_cluster" "eks" {
│ 

What I want to do:-
In case if I give eks_cluster_name (dummy_cluster_name) or eks cluster doesn't exist, still this code should run as same like oke, because if I assign dummy_name oke cluster or oke cluster doens't exist and input variable k8s_cluster_type to "eks" this terraform runs successfully, Could you suggest, how can I use same terraform for both kubernetes cluster to install argocd helm chart
locals {
  argocd_ns = "argocd"


  kubeconfig_str = var.k8s_cluster_type == "oke" ? data.oci_containerengine_cluster_kube_config.k8s.content : data.template_file.temp_kubeconfig_eks.rendered
  kubeconfig = yamldecode( local.kubeconfig_str )

  exec_cli = var.k8s_cluster_type == "oke"? "oci" : "aws"

  cluster_cert = var.k8s_cluster_type == "oke" ? base64decode(local.kubeconfig["clusters"][0]["cluster"]["certificate-authority-data"]) : base64decode(data.aws_eks_cluster.eks.certificate_authority[0].data)
  cluster_endpoint = var.k8s_cluster_type == "oke" ? local.kubeconfig["clusters"][0]["cluster"]["server"] : data.aws_eks_cluster.eks.endpoint


  exec_args_oke = [
    local.kubeconfig["users"][0]["user"]["exec"]["args"][0],
    local.kubeconfig["users"][0]["user"]["exec"]["args"][1],
    local.kubeconfig["users"][0]["user"]["exec"]["args"][2],
    local.kubeconfig["users"][0]["user"]["exec"]["args"][3],
    local.kubeconfig["users"][0]["user"]["exec"]["args"][4],
    local.kubeconfig["users"][0]["user"]["exec"]["args"][5],
    local.kubeconfig["users"][0]["user"]["exec"]["args"][6]
  ]

  exec_args_eks = ["eks", "get-token", "--cluster-name", var.eks_cluster_name]
  exec_args = var.k8s_cluster_type == "oke" ? local.exec_args_oke : local.exec_args_eks



}




data "oci_containerengine_cluster_kube_config" "k8s" {
  #count = var.k8s_cluster_type == "oke" ? 1 : 0
  cluster_id = var.k8s_cluster_id
}


resource "local_file" "temp_kubeconfig" {
  count = var.k8s_cluster_type == "oke" ? 1 : 0
  content  = local.kubeconfig_str
  filename = "${path.module}/kubeconfig_temp"
}

resource "local_file" "temp_kubeconfig_eks" {
  count = var.k8s_cluster_type == "oke" ? 0 : 1
  content  = data.template_file.temp_kubeconfig_eks.rendered
  filename = "${path.module}/kubeconfig_temp"
}


resource "kubernetes_namespace" "argocd" {
  metadata {
    name = local.argocd_ns
  }
}

resource "kubernetes_secret" "root_repo" {
  depends_on = [kubernetes_namespace.argocd]
  metadata {
    name = var.argocd_root_repo.name
    namespace = local.argocd_ns
    labels = {
      "argocd.argoproj.io/secret-type" = "repository"
    }
  }
  data = {
    url = var.argocd_root_repo.url
    name = var.argocd_root_repo.name
    password = var.argocd_root_repo_token
    username = var.argocd_root_repo.name

  }
}


data "template_file" "argocd-helm-values-override" {
  template = file("${path.module}/templates/argocd-helm-values-override.tpl")
  vars = {
    argocd_ns = local.argocd_ns
    repo_url = var.argocd_root_repo.url
    repo_path = var.argocd_root_repo.path
    repo_revision = var.argocd_root_repo.revision
  }
}

resource "helm_release" "argocd" {
  depends_on = [kubernetes_namespace.argocd, kubernetes_secret.root_repo ]
  version    = var.argocd_release.chart_version
  name       = var.argocd_release.release_name
  chart      = var.argocd_release.chart_name
  repository = var.argocd_release.chart_repo
  namespace  = local.argocd_ns
  values = [
    data.template_file.argocd-helm-values-override.rendered
  ]
}


data "template_file" "temp_kubeconfig_eks" {
  count = var.k8s_cluster_type == "oke" ? 0 : 1
  template = file("${path.module}/templates/kubeconfig_eks.tpl")
  vars = {
    cluster_url = data.aws_eks_cluster.eks.endpoint
    cluster_region = var.region
    cluster_cert_authority_data = data.aws_eks_cluster.eks.certificate_authority.0.data
    cluster_name = var.eks_cluster_name
  }
}

provider "kubernetes" {
  cluster_ca_certificate = local.cluster_cert
  host                   = local.cluster_endpoint
  exec {
    api_version = "client.authentication.k8s.io/v1beta1"
    command     = local.exec_cli
    args        = local.exec_args
  }
}
provider "helm" {
  kubernetes {
    cluster_ca_certificate = local.cluster_cert
    host                   = local.cluster_endpoint
    exec {
      api_version = "client.authentication.k8s.io/v1beta1"
      command     = local.exec_cli
      args        = local.exec_args
    }
  }
}


data "aws_eks_cluster" "eks" {
  #count = var.k8s_cluster_type == "oke" ? 0 : 1
  name = var.eks_cluster_name
}

data "aws_eks_cluster_auth" "eks" {
  #count = var.k8s_cluster_type == "oke" ? 0 : 1
  name = var.eks_cluster_name 
}

*.tfvars file:-
8s_cluster_id ="ocid1.cluster.oc1.xxx.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
k8s_cluster_type = "oke"

argocd_root_repo = {
  name = "argocd-xxxx-xxxx-config",
  url = "https://github.com/xxxxx/xxxx/argocd-xxxx-xxxx-config",
  path = "clusters/localsand1/apps",
  revision = "master"
}

region = "us-east-1"
eks_cluster_name = "oh-appb-01"

**Tags:** kubernetes, terraform, eks, kubernetes-helm, terragrunt


---

## 1 answers

### Answer by username_1 on 2022-11-15. Score: 0

You have commented out the count check of the aws_eks_cluster.eks data source that would make the data source conditional, depending on the k8s_cluster_type setting. Uncommenting the count argument should make it skip the data source and not complain about it not being found.
data "aws_eks_cluster" "eks" {
  count = var.k8s_cluster_type == "oke" ? 0 : 1
  name  = var.eks_cluster_name
}


---

