Kubernetes Identity
AWS exposes an Identity Access and Management (IAM) API which can be used to grant permissions to both human and bot users. Using this API, IAM User accounts can be slotted into IAM Groups (e.g., the networkAdmins IAM Group), which can then be allocated baseline permissions using IAM Policies.
AWS workloads (e.g., AWS Lambdas) can also be granted permissions temporarily, without the need for usernames and passwords, using IAM Roles.
In Crosswalk for AWS we showcase how to define IAM:
The full code for this stack is on GitHub.
Overview
We'll review how to:
Create an IAM Role for Admins
Create an admin role in AWS that assumes the AWS account caller, and attach EKS admin policies to the role. This role will be mapped into the system:masters group in Kubernetes RBAC.
import * as aws from "@pulumi/aws";
// Create the EKS cluster admins role.
const adminsName = "admins";
const adminsIamRole = new aws.iam.Role(`${adminsName}-eksClusterAdmin`, {
assumeRolePolicy: aws.getCallerIdentity().then(id =>
aws.iam.assumeRolePolicyForPrincipal({"AWS": `arn:aws:iam::${id.accountId}:root`}))
})
const adminsIamRolePolicy = new aws.iam.RolePolicy(`${adminsName}-eksClusterAdminPolicy`, {
role: adminsIamRole,
policy: {
Version: "2012-10-17",
Statement: [
{ Effect: "Allow", Action: ["eks:*", "ec2:DescribeImages"], Resource: "*", },
{ Effect: "Allow", Action: "iam:PassRole", Resource: "*"},
],
},
},
{ parent: adminsIamRole },
);Authenticate as the admin role.
$ aws sts assume-role --role-arn `pulumi stack output adminsIamRoleArn` --role-session-name k8s-adminCreate an IAM Role for Developers
Create a developer role in AWS that assumes the AWS account caller. This role will be mapped into a limited developer role in Kubernetes RBAC.
import * as aws from "@pulumi/aws";
// Create the EKS cluster developers role.
const devName = "devs";
const devsIamRole = new aws.iam.Role(`${devName}-eksClusterDeveloper`, {
assumeRolePolicy: aws.getCallerIdentity().then(id =>
aws.iam.assumeRolePolicyForPrincipal({"AWS": `arn:aws:iam::${id.accountId}:root`}))
})Authenticate as the devs role.
$ aws sts assume-role --role-arn `pulumi stack output devsIamRoleArn` --role-session-name k8s-devsCreate IAM Roles for EKS Node Groups
Create a node group worker role in AWS that assumes the EC2 Service, and attach required EKS cluster polices to the role. This role will be used by the node group in an instance profile during cluster configuration and creation.
// The managed policies EKS requires of nodegroups join a cluster.
const nodegroupManagedPolicyArns: string[] = [
"arn:aws:iam::aws:policy/AmazonEKSWorkerNodePolicy",
"arn:aws:iam::aws:policy/AmazonEKS_CNI_Policy",
"arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly",
];
// Create the standard node group worker role and attach the required policies.
const stdName = "standardNodeGroup";
const stdNodegroupIamRole = new aws.iam.Role(`${stdName}-eksClusterWorkerNode`, {
assumeRolePolicy: aws.iam.assumeRolePolicyForPrincipal({"Service": "ec2.amazonaws.com"})
})
attachPoliciesToRole(stdName, stdNodegroupIamRole, nodegroupManagedPolicyArns);
// Create the performant node group worker role and attach the required policies.
const perfName = "performanceNodeGroup";
const perfNodegroupIamRole = new aws.iam.Role(`${perfName}-eksClusterWorkerNode`, {
assumeRolePolicy: aws.iam.assumeRolePolicyForPrincipal({"Service": "ec2.amazonaws.com"})
})
attachPoliciesToRole(perfName, perfNodegroupIamRole, nodegroupManagedPolicyArns);
// Attach policies to a role.
function attachPoliciesToRole(name: string, role: aws.iam.Role, policyArns: string[]) {
for (const policyArn of policyArns) {
new aws.iam.RolePolicyAttachment(`${name}-${policyArn.split('/')[1]}`,
{ policyArn: policyArn, role: role },
);
}
}