Let pods act as an IAM role
Associate a Kubernetes service account with an IAM role, so its pods get short-lived credentials for the role and no access key is stored in the cluster.
Replace my-cluster with your cluster's name or id, shop with the namespace, api with the service account and shop-api with the IAM role's name.
Prerequisites
- Permission for
eks:CreatePodIdentityAssociationon the cluster, andiam:PassRoleon the role. To remove an association,eks:DeletePodIdentityAssociation(the console enables Remove only with it on every cluster,eks:*). - In the console, also
eks:DescribeClusterto open the cluster andeks:ListPodIdentityAssociationson it: without it the Pod identity tab shows neither the associations nor the form. - A cluster created after workload identity was introduced. On an older one the page says "This
cluster was created before workload identity." and the create answers
409.
Associate the service account
- Open Kubernetes, open the cluster, then choose Pod identity.
- Under Associate a service account with a role, enter the namespace, the service account and the IAM role name.
- Choose Associate.
dcloud eks pod-identity-associations create my-cluster \
--namespace shop --service-account api --role shop-apiPOST /eks/api/clusters/my-cluster/pod-identity-associations
{ "namespace": "shop", "serviceAccount": "api", "role": "shop-api" }resource "dcloud_eks_pod_identity_association" "shop_api" {
cluster_id = dcloud_eks_cluster.main.id
namespace = "shop"
service_account = "api"
role = "shop-api"
}Result
Pods of that service account created from then on get a projected token for Deutsche Cloud STS and
the variables AWS SDKs read: AWS_ROLE_ARN, AWS_WEB_IDENTITY_TOKEN_FILE, AWS_ENDPOINT_URL_STS,
AWS_ENDPOINT_URL_S3 and AWS_REGION. An AWS SDK or the AWS CLI then reaches Deutsche Cloud S3 as the
role with no code change. Pods already running keep what they had until they restart.
kubectl -n shop run s3 --rm -it --image=public.ecr.aws/aws-cli/aws-cli \
--overrides='{"spec":{"serviceAccountName":"api"}}' -- s3 ls s3://my-bucket/Remove an association
On the Pod identity tab, choose Remove on the row, then Delete in the dialog
Remove shop/api, or run
dcloud eks pod-identity-associations delete my-cluster association-id. Its pods
get no new credentials; those already issued run out within the hour.
Refusals
404"No IAM role named "shop-api"".403withoutiam:PassRoleon the role.409"shop/api already has a pod identity association on this cluster", "This cluster was created before workload identity; its service-account issuer is not published. Create a new cluster to use pod identity.", or the quota is full.