aws-samples/amazon-eks-cicd-codebuild

How entrypoint.sh get permission to update kubeconfig?

jialechan opened this issue · 2 comments

update_kubeconfig(){
    if [[ -n ${EKS_ROLE_ARN} ]]; then
        echo "[INFO] got EKS_ROLE_ARN=${EKS_ROLE_ARN}, updating kubeconfig with this role"
        aws eks update-kubeconfig --name $CLUSTER_NAME --kubeconfig $KUBECONFIG --role-arn "${EKS_ROLE_ARN}"
    else
        aws eks update-kubeconfig --name $CLUSTER_NAME --kubeconfig $KUBECONFIG    
    fi
}

Hello pahud, I have a question, this code should run in the code build, how does it get permissions to update kubeconfig?

pahud commented

Hi, good question.

The codebuild project will be running as project.role which is automatically genrated by CDK.

And I grant the required IAM policies for it here so the project.role can describe the cluster and get required info to generate the kubeconfig.

project.addToRolePolicy(new iam.PolicyStatement({
actions: ['eks:DescribeCluster'],
resources: [`${cluster.clusterArn}`],
}))

And ICYMI there's another magic here

cluster.awsAuth.addMastersRole(project.role!)

We just addMasterRole for project.role and this IAM role ARN will be added in to Amazon EKS RBAC as system:master so project.role can literally do anything with kubectl on this EKS cluster.

See AWS IAM Mapping in @aws-eks.

This is a detailed and clear answer,Thx :)