How to Add IAM User and IAM Role to AWS EKS Cluster?¶
- You can find the source code for this video in my GitHub Repo.
- If you want to create EKS cluster using terraform, you can follow this tutorial.
Add IAM User to EKS Cluster¶
- Create
ClusterRole
with read-only access to the Kubernetes cluster and bind it to thereader
group viaClusterRoleBinding
. Name the fileread-group.yaml
.
-
Apply RBAC policies with kubectl.
-
Create IAM policy to let users view nodes and workloads for all clusters in the AWS Management Console. Give it a name
AmazonEKSViewNodesAndWorkloadsPolicy
.
AmazonEKSViewNodesAndWorkloadsPolicy | |
---|---|
-
We can attach the IAM policy directly to the IAM user or follow the best practice and create an IAM group first. Let's call it
developers
and attachAmazonEKSViewNodesAndWorkloadsPolicy
IAM policy. -
Then, we need an IAM user. Let's create one and call it
developer
. We're going to place it indevelopers
IAM grop with read only access. Don't forget to download credentials, we will use them to configure aws cli. -
Now, we need to create local aws profile using
developer
's user credentials. To do that simplly add--profile
flag toaws configure
command. -
To map IAM user with Kubernetes RBAC system, we need to modify
aws-auth
configmap. Open the config map and add arn of the IAM user undermapUsers
key. -
Now, we need to switch to the
developer
user. We need to update Kubernetes context using thedeveloper
profile. Don't forget to update region and the cluster name. -
You can verify the Kubernetes context that you use
developer
profile to interact with the EKS cluster. -
By now, we have created RBAC policy with read only access and maped it to the IAM
developer
user. Let's see what we can do in our cluster now. You can runkubectl auth can-i <object>
to verify access. First let's check if we can get pods in Kubernetes cluster. The response should beyes
. -
Then, let's check if we can create pods in the Kuberentes. The response should be
no
. - You can also try to create the pod. You should get
Forbidden
error.
Add IAM Role to EKS Cluster¶
- First of all, let's create IAM policy with admin access to EKS clusters. Give it a name
AmazonEKSAdminPolicy
. To view information on the Nodes and Workloads in the AWS Management Console, you need additional IAM permissions, as well as Kubernetes permissions.
AmazonEKSAdminPolicy | |
---|---|
-
Then, create
eks-admin
IAM role and attachAmazonEKSAdminPolicy
policy that you just created. SelectAnother AWS account
and enter your account id. -
Optionally, you can describe
eks-admin
role to check who can use it. Potentially, any IAM user can assume this role if they have an appropriate policy in place. -
For any user that wants to use
eks-admin
IAM role, we need to create an additionalAmazonEKSAssumeEKSAdminPolicy
policy, that allows to assume the role.
AmazonEKSAssumeEKSAdminPolicy | |
---|---|
-
To test it, we need another IAM user. Let's create
manager
user and allow it to useeks-admin
IAM role. In this case, just attachAmazonEKSAssumeEKSAdminPolicy
directly to the user. -
The same thing for this user, we need to create a
manager
aws profile. -
You can check if the
manager
user can assume theeks-admin
role by running the following command. If we get credentials back, it means we can use it. -
Now, we need to switch back to the user that created the EKS cluster. When you omit the profile, aws will use the
default
one. -
It's a very similar process to add an IAM role. You also need to update the
aws-auth
configmap. In this case, we will use Kubernetes RBAC groupsystem:masters
that ships with the cluster. -
Finally, to test the IAM role, we need to create an
eks-admin
profile config to assume the role by themanager
user. You need to add it to~/.aws/config
. -
Update Kubernetes context to automatically assume
eks-admin
role bymanager
user. -
You can check the context with the following command.
-
Check if
manager
user has admin access to the EKS cluster. The response should beyes.
-
You can also try to create a pod using the previous command.
Clean
-
Delete IAM policies
AmazonEKSViewNodesAndWorkloadsPolicy
AmazonEKSAssumeEKSAdminPolicy
AmazonEKSAdminPolicy
-
Delete IAM roles
eks-admin
-
Delete IAM groups
eks-admin
developers
-
Delete IAM users
manager
developer
-
Clean UP
~/.aws/config
and~/.aws/credentials