Sub topic 8
Giving Pods AWS Permissions
This chapter assumes you have a running EKS cluster, can use kubectl and eksctl, and know what an IAM role and policy are. Pods often need to call AWS APIs, for example to read from S3. This chapter shows how to grant that access without putting long-lived keys in your containers.
The problem with node roles
Every worker node has an instance profile (an IAM role attached to the EC2 instance). Any pod on that node can reach the instance metadata service and use the node role's credentials. If you put S3 permissions on the node role, every pod on every node gets them.
The better approach is to give each Kubernetes service account its own IAM role. Pods that use that service account receive temporary credentials for that role only. EKS offers two ways to do this: IAM roles for service accounts (IRSA) and EKS Pod Identity.
Option 1: IRSA
IRSA uses the cluster's OpenID Connect (OIDC) issuer. The cluster signs a token for the pod. AWS STS (Security Token Service) accepts that token because the IAM role's trust policy names the cluster's OIDC provider and a specific service account.
With eksctl, one command creates the role, the policy attachment, and the annotated service account:
eksctl create iamserviceaccount \
--cluster demo \
--namespace default \
--name s3-reader \
--attach-policy-arn arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess \
--approveThe cluster needs an IAM OIDC provider associated with it. If the command reports that none exists, run eksctl utils associate-iam-oidc-provider --cluster demo --approve first.
Option 2: EKS Pod Identity
Pod Identity removes the OIDC setup. You install the Pod Identity Agent add-on, which runs as a DaemonSet (one pod per node). Then you create an association between a service account and a role.
The role's trust policy must allow the service principal pods.eks.amazonaws.com. Then create the association:
aws eks create-pod-identity-association \
--cluster-name demo \
--namespace default \
--service-account s3-reader \
--role-arn arn:aws:iam::111122223333:role/s3-reader-roleBecause the trust policy does not mention a specific cluster, one role can be reused across clusters.
Choosing between them
| Aspect | IRSA | Pod Identity |
|---|---|---|
| Setup | OIDC provider per cluster | Agent add-on per cluster |
| Trust policy | Names OIDC provider and service account | Names `pods.eks.amazonaws.com` |
| Reuse a role across clusters | Edit trust policy for each cluster | Same role works as is |
| Fargate support | Yes | No |
Use Pod Identity for new EC2-based clusters. Use IRSA on Fargate or where you already rely on it.
Using the service account in a pod
Credentials only reach pods that run under the service account. Set serviceAccountName in the pod template:
apiVersion: v1
kind: Pod
metadata:
name: s3-test
spec:
serviceAccountName: s3-reader
containers:
- name: aws-cli
image: amazon/aws-cli
command: ["aws", "s3", "ls"]
restartPolicy: NeverCommon errors
- Pod still uses the node role. The pod was created before the association or annotation existed. Restart it.
AccessDeniedonAssumeRoleWithWebIdentity. The trust policy names the wrong namespace, service account, or OIDC provider.- Pod Identity does nothing. The Pod Identity Agent add-on is missing or not running on that node.
- Old SDK ignores credentials. Use a recent AWS SDK version, since older ones do not read the injected credential sources.
Exercise
A pod uses the service account s3-reader, but its S3 calls run with the node role's permissions. You created the IAM association after the pod started. What is the likely cause, and what is the fix?
Show answer
Credentials are injected when a pod starts, so the existing pod never received them. Delete or restart the pod (for example with kubectl rollout restart on its Deployment) so the new pod starts with the association in place.