Learn how the Terraform command 'eksctl create iamserviceaccount' simplifies IAM service account creation and management in your Amazon EKS clusters.
Managing AWS permissions for your Kubernetes workloads running on Amazon EKS can be streamlined with the eksctl create iamserviceaccount
command. This command simplifies the process of granting your EKS service accounts the necessary access to AWS resources. Here's how it works:
eksctl create iamserviceaccount
simplifies the process of granting AWS permissions to Kubernetes service accounts within your EKS cluster. Here's a breakdown:
IAM Role Creation: It generates an IAM role with permissions you specify. This role dictates what AWS resources the service account can interact with.
eksctl create iamserviceaccount \
--name my-service-account \
--namespace my-namespace \
--cluster my-cluster \
--attach-policy-arn arn:aws:iam::...:policy/AmazonS3FullAccess
Trust Relationship: It configures the IAM role to trust the EKS cluster's identity. This allows Kubernetes to assume the role on behalf of the service account.
Service Account Creation: It creates a service account within your specified Kubernetes namespace.
Annotation: It attaches the IAM role's ARN to the service account as an annotation. This annotation is crucial:
eks.amazonaws.com/role-arn: arn:aws:iam::...:role/my-iam-role
(Optional) AWS Addon: If you're using the aws-iam-authenticator
, this annotation is enough. For the newer IRSA (IAM Roles for Service Accounts) system, an additional step is needed:
eksctl utils associate-iam-oidc-provider --cluster my-cluster
This command sets up the necessary OpenID Connect (OIDC) provider for IRSA.
Now, pods running with this service account can seamlessly leverage the associated IAM role's permissions when accessing AWS services.
This code sample shows how to give a Kubernetes service account read-only access to an S3 bucket using eksctl. It creates an IAM role with the necessary policy, a corresponding service account in your cluster, and then deploys a pod that uses this service account to access the S3 bucket. The example includes commands for creating the resources, associating the OIDC provider, deploying a test pod, and verifying the access. Remember to replace the placeholder values with your actual cluster, namespace, and bucket names.
This example demonstrates how to use eksctl create iamserviceaccount
to grant a Kubernetes service account read-only access to an S3 bucket.
1. Prerequisites:
my-cluster
.eksctl
and kubectl
installed and configured.my-bucket
.2. Create the IAM Service Account:
eksctl create iamserviceaccount \
--name s3-reader \
--namespace my-namespace \
--cluster my-cluster \
--attach-policy-arn arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess
This command:
eksctl-my-cluster-my-namespace-s3-reader-XXXX
(XXXX is a random suffix).AmazonS3ReadOnlyAccess
policy to the role.s3-reader
in the my-namespace
namespace.3. Associate OIDC Provider (for IRSA):
eksctl utils associate-iam-oidc-provider --cluster my-cluster
This step is required if your cluster uses IRSA. It configures the OIDC provider for your cluster.
4. Deploy a Pod using the Service Account:
Create a file named pod.yaml
:
apiVersion: v1
kind: Pod
metadata:
name: my-pod
namespace: my-namespace
spec:
serviceAccountName: s3-reader
containers:
- name: my-container
image: busybox
command:
- sleep
- "1000"
This pod:
s3-reader
service account.sleep
command (replace with your application logic).Deploy the pod:
kubectl apply -f pod.yaml
5. Verification:
Exec into the pod:
kubectl exec -it my-pod -n my-namespace -- sh
Try listing the contents of your S3 bucket:
aws s3 ls s3://my-bucket
You should see the contents of your bucket listed, confirming that the service account has read-only access.
Note: Replace placeholders like my-cluster
, my-namespace
, my-bucket
with your actual values.
eksctl
: While eksctl
simplifies the process, you can achieve the same results using the AWS CLI and kubectl
directly. This provides more granular control but requires a deeper understanding of the underlying mechanisms.kiam
can dynamically generate IAM policies based on pod labels, providing more flexibility.aws-iam-authenticator
vs. IRSA: While both methods allow pods to assume IAM roles, IRSA is the newer and recommended approach due to its improved security and scalability.eksctl delete iamserviceaccount
for this purpose.This command simplifies granting AWS permissions to Kubernetes service accounts in EKS clusters.
Feature | Description |
---|---|
IAM Role Creation | Generates an IAM role with specified permissions for the service account. |
Trust Relationship | Configures the IAM role to trust the EKS cluster, allowing role assumption. |
Service Account Creation | Creates a service account in the specified Kubernetes namespace. |
Annotation | Attaches the IAM role's ARN to the service account as an annotation. |
AWS Addon (Optional) | For IRSA, use eksctl utils associate-iam-oidc-provider to set up the OIDC provider. |
Benefits:
Example:
eksctl create iamserviceaccount \
--name my-service-account \
--namespace my-namespace \
--cluster my-cluster \
--attach-policy-arn arn:aws:iam::...:policy/AmazonS3FullAccess
Note: The eks.amazonaws.com/role-arn
annotation is crucial for both aws-iam-authenticator
and IRSA.
In conclusion, eksctl create iamserviceaccount
is a valuable tool for simplifying AWS permissions management in EKS environments. By automating the creation of IAM roles, trust relationships, service accounts, and annotations, it streamlines the process of granting pods the necessary access to AWS resources. Understanding its capabilities and following security best practices empowers developers to securely integrate their Kubernetes workloads with AWS services.