Skip to content

Commit 4177913

Browse files
authored
fix: Correct cluster access entry to create multiple policy associations per access entry (#2892)
1 parent a68aac6 commit 4177913

File tree

4 files changed

+104
-10
lines changed

4 files changed

+104
-10
lines changed

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ Terraform module which creates AWS EKS (Kubernetes) resources
1414
- [Upgrade to v17.x](https://github.com/terraform-aws-modules/terraform-aws-eks/blob/master/docs/UPGRADE-17.0.md)
1515
- [Upgrade to v18.x](https://github.com/terraform-aws-modules/terraform-aws-eks/blob/master/docs/UPGRADE-18.0.md)
1616
- [Upgrade to v19.x](https://github.com/terraform-aws-modules/terraform-aws-eks/blob/master/docs/UPGRADE-19.0.md)
17+
- [Upgrade to v20.x](https://github.com/terraform-aws-modules/terraform-aws-eks/blob/master/docs/UPGRADE-19.0.md)
1718

1819
### External Documentation
1920

2021
Please note that we strive to provide a comprehensive suite of documentation for __*configuring and utilizing the module(s)*__ defined here, and that documentation regarding EKS (including EKS managed node group, self managed node group, and Fargate profile) and/or Kubernetes features, usage, etc. are better left up to their respective sources:
22+
2123
- [AWS EKS Documentation](https://docs.aws.amazon.com/eks/latest/userguide/getting-started.html)
2224
- [Kubernetes Documentation](https://kubernetes.io/docs/home/)
2325

@@ -72,13 +74,45 @@ module "eks" {
7274
}
7375
}
7476
77+
# Cluster access entry
78+
# To add the current caller identity as an administrator
79+
enable_cluster_creator_admin_permissions = true
80+
81+
access_entries = {
82+
# One access entry with a policy associated
83+
example = {
84+
kubernetes_groups = []
85+
principal_arn = "arn:aws:iam::123456789012:role/something"
86+
87+
policy_associations = {
88+
example = {
89+
policy_arn = "arn:aws:eks::aws:cluster-access-policy/AmazonEKSViewPolicy"
90+
access_scope = {
91+
namespaces = ["default"]
92+
type = "namespace"
93+
}
94+
}
95+
}
96+
}
97+
}
98+
7599
tags = {
76100
Environment = "dev"
77101
Terraform = "true"
78102
}
79103
}
80104
```
81105

106+
### Cluster Access Entry
107+
108+
When enabling `authentication_mode = "API_AND_CONFIG_MAP"`, EKS will automatically create an access entry for the IAM role(s) used by managed nodegroup(s) and Fargate profile(s). There are no additional actions required by users. For self-managed nodegroups and the Karpenter sub-module, this project automatically adds the access entry on behalf of users so there are no additional actions required by users.
109+
110+
On clusters that were created prior to CAM support, there will be an existing access entry for the cluster creator. This was previously not visible when using `aws-auth` ConfigMap, but will become visible when access entry is enabled.
111+
112+
### Bootstrap Cluster Creator Admin Permissions
113+
114+
Setting the `bootstrap_cluster_creator_admin_permissions` is a one time operation when the cluster is created; it cannot be modified later through the EKS API. In this project we are hardcoding this to `false`. If users wish to achieve the same functionality, we will do that through an access entry which can be enabled or disabled at any time of their choosing using the variable `enable_cluster_creator_admin_permissions`
115+
82116
## Examples
83117

84118
- [EKS Managed Node Group](https://github.com/terraform-aws-modules/terraform-aws-eks/tree/master/examples/eks_managed_node_group): EKS Cluster using EKS managed node groups

examples/eks_managed_node_group/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ Note that this example may create resources which cost money. Run `terraform des
5555
| Name | Type |
5656
|------|------|
5757
| [aws_iam_policy.node_additional](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
58+
| [aws_iam_role.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
5859
| [aws_security_group.remote_access](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group) | resource |
5960
| [aws_ami.eks_default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ami) | data source |
6061
| [aws_ami.eks_default_arm](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ami) | data source |

examples/eks_managed_node_group/main.tf

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ module "eks" {
3535
cluster_ip_family = "ipv6"
3636
create_cni_ipv6_iam_policy = true
3737

38+
enable_cluster_creator_admin_permissions = true
39+
3840
cluster_addons = {
3941
coredns = {
4042
most_recent = true
@@ -241,6 +243,46 @@ module "eks" {
241243
}
242244
}
243245

246+
access_entries = {
247+
# One access entry with a policy associated
248+
ex-single = {
249+
kubernetes_groups = []
250+
principal_arn = aws_iam_role.this["single"].arn
251+
252+
policy_associations = {
253+
single = {
254+
policy_arn = "arn:aws:eks::aws:cluster-access-policy/AmazonEKSViewPolicy"
255+
access_scope = {
256+
namespaces = ["default"]
257+
type = "namespace"
258+
}
259+
}
260+
}
261+
}
262+
263+
# Example of adding multiple policies to a single access entry
264+
ex-multiple = {
265+
kubernetes_groups = []
266+
principal_arn = aws_iam_role.this["multiple"].arn
267+
268+
policy_associations = {
269+
ex-one = {
270+
policy_arn = "arn:aws:eks::aws:cluster-access-policy/AmazonEKSEditPolicy"
271+
access_scope = {
272+
namespaces = ["default"]
273+
type = "namespace"
274+
}
275+
}
276+
ex-two = {
277+
policy_arn = "arn:aws:eks::aws:cluster-access-policy/AmazonEKSViewPolicy"
278+
access_scope = {
279+
type = "cluster"
280+
}
281+
}
282+
}
283+
}
284+
}
285+
244286
tags = local.tags
245287
}
246288

@@ -436,3 +478,26 @@ data "aws_ami" "eks_default_bottlerocket" {
436478
values = ["bottlerocket-aws-k8s-${local.cluster_version}-x86_64-*"]
437479
}
438480
}
481+
482+
resource "aws_iam_role" "this" {
483+
for_each = toset(["single", "multiple"])
484+
485+
name = "ex-${each.key}"
486+
487+
# Just using for this example
488+
assume_role_policy = jsonencode({
489+
Version = "2012-10-17"
490+
Statement = [
491+
{
492+
Action = "sts:AssumeRole"
493+
Effect = "Allow"
494+
Sid = "Example"
495+
Principal = {
496+
Service = "ec2.amazonaws.com"
497+
}
498+
},
499+
]
500+
})
501+
502+
tags = local.tags
503+
}

main.tf

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -168,28 +168,22 @@ locals {
168168
for pol_key, pol_val in lookup(entry_val, "policy_associations", {}) :
169169
merge(
170170
{
171-
principal_arn = entry_val.principal_arn
172-
kubernetes_groups = lookup(entry_val, "kubernetes_groups", [])
173-
tags = lookup(entry_val, "tags", {})
174-
type = lookup(entry_val, "type", "STANDARD")
175-
user_name = lookup(entry_val, "user_name", null)
171+
principal_arn = entry_val.principal_arn
172+
entry_key = entry_key
173+
pol_key = pol_key
176174
},
177175
{ for k, v in {
178176
association_policy_arn = pol_val.policy_arn
179177
association_access_scope_type = pol_val.access_scope.type
180178
association_access_scope_namespaces = lookup(pol_val.access_scope, "namespaces", [])
181179
} : k => v if !contains(["EC2_LINUX", "EC2_WINDOWS", "FARGATE_LINUX"], lookup(entry_val, "type", "STANDARD")) },
182-
{
183-
entry_key = entry_key
184-
pol_key = pol_key
185-
}
186180
)
187181
]
188182
])
189183
}
190184

191185
resource "aws_eks_access_entry" "this" {
192-
for_each = { for k, v in local.flattened_access_entries : "${v.entry_key}_${v.pol_key}" => v if local.create }
186+
for_each = { for k, v in local.merged_access_entries : k => v if local.create }
193187

194188
cluster_name = aws_eks_cluster.this[0].name
195189
kubernetes_groups = try(each.value.kubernetes_groups, [])

0 commit comments

Comments
 (0)