Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Servers, nginx and Docker

kubectl on Amazon EKS says the server asked for credentials

Your error

error validating data: failed to download openapi: the server has asked for the client to provide credentials; if you choose to ignore these errors, turn validation off with --validate=false2 more lines it can show

Fix it fast

Most common cause, 1 of 4

The IAM role that runs kubectl has no access entry

Create an access entry for the role, written in the form arn:aws:iam::<account>:role/<name>, then attach an access policy to it.

Fix
aws eks create-access-entry --cluster-name my-cluster \
  --principal-arn arn:aws:iam::111122223333:role/my-jenkins-role --type STANDARD
aws eks associate-access-policy --cluster-name my-cluster \
  --principal-arn arn:aws:iam::111122223333:role/my-jenkins-role \
  --access-scope type=cluster \
  --policy-arn arn:aws:eks::aws:cluster-access-policy/AmazonEKSEditPolicy
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

"The server has asked for the client to provide credentials" is Kubernetes wording for HTTP 401. On Amazon EKS it means the IAM principal behind kubectl, often a Jenkins or CI role, maps to no Kubernetes user. Run aws sts get-caller-identity, then create an EKS access entry (or aws-auth mapping) for that ARN on Kubernetes 1.34 to 1.37 clusters.

  • Applies to Amazon EKS, Kubernetes 1.34 to 1.37, kubectl 1.37, AWS CLI 2.41
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Run these lines inside the failing job, as the same user and with the same environment. The first prints who kubectl will sign in as. The second shows how the cluster maps IAM to Kubernetes, and the third lists the principals it already knows.

Run this first
aws sts get-caller-identity --query Arn --output text
aws eks describe-cluster --name my-cluster --query cluster.accessConfig
aws eks list-access-entries --cluster-name my-cluster

Then find the line you see in this list and open its fix:

  1. 1The IAM role that runs kubectl has no access entryauthenticationMode is API or API_AND_CONFIG_MAP, and the role named in the get-caller-identity output is absent from list-access-entries
  2. 2kubectl authenticates as some other principal, not the granted roleget-caller-identity prints an ARN you did not expect, such as the EC2 instance role instead of the deploy role, or a user from another account
  3. 3The cluster only reads the aws-auth ConfigMap, and the role is not in itdescribe-cluster shows authenticationMode CONFIG_MAP, and create-access-entry is refused for the cluster
  4. 4The IAM role was deleted and created again under the same nameThe role ARN is listed in list-access-entries, yet the job still gets 401 after someone rebuilt the role (for example by redeploying an IaC stack)

What does the error look like?

  • error validating data: failed to download openapi: the server has asked for the client to provide credentials; if you choose to ignore these errors, turn validation off with --validate=false
  • error: You must be logged in to the server (the server has asked for the client to provide credentials)
  • error: You must be logged in to the server (Unauthorized)

Cause 1 of 4

The IAM role that runs kubectl has no access entry

You seeauthenticationMode is API or API_AND_CONFIG_MAP, and the role named in the get-caller-identity output is absent from list-access-entries

Check

kubectl reports the server has asked for the client to provide credentials because the cluster cannot turn the calling IAM role into a Kubernetes identity. AWS's troubleshooting page names a missing access entry, or one without groups or an access policy, as the reason. CI roles are the usual gap: the person who built the cluster has access, the Jenkins role does not.

Check
aws eks list-access-entries --cluster-name my-cluster --output text | grep my-jenkins-role

Fix

Create an access entry for the role, written in the form arn:aws:iam::<account>:role/<name>, then attach an access policy to it. Pick the smallest policy that covers your deploys, and use namespace scope when the pipeline only touches a few namespaces.

Fix
aws eks create-access-entry --cluster-name my-cluster \
  --principal-arn arn:aws:iam::111122223333:role/my-jenkins-role --type STANDARD
aws eks associate-access-policy --cluster-name my-cluster \
  --principal-arn arn:aws:iam::111122223333:role/my-jenkins-role \
  --access-scope type=cluster \
  --policy-arn arn:aws:eks::aws:cluster-access-policy/AmazonEKSEditPolicy

