Sub topic 8
Giving Pods AWS Permissions
This chapter assumes you can create an EKS cluster with eksctl, write Kubernetes manifests, and read an IAM policy. You should also know what a service account is in Kubernetes. The goal is to let a pod call AWS APIs, such as reading from S3, without putting access keys in the pod.
The problem: pods need their own identity
A pod that calls AWS needs credentials. There are three places they can come from, and two of them are poor choices.
- Static access keys in a Secret or environment variable. They are long-lived and easy to leak. Rotating them is manual work.
- The node's IAM role. The EC2 instance profile is reachable from every pod on the node through the instance metadata service. All pods on that node then share the same permissions, so one compromised pod gets everything the node can do.
- A role per workload, with temporary credentials. Each service account maps to its own IAM role. AWS STS issues short-lived credentials, and the AWS SDK refreshes them on its own.
EKS gives you two ways to do the third option: IAM roles for service accounts (IRSA) and EKS Pod Identity.
Option 1: IRSA
IRSA uses OpenID Connect (OIDC) federation. Your cluster has an OIDC issuer URL. You register that issuer in IAM as an identity provider. Kubernetes then signs a JWT for each pod's service account, and IAM trusts tokens from that issuer.
How IRSA works
- You create an IAM OIDC provider for the cluster's issuer URL.
- You create an IAM role whose trust policy allows
sts:AssumeRoleWithWebIdentityfor one specific service account. - You annotate the Kubernetes service account with the role ARN.
- When a pod starts, a mutating webhook in the cluster sees the annotation. It adds environment variables and a projected token file to the pod.
- The AWS SDK reads
AWS_ROLE_ARNandAWS_WEB_IDENTITY_TOKEN_FILE. It calls STS with the token and receives temporary credentials.
The eksctl command below does steps 2 and 3 for you. It creates the IAM role, attaches the managed read-only S3 policy, and creates the annotated service account.
eksctl create iamserviceaccount \
--cluster demo \
--namespace default \
--name s3-reader \
--attach-policy-arn arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess \
--approveStep 1 must be done once per cluster. If the OIDC provider is missing, run this first:
eksctl utils associate-iam-oidc-provider --cluster demo --approveThe trust policy
The trust policy is where IRSA is secured. It must restrict the sub claim to a single namespace and service account. Without that condition, any service account in the cluster could assume the role.
{
"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:default:s3-reader",
"oidc.eks.us-east-1.amazonaws.com/id/EXAMPLED539D4633E53DE1B71EXAMPLE:aud": "sts.amazonaws.com"
}
}
}
]
}The role is tied to one cluster, because the principal names that cluster's OIDC provider. If you reuse the same role in another cluster, you must edit its trust policy.
Option 2: EKS Pod Identity
Pod Identity is the newer mechanism. It does not use OIDC. Instead, an agent runs on each node, and the EKS service handles the mapping between service accounts and roles.
How Pod Identity works
- You install the EKS Pod Identity Agent add-on. It runs as a DaemonSet.
- You create an IAM role whose trust policy allows the service principal
pods.eks.amazonaws.com. - You create a pod identity association that links a cluster, namespace, and service account to the role.
- When a pod using that service account starts, EKS injects environment variables that point the SDK at the agent. The agent obtains credentials from the EKS Auth API and returns them to the SDK.
The trust policy for a Pod Identity role does not name a cluster or an OIDC provider:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "pods.eks.amazonaws.com"
},
"Action": [
"sts:AssumeRole",
"sts:TagSession"
]
}
]
}Install the agent add-on, then create the association:
aws eks create-addon \
--cluster-name demo \
--addon-name eks-pod-identity-agentaws eks create-pod-identity-association \
--cluster-name demo \
--namespace default \
--service-account s3-reader \
--role-arn arn:aws:iam::111122223333:role/s3-reader-roleThe service account itself needs no annotation. The association lives in the EKS API, not in the Kubernetes object.
IRSA and Pod Identity compared
| Aspect | IRSA | EKS Pod Identity |
|---|---|---|
| Trust principal | The cluster's OIDC provider | `pods.eks.amazonaws.com` |
| Service account annotation | Required (role ARN) | Not needed |
| Per-cluster setup | OIDC provider in IAM | Agent add-on |
| Reusing a role across clusters | Trust policy must list each OIDC provider | Same role works unchanged |
| Session tags | Not supplied by EKS | Cluster, namespace, and service account tags are added |
| Role limit concerns | Trust policy size grows per cluster | One association per service account |
| Fargate | Supported | Not supported |
| Where the mapping lives | Kubernetes annotation plus IAM trust | EKS API |
Session tags let you write one IAM policy that uses attribute-based access control. For example, a policy can allow access to S3 prefixes that match the namespace tag.
Testing the setup
Run a pod that uses the service account and call AWS from inside it. This manifest uses the amazon/aws-cli image.
apiVersion: v1
kind: Pod
metadata:
name: whoami
spec:
serviceAccountName: s3-reader
restartPolicy: Never
containers:
- name: cli
image: amazon/aws-cli:latest
command: ["aws", "sts", "get-caller-identity"]kubectl apply -f whoami.yaml
kubectl logs whoamiThe output should show an assumed-role ARN that contains your role name. If it shows the node's role, the pod did not receive the injected configuration.
Common errors
AccessDeniedwithAssumeRoleWithWebIdentity(IRSA). Thesubcondition does not match. Check the namespace and service account name for typos, and confirm theaudvalue issts.amazonaws.com.- The pod shows the node role. The pod was created before the annotation or association existed. Delete the pod so the controller recreates it. Also check that the pod uses the correct
serviceAccountName. - Pod Identity credentials are not found. The agent add-on is missing or its DaemonSet pods are not running. Run
kubectl get pods -n kube-systemand look foreks-pod-identity-agent. - The SDK is too old. Both methods need a recent SDK that supports the container credential provider (Pod Identity) or the web identity provider (IRSA). Very old SDK versions fall back to the node role without warning.
- Fargate with Pod Identity. It is not supported there. Use IRSA for pods on Fargate.
- The role allows too much.
AmazonS3ReadOnlyAccesslets the pod read every bucket in the account. For production, write a custom policy scoped to specific buckets.
Choosing between them
Use Pod Identity for new clusters that run on EC2 nodes. It needs less setup, the same role can serve several clusters, and the session tags allow finer policies. Use IRSA when you run pods on Fargate, when the cluster is not on EKS (such as EKS Anywhere or self-managed Kubernetes), or when you have many existing workloads that already use it. The two methods can coexist in one cluster. A pod that has both configured uses the Pod Identity credentials, so migrate one service account at a time and verify the identity with get-caller-identity after each move.
Exercise
A pod using IRSA returns the error "Not authorized to perform sts:AssumeRoleWithWebIdentity". The service account is reports in the namespace analytics, and the trust policy contains the condition "...:sub": "system:serviceaccount:default:reports". What is wrong, and how do you fix it?
Show answer
The sub condition names the default namespace, but the service account lives in analytics. IAM sees a different subject than the one allowed. Change the condition to system:serviceaccount:analytics:reports, then restart the pod so it requests new credentials.
Exercise
You want one IAM role to serve the same service account in three clusters. Which mechanism makes that simplest, and why?
Show answer
EKS Pod Identity. Its trust policy names the service principal pods.eks.amazonaws.com and no cluster-specific OIDC provider, so the role works in every cluster. You only create one pod identity association per cluster. With IRSA you would have to add each cluster's OIDC provider to the trust policy.