Terraform Cloud, multiple applies, EKS

2/6/2022

I'm having the following issue.

I'm trying to deploy an EKS cluster with EKS addons (vpc cni, kubeproxy) and k8s addons (autoscaler, fluentbit). My ADO repo that has the .tf files is connected to TF Cloud, meaning my state is remote. I've recently found out that k8s/terraform won't let you deploy an EKS cluster and its addons in the same run, for some reason (I would get many random errors, at random times). I had to have a separate terraform apply for eks and addons, respectively.

So, I've decided to modularize my code.

Before, my main folder looked like this:

├── Deployment
│   └── main.tf
│   └── eks.tf
│   └── addons.tf

Now, my folder looks like this:

└───Deployment   
│   │
│   └─── eks_deploy
│       │   main.tf
│       │   eks.tf
│   │
│   └─── addons_deploy
│       │   main.tf
│       │   addons.tf

And so, I initialize the same remote backend in both. So far, so good. Went ahead with a terraform apply in my eks_deploy folder. Deployed without problems, a clean EKS cluster with no addons. Now, it was time to deploy addons.

And that's where we have a problem. My main.tf files are the exact same in both folders. And the file looks like this:

terraform {

  backend "remote" {}

  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = ">= 3.66.0"
    }
    kubernetes = {
      source  = "hashicorp/kubernetes"
      version = ">= 2.7.1"
    }
    helm = {
      source  = "hashicorp/helm"
      version = ">= 2.4.1"
    }
  }
}

data "aws_eks_cluster" "cluster" {
  name = module.eks-ssp.eks_cluster_id
}

data "aws_eks_cluster_auth" "cluster" {
  name = module.eks-ssp.eks_cluster_id
}

# I am aware you're not supposed to hardcode your creds
provider "aws" {
  access_key = "xxx" 
  secret_key = "xxx"
  region     = "xxx"
  assume_role {
    role_arn = "xxx"
  }
}

provider "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
}

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

The EKS cluster deployed without problems because it had an eks.tf file that contains the module and all needed info to deploy a cluster. However, my addon deployment throws the following errors:

Error: Reference to undeclared module
on addons.tf line 60, in module "eks-ssp-kubernetes-addons":
60:     depends_on = [module.eks-ssp.self_managed_node_groups]
│ No module call named "eks-ssp" is declared in the root module.
Error: Reference to undeclared module
on main.tf line 22, in data "aws_eks_cluster" "cluster":
22:   name = module.eks-ssp.eks_cluster_id
│ No module call named "eks-ssp" is declared in the root module.
Error: Reference to undeclared module
on main.tf line 26, in data "aws_eks_cluster_auth" "cluster":
26:   name = module.eks-ssp.eks_cluster_id
│ No module call named "eks-ssp" is declared in the root module.

This is completely understandable, since the EKS cluster DOES NOT exist in the addon deployment, thus the addon deployment has no clue where to actually deploy those addons.

So my question is... how do I perform 2 different applies, for what's essentially the same resource (EKS), with each deployment being fully aware of each other (working as if they were in the same file and deployment? People mentioned "terragrunt", but I still don't understand how I could use it in my case, so if that's the solution you propose as well, please give a description of its way of use. There is also the following question - how would I connect the same repo, with 2 different folders/deployments, having separate applies? Does TF cloud even allow such a thing? At this point, I'm starting to think that a completely separate workspace, and hardcoded EKS values inside addons.tf is the only way. Thank you.

-- TFaws
amazon-eks
kubernetes
terraform

0 Answers