Sub topic 10
Workload identity on EKS with IRSA
This chapter assumes you know Pods, namespaces, and ServiceAccounts, and that you have seen projected tokens and the cluster's OIDC issuer from earlier chapters. You should also know what an AWS IAM role is.
What IRSA does
IAM Roles for Service Accounts (IRSA) lets a Pod on EKS call AWS APIs without stored access keys. The Pod presents a projected ServiceAccount token to AWS Security Token Service (STS). STS checks the token against the cluster's OIDC provider and returns short-lived credentials for an IAM role.
Three pieces make this work:
- An IAM OIDC provider in your AWS account that points at the cluster's issuer URL.
- An IAM role whose trust policy names the allowed ServiceAccount.
- A ServiceAccount annotated with that role's ARN.
Step 1: Annotate the ServiceAccount
The annotation tells the pod identity webhook which role to inject.
apiVersion: v1
kind: ServiceAccount
metadata:
name: app-reader
namespace: demo
annotations:
eks.amazonaws.com/role-arn: arn:aws:iam::111122223333:role/app-reader-roleWhen a Pod using this ServiceAccount is created, the webhook mutates it. It adds a projected token volume with the audience sts.amazonaws.com. It also sets two environment variables: AWS_ROLE_ARN and AWS_WEB_IDENTITY_TOKEN_FILE. Current AWS SDKs read these variables and call STS automatically.
Step 2: Write the trust policy
The role's trust policy decides who may assume it. This one allows only the demo/app-reader ServiceAccount.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Federated": "arn:aws:iam::111122223333:oidc-provider/oidc.eks.us-east-1.amazonaws.com/id/EXAMPLED539D4633E53DE1B71EXAMPLE"
},
"Action": "sts:AssumeRoleWithWebIdentity",
"Condition": {
"StringEquals": {
"oidc.eks.us-east-1.amazonaws.com/id/EXAMPLED539D4633E53DE1B71EXAMPLE:sub": "system:serviceaccount:demo:app-reader",
"oidc.eks.us-east-1.amazonaws.com/id/EXAMPLED539D4633E53DE1B71EXAMPLE:aud": "sts.amazonaws.com"
}
}
}
]
}The Principal is the OIDC provider, not a user or role. The sub condition pins the namespace and ServiceAccount name. The aud condition requires the audience that the webhook sets.
Step 3: Verify
Create the OIDC provider once per cluster, then attach permissions to the role. Start a Pod with serviceAccountName: app-reader and check the injected variables:
kubectl exec -n demo token-demo -- env | grep AWS_You should see AWS_ROLE_ARN and AWS_WEB_IDENTITY_TOKEN_FILE. If the Pod already existed before you added the annotation, delete it. The webhook only acts when a Pod is created.
Common errors
| Symptom | Likely cause |
|---|---|
| `AccessDenied` from `AssumeRoleWithWebIdentity` | The `sub` value has the wrong namespace or ServiceAccount name |
| `InvalidIdentityToken` | The IAM OIDC provider is missing or its URL does not match the cluster issuer |
| Pod uses the node's role instead | The annotation is missing, or the Pod was created before it was added |
| `aud` mismatch | A custom token volume uses an audience other than `sts.amazonaws.com` |
Exercise
A Pod is running with a ServiceAccount that you annotated with a role ARN an hour ago, but its AWS calls still use the node's role. The trust policy is correct. What do you check and do first?
Show answer
Check whether the Pod was created before the annotation was added, for example by looking for AWSROLEARN in its environment. The webhook only mutates Pods at creation time, so delete the Pod, or restart its Deployment, so that a new Pod is created and gets the injected token volume and variables.