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/AmazonS3ReadOnlyAccessThis 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-clusterThis 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.yaml5. Verification:
Exec into the pod:
kubectl exec -it my-pod -n my-namespace -- shTry listing the contents of your S3 bucket:
aws s3 ls s3://my-bucketYou 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.
Problem with Dgraph helm chart serviceAccount creation - Dgraph ... | What I want to do I want a K8s service account to be created by proper setting in Dgraph’s helm chart values.yaml file. What I did I used helm chart to install Dgraph in EKS cluster. The serviceAccount config in values.yaml Dgraph Helm Chart Values doesn’t create the K8s service account when it’s been installed though the attribute create is set as true. Both version, v21.12.0 and v23.0.1 failed. Here is my settings. serviceAccount: create: true annotations: eks.amazonaws.com/role-a...
On Amazon EKS and cluster add-ons | by Dirk Michel | Medium | Why you should consider running cluster add-ons on AWS Fargate serverless compute.
Introducing fine-grained IAM roles for service accounts | AWS Open ... | Here at AWS we focus first and foremost on customer needs. In the context of access control in Amazon EKS, you asked in issue #23 of our public container roadmap for fine-grained IAM roles in EKS. To address this need, the community came up with a number of open source solutions, such as kube2iam, kiam, […]
Unlock High-Speed Access to Amazon S3 Objects from Kubernetes ... | During re:Invent 2023, AWS has made Mountpoint for Amazon S3 Container Storage Interface (CSI) driver generally available. Amazon S3 CSI…
Amazon Web Services Connection — apache-airflow-providers ... | This command will use an existing EKS Cluster ID and create an IAM role, service account and namespace. eksctl create iamserviceaccount --cluster="< ...
Deployment of 2048 Game via EKS Cluster Using Ingress | by ... | We will configure and deploy our sample application i.e 2048 Game on AWS EKS Cluster using Helm Chart