Create and Manage IAM Roles for Service Accounts¶
Let's see how we can create and manage IAM Roles for Service Accounts (IRSA).
Step 1: Create a Service Account¶
Let's create a ordinary service account in the default namespace.
Apply the manifest to create the service account:
Verify the service account:
Step 2: Create Pod With Service Account¶
Let's create pods that uses the ordinary service account we created. We'll use deployment to create pods:
Apply the manifest to create deployment:
Verify Deployment and Pods:
Step 3: Access AWS Resources From Within Pod¶
Let's try to list S3 buckets from within a pod in the deployment:
# Start a shell session inside the container
kubectl exec -it <pod-name> -- bash
# Verify if the aws-cli is installed
aws --version
# List S3 buckets
aws s3 ls
You'll receive the following error:
This is because the pod doesn't have permission to access S3 buckets.
Step 4: Create IAM Role for Service Account¶
We will use eksctl
to create an IAM Role for the service account.
You have the flexibility to either provide all the parameters directly in the command line or use the --config-file
option to supply the parameters in the eksctl
command.
-
Create IRSA without config file
-
Create IRSA using config file
First we need to create the config file as follows:
Now, we can create the service account using eksctl as follows:
This will do the following for you:
- Create an IAM Role in AWS
- Create a service account in your Kubernetes cluster
- Annotate the service account with the IAM Role ARN
Visit the AWS console and verify the IAM Role. Pay close attention to the trust policy associated with the IAM Role.
The trust policy enables the IAM OIDC provider to assume a role with web identity, but only for the specified service account.
Also, verify the service account created:
# List service accounts
kubectl get sa
# Describe the service account
kubectl describe sa <service-account-name>
You'll observe the service account we created has eks.amazonaws.com/role-arn
annotation.
Step 5: Update the Service Account Name in Deployment¶
Let's update the deployment to use the newly created service account that is associated with an IAM role granting S3 permissions.
Apply the manifest to update the deployment:
Verify deployment and pods:
Step 6: Retry Accessing AWS Resources from Within a Pod¶
# Start a shell session inside the container
kubectl exec -it <pod-name> -- bash
# Verify if the aws-cli is installed
aws --version
# List S3 buckets
aws s3 ls
This time, you will notice that you are able to list the S3 buckets because the service account associated with the pod is connected to an IAM Role that provides the necessary access to S3 buckets.
Clean Up¶
Assuming your folder structure looks like the one below:
Let's delete all the resources we created:
Also, delete IAM Role for Service Account (IRSA) that we created:
# Without config file
eksctl delete iamserviceaccount --name service-account-for-s3-access --cluster my-cluster
{OR}
# Using config file
eksctl delete iamserviceaccount --config-file=irsa.yml --approve
This will delete both the IAM Role and the service account that were created.