How to give permission to AWS Code build to access EKS cluster using a role
TL;DR — This is for DevOps who have working knowledge on AWS Codebuild, IAM roles, EKS cluster and AWS CLI
It is very common practice to use roles for making AWS services talk to each other. It’s the best practice rather than sharing credentials with developers. Credentials can be leaked out and get your environment compromised.
Here I am gonna explain how we can give permission to AWS CodeBuild service to access AWS EKS environment securely without storing kubeconfig file anywhere. I have seen many people storing there EKS cluster kubeconfig file in S3 bucket or in their git repo, which is very frightening 😱.
Here is the recipe 🤗
The Workflow consists of 4 steps,
1 — Create IAM Role with trust relationship from CodeBuild Service Role
In my example, the CODEBUILD role is service-role/codebuild-BUILD222-service-role, this is auto-generated when you create a CodeBuild.
2 — Configure aws-auth configmap to authenticate in the cluster using the role created in step1
3 — Create a Policy and attach to CodeBuild Service Role, to perform STS:assumerole and permission to READ in EKS:* to the role created in step 1
4 — Using aws eks update-kubeconfig with the argument — role-arn , you will be able to authenticate in the EKS cluster. Below is a sample buildspec file which you can try ✌️
Now you can manually trigger a build to test it. If everything is configured as I mentioned in above steps, kubectl commands against the EKS cluster will execute successfully and it can be verified in build logs. Hope you find this article useful and helped in building a secure CICD pipeline 👍