Update local kubeconfig to connect to an AWS EKS cluster

2 min read | by Jordi Prats

If we want to connect to an AWS EKS cluster using kubectl we need to update our kubeconfig (~/.kube/config) To do se we can use awscli

We just need to use eks update-kubeconfig specifying the name of the cluster we want to use:

aws eks update-kubeconfig --name clutername

We might also need to specify an AWS profile so the actual command would look like this:

$ aws eks --profile ampa update-kubeconfig --name prod
Updated context arn:aws:eks:eu-west-1:123456789123:cluster/prod in /home/pet2cattle/.kube/config

Once we have the kubeconfig updated we can use kubectl config to switch between clusters that we already have on our local kubeconfig:

$ kubectl config get-contexts
CURRENT   NAME                                                 CLUSTER                                              AUTHINFO                                             NAMESPACE
          arn:aws:eks:eu-west-1:123456789123:cluster/prod      arn:aws:eks:eu-west-1:123456789123:cluster/prod      arn:aws:eks:eu-west-1:123456789123:cluster/prod      ampa
          arn:aws:eks:eu-west-1:123456789123:cluster/test      arn:aws:eks:eu-west-1:123456789123:cluster/test      arn:aws:eks:eu-west-1:123456789123:cluster/test      
*         arn:aws:eks:eu-west-1:123456789123:cluster/preprod   arn:aws:eks:eu-west-1:123456789123:cluster/preprod   arn:aws:eks:eu-west-1:123456789123:cluster/preprod   
          minikube                                             minikube                                             minikube                                             default
$ kubectl config use-context  arn:aws:eks:eu-west-1:123456789123:cluster/prod 
Switched to context "arn:aws:eks:eu-west-1:123456789123:cluster/prod".
$ kubectl config get-contexts
CURRENT   NAME                                                 CLUSTER                                              AUTHINFO                                             NAMESPACE
*         arn:aws:eks:eu-west-1:123456789123:cluster/prod      arn:aws:eks:eu-west-1:123456789123:cluster/prod      arn:aws:eks:eu-west-1:123456789123:cluster/prod      ampa
          arn:aws:eks:eu-west-1:123456789123:cluster/test      arn:aws:eks:eu-west-1:123456789123:cluster/test      arn:aws:eks:eu-west-1:123456789123:cluster/test      
          arn:aws:eks:eu-west-1:123456789123:cluster/preprod   arn:aws:eks:eu-west-1:123456789123:cluster/preprod   arn:aws:eks:eu-west-1:123456789123:cluster/preprod   
          minikube                                             minikube                                             minikube                                             default

Posted on 04/10/2021