简体   繁体   中英

Can we create EKS Cluster and Kubernetes deployment in a single terraform apply?

I want to create a EKS cluster using Terraform, build custom docker images and then perform Kubernetes deployments on the created cluster via terraform. I want to perform all the tasks with a single terraform apply. But I see that the kubernetes provider needs the details of cluster on initialization itself. Is there a way I can achieve both cluster creation and deployment using a single terraform apply, so that once the cluster is created, the cluster details can be passed to Kubernetes provider and then the pods are deployed.

Please let me know how I can achieve this?

I am doing this with ease, below is the pseudo code, you just need to be careful with the way you are using depends_on attribute with resources and try to encapsulate as much as possible

Kubernetes Provider in a separate file like kubernetes.tf

  host                   = module.eks.cluster_endpoint
  cluster_ca_certificate = base64decode(module.eks.cluster_certificate_authority_data)
  exec {
    api_version = "client.authentication.k8s.io/v1alpha1"
    command     = "aws"
    args = [
      "eks",
      "get-token",
      "--cluster-name",
      module.eks.cluster_id
    ]
  }
}

Assuming you've got the network setup, here I am relying on implicit dependencies rather than an explicit one.

module "eks" {
  source = "./modules/ekscluster"

  clustername     = module.clustername.cluster_name
  eks_version     = var.eks_version
  private_subnets = module.networking.private_subnets_id
  vpc_id          = module.networking.vpc_id
  environment     = var.environment
  instance_types  = var.instance_types

}

Creating k8s resources using the depends_on attribute.

resource "kubernetes_namespace_v1" "app" {
  metadata {
    annotations = {
      name = var.org_name
    }
    name = var.org_name
  }
  depends_on = [module.eks]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM