terragrunt, eks-aws module and data "aws_eks_cluster_auth" fails during plan if cluster is not yet created

50 Views Asked by At

I'm writing terragrunt code that should use terraform aws eks module to create the cluster and then use helm provider to install cert-manager and ingress-nginx there. If I create cluster first there's no problem. However, if nothing exists before the first terragrunt run-all plan I get

│ Error: Reference to undeclared resource
│
│   on helm_provider.tf line 13, in provider "helm":
│   13:     host                   = data.aws_eks_cluster.cluster.endpoint
│
│ A data resource "aws_eks_cluster" "cluster" has not been declared in the
│ root module.
╵
╷
│ Error: Reference to undeclared resource
│
│   on helm_provider.tf line 14, in provider "helm":
│   14:     cluster_ca_certificate = base64decode(data.aws_eks_cluster.cluster.certificate_authority[0].data)
│
│ A data resource "aws_eks_cluster" "cluster" has not been declared in the
│ root module.
╵
╷
│ Error: Reference to undeclared resource
│
│   on helm_provider.tf line 15, in provider "helm":
│   15:     token                  = data.aws_eks_cluster_auth.cluster.token
│
│ A data resource "aws_eks_cluster_auth" "cluster" has not been declared in
│ the root module.
╵

here's my terragrunt.hcl for installing addons

terraform {
  source = "../../../modules/k8s-addons"
}

include "root" {
  path = find_in_parent_folders()
}

include "env" {
  path   = find_in_parent_folders("env.hcl")
  expose = true
}

inputs = {
  cluster_name = dependency.eks.outputs.cluster_name
}

dependency "eks" {
  config_path = "../eks"

  mock_outputs = {
    cluster_name     = "eks-cluster"
    cluster_endpoint = "https://eks-cluster.k8s.local"
    cluster_version  = "1.29"
  }
}

generate "helm_provider" {
  path      = "helm_provider.tf"
  if_exists = "overwrite_terragrunt"
  contents  = <<EOF

data "aws_eks_cluster" "eks" {
    name = "${dependency.eks.outputs.cluster_name}"
}

data "aws_eks_cluster_auth" "eks" {
    name = "${dependency.eks.outputs.cluster_name}"
}

provider "helm" {
  kubernetes {
    host                   = data.aws_eks_cluster.cluster.endpoint
    cluster_ca_certificate = base64decode(data.aws_eks_cluster.cluster.certificate_authority[0].data)
    token                  = data.aws_eks_cluster_auth.cluster.token
  }
}
EOF
}

I know I can exclude-dir when I run terragrunt run-all plan. I also tried to the method described here: Error: reading EKS Cluster (): couldn't find resource when running terraform plan but it doesn't seem to work with terragrunt specifically (or I'm making a different error there).

what can I do to achieve my goal i.e. running terragrunt run-all plan with no resource created and it not failing?

0

There are 0 best solutions below