All guides

Workload identity on EKS, AKS and GKE: pods without cloud keys

Amazon EKS, AKS and GKE can each give a pod cloud credentials through its Kubernetes service account. No access key is created and distributed to the containers.

Hands on a laptop beside an open notebook

The shared idea

On all three platforms the mapping starts from the Kubernetes service account that the pod uses. Amazon EKS associates an IAM role with a service account. AKS federates the token of a service account with Microsoft Entra ID. GKE grants IAM roles to a principal that names the service account. What differs is where you record the mapping.

Amazon EKS with Pod Identity

AWS recommends EKS Pod Identity whenever possible. Each association maps one IAM role to one service account, in one namespace, in one cluster. The associations are configured in Amazon EKS, and this method uses no OIDC identity provider.

The role's trust policy is the one AWS documents:

JSON

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "AllowEksAuthToAssumeRoleForPodIdentity",
      "Effect": "Allow",
      "Principal": {
        "Service": "pods.eks.amazonaws.com"
      },
      "Action": [
        "sts:AssumeRole",
        "sts:TagSession"
      ]
    }
  ]
}

Shell

aws eks create-pod-identity-association --cluster-name my-cluster \
  --role-arn arn:aws:iam::111122223333:role/my-role \
  --namespace default --service-account my-service-account

The Amazon EKS Pod Identity Agent runs on each node. You set it up once for each cluster, and that step is not needed when EKS Auto Mode is enabled. For the same application in several clusters, you make identical associations in each cluster and leave the role's trust policy unchanged.

EKS Pod Identity is available only on Amazon EKS, on worker nodes that are Linux Amazon EC2 instances. Pods on Fargate and on Windows EC2 instances are not supported. A cluster supports up to 5,000 associations.

IAM roles for service accounts on EKS

IAM roles for service accounts (IRSA) uses OpenID Connect. Each cluster has an OIDC issuer URL, and a unique OIDC provider has to be created in IAM for each cluster. In AWS's example, the role's trust policy allows sts:AssumeRoleWithWebIdentity with two conditions on that provider: sub equals system:serviceaccount:default:my-service-account, and aud equals sts.amazonaws.com.

The mapping is an annotation on the service account. This is the documented command:

Shell

kubectl annotate serviceaccount -n $namespace $service_account \
  eks.amazonaws.com/role-arn=arn:aws:iam::$account_id:role/my-role

IRSA works, according to AWS, on Amazon EKS, on Amazon EKS Anywhere, on Red Hat OpenShift Service on AWS and on Kubernetes clusters you manage yourself on EC2. With IRSA, the role's trust policy has to be updated with the new cluster's OIDC provider each time the role is used in a new cluster.

AKS with Microsoft Entra Workload ID

On an AKS Standard cluster you enable the OIDC issuer and workload identity with --enable-oidc-issuer and --enable-workload-identity, on az aks create or az aks update. On AKS Automatic clusters both are preconfigured.

Microsoft's walkthrough uses a user-assigned managed identity and makes the mapping in three places. The first is a federated identity credential on the managed identity. The names here are yours to choose:

Shell

az identity federated-credential create --name my-app-credential \
  --identity-name my-app-identity --resource-group my-rg \
  --issuer <oidc-issuer-url> \
  --subject system:serviceaccount:default:my-service-account \
  --audience api://AzureADTokenExchange

The second is an annotation on the service account, which carries the client ID of the identity:

YAML

apiVersion: v1
kind: ServiceAccount
metadata:
  annotations:
    azure.workload.identity/client-id: "<client-id>"
  name: "my-service-account"
  namespace: "default"

The third is the label azure.workload.identity/use: "true" in the pod template, on a pod whose serviceAccountName is that service account. The label is required. Only pods with this label are mutated by the workload identity webhook, which injects the projected service account token volume and environment variables such as AZURE_FEDERATED_TOKEN_FILE.

A managed identity can have 20 federated identity credentials at most.

GKE with Workload Identity Federation for GKE

In Autopilot clusters, Workload Identity Federation for GKE is always enabled. On a Standard cluster you enable it with --workload-pool=PROJECT_ID.svc.id.goog on gcloud container clusters update, and a node pool uses the GKE metadata server when it has --workload-metadata=GKE_METADATA.

Google's best practice is to use IAM principal identifiers. You grant a role to a principal that names the namespace and the Kubernetes service account. The command below is Google's example, with the long member string built from two shell variables:

Shell

POOL="projects/<project-number>/locations/global/workloadIdentityPools"
MEMBER="principal://iam.googleapis.com/${POOL}/<project-id>.svc.id.goog"

gcloud storage buckets add-iam-policy-binding gs://<bucket> \
  --role=roles/storage.objectViewer \
  --member="${MEMBER}/subject/ns/default/sa/my-service-account" \
  --condition=None

Google notes that this federated identity has limitations with some Google Cloud APIs. For those, you link the Kubernetes service account to an IAM service account. That takes an allow policy with the role roles/iam.workloadIdentityUser for the member serviceAccount:PROJECT_ID.svc.id.goog[NAMESPACE/KSA_NAME], and the annotation iam.gke.io/gcp-service-account on the Kubernetes service account. Both are required.

GKE creates one fixed workload identity pool for a project. Workloads with the same namespace and service account name in two clusters of that project are the same identity to IAM. Google calls this identity sameness. It states two things: every cluster in the pool has to be one you can trust, and conditional IAM policies can narrow access to specific clusters.

What to check on any platform

  1. The service account. On GKE, a workload with no service account assigned gets the default service account of its namespace. On EKS, only pods that use the associated service account get the role's permissions. Give each workload its own service account.
  2. Node credentials. On EKS, if access to the instance metadata service is not restricted, a pod's containers also have access to the node's IAM role. Restrict that access.
  3. The SDK. AWS says to check two things in the workload: its AWS SDK is a supported version, and it uses the default credential chain. Microsoft lists a minimum Azure Identity library version for each language.

Sources

Want a second pair of eyes on yours? We do this work for a living.

Book a call

More guides