Chris Snow
Chris Snow

Reputation: 24626

How to retrieve the EKS kubeconfig?

I have defined an aws_eks_cluster and aws_eks_node_group as follows:

resource "aws_eks_cluster" "example" {
  count = var.create_eks_cluster ? 1 : 0
  name     = local.cluster_name
  role_arn = aws_iam_role.example[count.index].arn

  vpc_config {
    subnet_ids = [
      aws_subnet.main2.id, 
      aws_subnet.main3.id
    ]
    security_group_ids = [
      module.network.security_group_allow_all_from_client_ip,
      module.network.security_group_main_id
    ]
    endpoint_private_access = true
    endpoint_public_access = false
  }

  # Ensure that IAM Role permissions are created before and deleted after EKS Cluster handling.
  # Otherwise, EKS will not be able to properly delete EKS managed EC2 infrastructure such as Security Groups.
  depends_on = [
    aws_iam_role_policy_attachment.example-AmazonEKSClusterPolicy,
    aws_iam_role_policy_attachment.example-AmazonEKSVPCResourceController,
  ]
}


resource "aws_eks_node_group" "example" {
  count = var.create_eks_cluster ? 1 : 0
  cluster_name    = aws_eks_cluster.example[count.index].name
  node_group_name = random_uuid.deployment_uuid.result
  node_role_arn   = aws_iam_role.eks-node-group-example[count.index].arn
  subnet_ids      = [
    aws_subnet.main2.id, 
    aws_subnet.main3.id
    ]

  scaling_config {
    desired_size = 1
    max_size     = 5
    min_size     = 1
  }

  # Ensure that IAM Role permissions are created before and deleted after EKS Node Group handling.
  # Otherwise, EKS will not be able to properly delete EC2 Instances and Elastic Network Interfaces.
  depends_on = [
    aws_iam_role_policy_attachment.example-AmazonEKSWorkerNodePolicy,
    aws_iam_role_policy_attachment.example-AmazonEKS_CNI_Policy,
    aws_iam_role_policy_attachment.example-AmazonEC2ContainerRegistryReadOnly,
  ]
}

How can I retrieve the KubeConfig?

I have seen that the kubeconfig is available as an output on the eks module.

Do I need to replace aws_eks_cluster and aws_eks_node_group with the eks module?

Upvotes: 3

Views: 6090

Answers (1)

bpdohall
bpdohall

Reputation: 1051

The EKS module composes a kubeconfig based on a template.

You can include that template alongside your terraform code.

You will need to provide default values for all the variables in the templatefile function call and reference your own EKS resource name. It's fine to drop all the coalescelist functions too.

e.g.:

locals {
  kubeconfig = templatefile("templates/kubeconfig.tpl", {
    kubeconfig_name                   = local.kubeconfig_name
    endpoint                          = aws_eks_cluster.example.endpoint
    cluster_auth_base64               = aws_eks_cluster.example.certificate_authority[0].data
    aws_authenticator_command         = "aws-iam-authenticator"
    aws_authenticator_command_args    = ["token", "-i", aws_eks_cluster.example.name]
    aws_authenticator_additional_args = []
    aws_authenticator_env_variables   = {}
  })
}

output "kubeconfig" { value = local.kubeconfig }

Upvotes: 2

Related Questions