# 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:CreatePodIdentityAssociation` on the cluster, and `iam:PassRole` on the role.
  To remove an association, `eks:DeletePodIdentityAssociation` (the console enables **Remove** only
  with it on every cluster, `eks:*`).
- In the console, also `eks:DescribeCluster` to open the cluster and
  `eks:ListPodIdentityAssociations` on 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

**Console**
1. Open **Kubernetes**, open the cluster, then choose **Pod identity**.
2. Under **Associate a service account with a role**, enter the namespace, the service account and
   the IAM role name.
3. Choose **Associate**.

**CLI**
```sh
dcloud eks pod-identity-associations create my-cluster \
  --namespace shop --service-account api --role shop-api
```

**API**
```http
POST /eks/api/clusters/my-cluster/pod-identity-associations
{ "namespace": "shop", "serviceAccount": "api", "role": "shop-api" }
```

**Terraform**
```hcl
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.

```sh
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_"".
- `403` without `iam:PassRole` on 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.

## Related

- [Cluster access](cluster-access.md)
- `eks.pod-identity-associations.create` · `dcloud eks pod-identity-associations create` · `dcloud_eks_pod_identity_association`
