|
| 1 | +/* |
| 2 | +1️⃣ Create an IAM Group (eks-admin-group) |
| 3 | +
|
| 4 | +This group will contain users who need admin access to EKS. |
| 5 | +2️⃣ Create an IAM Role (k8s-admin-role) |
| 6 | +
|
| 7 | +Allows users in the eks-admin-group to assume this role for EKS admin tasks. |
| 8 | +Trust policy ensures only eks-admin-group members can assume the role. |
| 9 | +3️⃣ Create an IAM Policy (eks-assume-role-policy) |
| 10 | +
|
| 11 | +Grants sts:AssumeRole permission for the k8s-admin-role. |
| 12 | +Attached to the eks-admin-group, allowing its users to assume the role. |
| 13 | +4️⃣ Register IAM Role with EKS (aws_eks_access_entry) |
| 14 | +
|
| 15 | +Associates k8s-admin-role with the EKS cluster. |
| 16 | +5️⃣ Attach EKS Admin Policy (aws_eks_access_policy_association) |
| 17 | +
|
| 18 | +Grants k8s-admin-role full EKS administrative access |
| 19 | +*/ |
| 20 | +resource "aws_iam_group" "admin_group" { |
| 21 | + name = "eks-admin-group" |
| 22 | +} |
| 23 | +resource "aws_iam_role" "admin_role" { |
| 24 | + name = "eks-admin-role" |
| 25 | + assume_role_policy = jsonencode({ |
| 26 | + Version = "2012-10-17", |
| 27 | + Statement = [ |
| 28 | + { |
| 29 | + Effect = "Allow", |
| 30 | + Principal = { |
| 31 | + "AWS": "arn:aws:iam::${data.aws_caller_identity.current.account_id |
| 32 | +}:root" |
| 33 | + }, |
| 34 | + Action = "sts:AssumeRole" |
| 35 | + } |
| 36 | + ] |
| 37 | + }) |
| 38 | +} |
| 39 | + |
| 40 | +resource "aws_iam_role_policy_attachment" "admin_permissions" { |
| 41 | + role = aws_iam_role.admin_role.name |
| 42 | + policy_arn = "arn:aws:iam::aws:policy/AdministratorAccess" |
| 43 | +} |
| 44 | +# Create IAM Policy for AssumeRole |
| 45 | +resource "aws_iam_policy" "eks_assume_role_policy" { |
| 46 | + name = "eks-assume-role-policy" |
| 47 | + description = "Allows users in the group to assume the eks-admins-role" |
| 48 | + policy = jsonencode({ |
| 49 | + Version = "2012-10-17" |
| 50 | + Statement = [ |
| 51 | + { |
| 52 | + Effect = "Allow" |
| 53 | + Action = "sts:AssumeRole" |
| 54 | + Resource = "arn:aws:iam::936379345511:role/${aws_iam_role.admin_role.name}" |
| 55 | + } |
| 56 | + ] |
| 57 | + }) |
| 58 | +} |
| 59 | + |
| 60 | +# Attach Policy to IAM Group |
| 61 | +resource "aws_iam_group_policy_attachment" "attach_assume_role_policy" { |
| 62 | + group = aws_iam_group.admin_group.name |
| 63 | + policy_arn = aws_iam_policy.eks_assume_role_policy.arn |
| 64 | +} |
| 65 | + |
| 66 | +resource "aws_eks_access_entry" "example" { |
| 67 | + cluster_name = module.eks.cluster_name |
| 68 | + principal_arn = aws_iam_role.admin_role.arn |
| 69 | + type = "STANDARD" |
| 70 | +} |
| 71 | + |
| 72 | +resource "aws_eks_access_policy_association" "example" { |
| 73 | + cluster_name = module.eks.cluster_name |
| 74 | + policy_arn = "arn:aws:eks::aws:cluster-access-policy/AmazonEKSClusterAdminPolicy" |
| 75 | + principal_arn = aws_iam_role.admin_role.arn |
| 76 | + access_scope { |
| 77 | + type = "cluster" |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | + |
| 82 | + |
0 commit comments