Verify

The role now shows in the access entry list, and a read call from the job returns objects instead of a 401.

Verify
kubectl get svc

Confirmed by docs.aws.amazon.com

Did this fix it?

Cause 2 of 4

kubectl authenticates as some other principal, not the granted role

You seeget-caller-identity prints an ARN you did not expect, such as the EC2 instance role instead of the deploy role, or a user from another account

Check

The kubeconfig that aws eks update-kubeconfig writes calls aws eks get-token, and that command uses the default AWS credential chain unless a role is named. A kubeconfig copied from a laptop, or a job missing its AWS_PROFILE, ends up sending a principal the cluster has never seen.

Check
grep -A12 'exec:' ~/.kube/config

Fix

Write the kubeconfig inside the job itself and name the role that has cluster access with --role-arn. Every token kubectl asks for is then issued for that role, whatever credentials the agent machine starts with.

Fix
aws eks update-kubeconfig --region us-east-1 --name my-cluster --role-arn arn:aws:iam::111122223333:role/my-deploy-role

Verify

The exec block in the kubeconfig now lists the role ARN, and kubectl apply gets past validation with no openapi download error.

Verify
kubectl apply -f deploy.yaml

Confirmed by docs.aws.amazon.com

Did this fix it?

Cause 3 of 4

The cluster only reads the aws-auth ConfigMap, and the role is not in it

You seedescribe-cluster shows authenticationMode CONFIG_MAP, and create-access-entry is refused for the cluster

Check

Older clusters authenticate only through the aws-auth ConfigMap. AWS's troubleshooting guide says that in this mode a principal without an entry there, or with an entry that maps to no bound group, gets the same unauthorized reply.

Check
eksctl get iamidentitymapping --cluster my-cluster

Fix

Switch the cluster to API_AND_CONFIG_MAP so access entries work while the existing ConfigMap keeps working. Wait for the cluster to return to Active, then add the access entry from the first cause. AWS notes the change cannot be undone back to a mode without the API.

Fix
aws eks update-cluster-config --name my-cluster --access-config authenticationMode=API_AND_CONFIG_MAP

Verify

describe-cluster now reports API_AND_CONFIG_MAP and create-access-entry succeeds.

Verify
aws eks describe-cluster --name my-cluster --query cluster.accessConfig.authenticationMode

Confirmed by docs.aws.amazon.com

Did this fix it?

Cause 4 of 4

The IAM role was deleted and created again under the same name

You seeThe role ARN is listed in list-access-entries, yet the job still gets 401 after someone rebuilt the role (for example by redeploying an IaC stack)

Check

EKS stores the hidden role ID with each access entry, not only the ARN. A recreated role has a new ID, which get-caller-identity shows as UserId, so the old entry no longer matches even though the ARN text is identical.

Check
aws sts get-caller-identity --query UserId --output text

Fix

Remove the stale entry and add it back so EKS records the new role ID. Then repeat the associate-access-policy step from the first cause.

Fix
aws eks delete-access-entry --cluster-name my-cluster \
  --principal-arn arn:aws:iam::111122223333:role/my-jenkins-role
aws eks create-access-entry --cluster-name my-cluster \
  --principal-arn arn:aws:iam::111122223333:role/my-jenkins-role --type STANDARD

Verify

Once the access policy is associated again, the pipeline's kubectl call goes through.

Verify
kubectl get svc

Confirmed by docs.aws.amazon.com

Did this fix it?

How do I stop it coming back?

  • Create the CI role's access entry in the same Terraform or CloudFormation stack as the role, so a rebuilt role always gets a fresh entry.
  • Start every deploy job with aws sts get-caller-identity in the log, so a wrong identity is visible before kubectl runs.
  • Generate the kubeconfig inside the job with --role-arn instead of storing a copied kubeconfig as a Jenkins secret.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: Dispatch Pro, Raheeb, Loade Driver.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.