Skip to content

Commit 990c904

Browse files
committed
feat: Add policy for custom tags on EKS Auto Mode, validate examples
1 parent 8c2b5e9 commit 990c904

File tree

6 files changed

+201
-50
lines changed

6 files changed

+201
-50
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,12 +370,14 @@ We are grateful to the community for contributing bugfixes and improvements! Ple
370370
| [aws_iam_openid_connect_provider.oidc_provider](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_openid_connect_provider) | resource |
371371
| [aws_iam_policy.cluster_encryption](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
372372
| [aws_iam_policy.cni_ipv6_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
373+
| [aws_iam_policy.eks_auto_custom](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
373374
| [aws_iam_role.eks_auto](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
374375
| [aws_iam_role.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
375376
| [aws_iam_role_policy_attachment.additional](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
376377
| [aws_iam_role_policy_attachment.cluster_encryption](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
377378
| [aws_iam_role_policy_attachment.eks_auto](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
378379
| [aws_iam_role_policy_attachment.eks_auto_additional](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
380+
| [aws_iam_role_policy_attachment.eks_auto_custom](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
379381
| [aws_iam_role_policy_attachment.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
380382
| [aws_security_group.cluster](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group) | resource |
381383
| [aws_security_group.node](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group) | resource |
@@ -386,6 +388,7 @@ We are grateful to the community for contributing bugfixes and improvements! Ple
386388
| [aws_eks_addon_version.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/eks_addon_version) | data source |
387389
| [aws_iam_policy_document.assume_role_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
388390
| [aws_iam_policy_document.cni_ipv6_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
391+
| [aws_iam_policy_document.eks_auto_custom](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
389392
| [aws_iam_policy_document.node_assume_role_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
390393
| [aws_iam_session_context.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_session_context) | data source |
391394
| [aws_partition.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/partition) | data source |
@@ -452,6 +455,7 @@ We are grateful to the community for contributing bugfixes and improvements! Ple
452455
| <a name="input_enable_efa_support"></a> [enable\_efa\_support](#input\_enable\_efa\_support) | Determines whether to enable Elastic Fabric Adapter (EFA) support | `bool` | `false` | no |
453456
| <a name="input_enable_irsa"></a> [enable\_irsa](#input\_enable\_irsa) | Determines whether to create an OpenID Connect Provider for EKS to enable IRSA | `bool` | `true` | no |
454457
| <a name="input_enable_kms_key_rotation"></a> [enable\_kms\_key\_rotation](#input\_enable\_kms\_key\_rotation) | Specifies whether key rotation is enabled | `bool` | `true` | no |
458+
| <a name="input_enable_node_custom_tags_permissions"></a> [enable\_node\_custom\_tags\_permissions](#input\_enable\_node\_custom\_tags\_permissions) | Determines whether to enable permissions for custom tags for the EKS Auto node IAM role | `bool` | `true` | no |
455459
| <a name="input_enable_security_groups_for_pods"></a> [enable\_security\_groups\_for\_pods](#input\_enable\_security\_groups\_for\_pods) | Determines whether to add the necessary IAM permission policy for security groups for pods | `bool` | `true` | no |
456460
| <a name="input_fargate_profile_defaults"></a> [fargate\_profile\_defaults](#input\_fargate\_profile\_defaults) | Map of Fargate Profile default configurations | `any` | `{}` | no |
457461
| <a name="input_fargate_profiles"></a> [fargate\_profiles](#input\_fargate\_profiles) | Map of Fargate Profile definitions to create | `any` | `{}` | no |
@@ -479,6 +483,7 @@ We are grateful to the community for contributing bugfixes and improvements! Ple
479483
| <a name="input_node_iam_role_name"></a> [node\_iam\_role\_name](#input\_node\_iam\_role\_name) | Name to use on the EKS Auto node IAM role created | `string` | `null` | no |
480484
| <a name="input_node_iam_role_path"></a> [node\_iam\_role\_path](#input\_node\_iam\_role\_path) | The EKS Auto node IAM role path | `string` | `null` | no |
481485
| <a name="input_node_iam_role_permissions_boundary"></a> [node\_iam\_role\_permissions\_boundary](#input\_node\_iam\_role\_permissions\_boundary) | ARN of the policy that is used to set the permissions boundary for the EKS Auto node IAM role | `string` | `null` | no |
486+
| <a name="input_node_iam_role_policy_statements"></a> [node\_iam\_role\_policy\_statements](#input\_node\_iam\_role\_policy\_statements) | A list of IAM policy [statements](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document#statement) - used for adding specific IAM permissions as needed | `any` | `[]` | no |
482487
| <a name="input_node_iam_role_tags"></a> [node\_iam\_role\_tags](#input\_node\_iam\_role\_tags) | A map of additional tags to add to the EKS Auto node IAM role created | `map(string)` | `{}` | no |
483488
| <a name="input_node_iam_role_use_name_prefix"></a> [node\_iam\_role\_use\_name\_prefix](#input\_node\_iam\_role\_use\_name\_prefix) | Determines whether the EKS Auto node IAM role name (`node_iam_role_name`) is used as a prefix | `bool` | `true` | no |
484489
| <a name="input_node_security_group_additional_rules"></a> [node\_security\_group\_additional\_rules](#input\_node\_security\_group\_additional\_rules) | List of additional security group rules to add to the node security group created. Set `source_cluster_security_group = true` inside rules to set the `cluster_security_group` as source | `any` | `{}` | no |

examples/eks-hybrid-nodes/ami/amazon-eks-ubuntu.pkr.hcl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,8 @@ build {
296296

297297
"snap install aws-cli --classic",
298298
"snap switch --channel=candidate amazon-ssm-agent",
299-
"curl -OL https://hybrid-assets.eks.amazonaws.com/releases/latest/bin/linux/amd64/nodeadm /usr/bin/",
299+
"curl -OL 'https://hybrid-assets.eks.amazonaws.com/releases/latest/bin/linux/amd64/nodeadm'",
300+
"mv nodeadm /usr/bin/nodeadm",
300301
"chmod +x /usr/bin/nodeadm",
301302
"nodeadm install ${var.eks_version} --credential-provider ${var.credential_provider}",
302303
]

examples/eks-hybrid-nodes/main.tf

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -42,34 +42,27 @@ module "eks" {
4242
cluster_endpoint_public_access = true
4343
enable_cluster_creator_admin_permissions = true
4444

45-
cluster_security_group_additional_rules = {
46-
hybrid-all = {
47-
cidr_blocks = [local.remote_network_cidr]
48-
description = "Allow all HTTPS traffic from remote node/pod network"
49-
from_port = 443
50-
to_port = 443
51-
protocol = "tcp"
52-
type = "ingress"
53-
}
45+
cluster_addons = {
46+
coredns = {}
47+
eks-pod-identity-agent = {}
48+
kube-proxy = {}
5449
}
5550

56-
node_security_group_additional_rules = {
51+
create_node_security_group = false
52+
cluster_security_group_additional_rules = {
5753
hybrid-all = {
5854
cidr_blocks = [local.remote_network_cidr]
5955
description = "Allow all traffic from remote node/pod network"
60-
from_port = "-1"
61-
to_port = "-1"
56+
from_port = 0
57+
to_port = 0
6258
protocol = "all"
6359
type = "ingress"
6460
}
6561
}
6662

67-
# EKS Addons
68-
cluster_addons = {
69-
coredns = {}
70-
eks-pod-identity-agent = {}
71-
kube-proxy = {}
72-
vpc-cni = {}
63+
cluster_compute_config = {
64+
enabled = true
65+
node_pools = ["system"]
7366
}
7467

7568
access_entries = {
@@ -91,16 +84,6 @@ module "eks" {
9184
}
9285
}
9386

94-
eks_managed_node_groups = {
95-
default = {
96-
instance_types = ["m6i.large"]
97-
98-
min_size = 2
99-
max_size = 5
100-
desired_size = 2
101-
}
102-
}
103-
10487
tags = local.tags
10588
}
10689

examples/eks-hybrid-nodes/remote.tf

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,13 @@ resource "local_file" "join" {
7171
EOF
7272
7373
# Use SCP/SSH to execute commands on the remote host
74-
scp -i ${local_file.key_pem.filename} nodeConfig.yaml ubuntu@${aws_instance.hybrid_node.public_ip}:/home/ubuntu/nodeConfig.yaml
75-
ssh -n -i ${local_file.key_pem.filename} ubuntu@${aws_instance.hybrid_node.public_ip} sudo nodeadm init -c file://nodeConfig.yaml
76-
ssh -n -i ${local_file.key_pem.filename} ubuntu@${aws_instance.hybrid_node.public_ip} sudo systemctl daemon-reload
74+
scp -i ${local_file.key_pem.filename} nodeConfig.yaml ubuntu@${aws_instance.hybrid_node["one"].public_ip}:/home/ubuntu/nodeConfig.yaml
75+
ssh -n -i ${local_file.key_pem.filename} ubuntu@${aws_instance.hybrid_node["one"].public_ip} sudo nodeadm init -c file://nodeConfig.yaml
76+
ssh -n -i ${local_file.key_pem.filename} ubuntu@${aws_instance.hybrid_node["one"].public_ip} sudo systemctl daemon-reload
77+
78+
scp -i ${local_file.key_pem.filename} nodeConfig.yaml ubuntu@${aws_instance.hybrid_node["two"].public_ip}:/home/ubuntu/nodeConfig.yaml
79+
ssh -n -i ${local_file.key_pem.filename} ubuntu@${aws_instance.hybrid_node["two"].public_ip} sudo nodeadm init -c file://nodeConfig.yaml
80+
ssh -n -i ${local_file.key_pem.filename} ubuntu@${aws_instance.hybrid_node["two"].public_ip} sudo systemctl daemon-reload
7781
7882
# Clean up
7983
rm nodeConfig.yaml
@@ -85,14 +89,16 @@ data "aws_ami" "hybrid_node" {
8589
provider = aws.remote
8690

8791
most_recent = true
88-
name_regex = "amazon-eks-ubuntu-${local.cluster_version}-amd64-*"
92+
name_regex = "eks-hybrid-ubuntu-${local.cluster_version}-amd64-*"
8993
owners = ["self"]
9094
}
9195

9296
# Demonstration only - AWS EC2 instances are not supported for EKS Hybrid nodes
9397
resource "aws_instance" "hybrid_node" {
9498
provider = aws.remote
9599

100+
for_each = { one = 0, two = 1 }
101+
96102
ami = data.aws_ami.hybrid_node.id
97103
associate_public_ip_address = true
98104
instance_type = "m5.large"
@@ -103,11 +109,11 @@ resource "aws_instance" "hybrid_node" {
103109
}
104110

105111
vpc_security_group_ids = [aws_security_group.remote_node.id]
106-
subnet_id = element(module.remote_node_vpc.public_subnets, 0)
112+
subnet_id = element(module.remote_node_vpc.public_subnets, each.value)
107113

108114
tags = merge(
109115
local.tags,
110-
{ Name = "hybrid-node" }
116+
{ Name = "hybrid-node-${each.key}" }
111117
)
112118
}
113119

@@ -141,12 +147,10 @@ resource "aws_vpc_security_group_ingress_rule" "remote_node" {
141147
cluster-all = {
142148
description = "Allow all traffic from cluster network"
143149
cidr_ipv4 = module.vpc.vpc_cidr_block
144-
from_port = "-1"
145150
ip_protocol = "all"
146151
}
147152
remote-all = {
148153
description = "Allow all traffic from within the remote network itself"
149-
from_port = "-1"
150154
ip_protocol = "all"
151155
referenced_security_group_id = aws_security_group.remote_node.id
152156
}
@@ -179,10 +183,7 @@ resource "aws_vpc_security_group_egress_rule" "remote_node" {
179183
all = {
180184
description = "Allow all egress"
181185
cidr_ipv4 = "0.0.0.0/0"
182-
description = "All"
183-
from_port = "-1"
184186
ip_protocol = "all"
185-
to_port = "-1"
186187
}
187188
}
188189

@@ -207,21 +208,14 @@ resource "helm_release" "cilium" {
207208
name = "cilium"
208209
repository = "https://helm.cilium.io/"
209210
chart = "cilium"
210-
version = "1.15.10"
211+
version = "1.16.4"
211212
namespace = "kube-system"
212213
wait = false
213214

214215
values = [
215216
<<-EOT
216-
affinity:
217-
nodeAffinity:
218-
requiredDuringSchedulingIgnoredDuringExecution:
219-
nodeSelectorTerms:
220-
- matchExpressions:
221-
- key: eks.amazonaws.com/compute-type
222-
operator: In
223-
values:
224-
- hybrid
217+
nodeSelector:
218+
eks.amazonaws.com/compute-type: hybrid
225219
ipam:
226220
mode: cluster-pool
227221
operator:

main.tf

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,8 @@ resource "aws_eks_identity_provider_config" "this" {
691691
locals {
692692
create_node_iam_role = local.create && var.create_node_iam_role && local.auto_mode_nodepools_enabled
693693
node_iam_role_name = coalesce(var.node_iam_role_name, "${var.cluster_name}-eks-auto")
694+
695+
create_node_iam_role_custom_policy = local.create_node_iam_role && (var.enable_node_custom_tags_permissions || length(var.node_iam_role_policy_statements) > 0)
694696
}
695697

696698
data "aws_iam_policy_document" "node_assume_role_policy" {
@@ -742,3 +744,157 @@ resource "aws_iam_role_policy_attachment" "eks_auto_additional" {
742744
policy_arn = each.value
743745
role = aws_iam_role.eks_auto[0].name
744746
}
747+
748+
resource "aws_iam_role_policy_attachment" "eks_auto_custom" {
749+
count = local.create_node_iam_role_custom_policy ? 1 : 0
750+
751+
policy_arn = aws_iam_policy.eks_auto_custom[0].arn
752+
role = aws_iam_role.eks_auto[0].name
753+
}
754+
755+
data "aws_iam_policy_document" "eks_auto_custom" {
756+
count = local.create_node_iam_role_custom_policy ? 1 : 0
757+
758+
dynamic "statement" {
759+
for_each = var.enable_node_custom_tags_permissions ? [1] : []
760+
761+
content {
762+
sid = "Compute"
763+
actions = [
764+
"ec2:CreateFleet",
765+
"ec2:RunInstances",
766+
"ec2:CreateLaunchTemplate",
767+
]
768+
resources = ["*"]
769+
770+
condition {
771+
test = "StringEquals"
772+
variable = "aws:RequestTag/eks:eks-cluster-name"
773+
values = ["$${aws:PrincipalTag/eks:eks-cluster-name}"]
774+
}
775+
776+
condition {
777+
test = "StringLike"
778+
variable = "aws:RequestTag/eks:kubernetes-node-class-name"
779+
values = ["*"]
780+
}
781+
782+
condition {
783+
test = "StringLike"
784+
variable = "aws:RequestTag/eks:kubernetes-node-pool-name"
785+
values = ["*"]
786+
}
787+
}
788+
}
789+
790+
dynamic "statement" {
791+
for_each = var.enable_node_custom_tags_permissions ? [1] : []
792+
793+
content {
794+
sid = "Storage"
795+
actions = [
796+
"ec2:CreateVolume",
797+
"ec2:CreateSnapshot",
798+
]
799+
resources = [
800+
"arn:${local.partition}:ec2:*:*:volume/*",
801+
"arn:${local.partition}:ec2:*:*:snapshot/*",
802+
]
803+
804+
condition {
805+
test = "StringEquals"
806+
variable = "aws:RequestTag/eks:eks-cluster-name"
807+
values = ["$${aws:PrincipalTag/eks:eks-cluster-name}"]
808+
}
809+
}
810+
}
811+
812+
dynamic "statement" {
813+
for_each = var.enable_node_custom_tags_permissions ? [1] : []
814+
815+
content {
816+
sid = "Networking"
817+
actions = ["ec2:CreateNetworkInterface"]
818+
resources = ["*"]
819+
820+
condition {
821+
test = "StringEquals"
822+
variable = "aws:RequestTag/eks:eks-cluster-name"
823+
values = ["$${aws:PrincipalTag/eks:eks-cluster-name}"]
824+
}
825+
826+
condition {
827+
test = "StringEquals"
828+
variable = "aws:RequestTag/eks:kubernetes-cni-node-name"
829+
values = ["*"]
830+
}
831+
}
832+
}
833+
834+
dynamic "statement" {
835+
for_each = var.enable_node_custom_tags_permissions ? [1] : []
836+
837+
content {
838+
sid = "LoadBalancer"
839+
actions = [
840+
"elasticloadbalancing:CreateLoadBalancer",
841+
"elasticloadbalancing:CreateTargetGroup",
842+
"elasticloadbalancing:CreateListener",
843+
"elasticloadbalancing:CreateRule",
844+
"ec2:CreateSecurityGroup",
845+
]
846+
resources = ["*"]
847+
848+
condition {
849+
test = "StringEquals"
850+
variable = "aws:RequestTag/eks:eks-cluster-name"
851+
values = ["$${aws:PrincipalTag/eks:eks-cluster-name}"]
852+
}
853+
}
854+
}
855+
856+
dynamic "statement" {
857+
for_each = var.enable_node_custom_tags_permissions ? [1] : []
858+
859+
content {
860+
sid = "ShieldProtection"
861+
actions = ["shield:CreateProtection"]
862+
resources = ["*"]
863+
864+
condition {
865+
test = "StringEquals"
866+
variable = "aws:RequestTag/eks:eks-cluster-name"
867+
values = ["$${aws:PrincipalTag/eks:eks-cluster-name}"]
868+
}
869+
}
870+
}
871+
872+
dynamic "statement" {
873+
for_each = var.enable_node_custom_tags_permissions ? [1] : []
874+
875+
content {
876+
sid = "ShieldTagResource"
877+
actions = ["shield:TagResource"]
878+
resources = ["arn:${local.partition}:shield::*:protection/*"]
879+
880+
condition {
881+
test = "StringEquals"
882+
variable = "aws:RequestTag/eks:eks-cluster-name"
883+
values = ["$${aws:PrincipalTag/eks:eks-cluster-name}"]
884+
}
885+
}
886+
}
887+
}
888+
889+
resource "aws_iam_policy" "eks_auto_custom" {
890+
count = local.create_node_iam_role_custom_policy ? 1 : 0
891+
892+
name = var.node_iam_role_use_name_prefix ? null : local.node_iam_role_name
893+
name_prefix = var.node_iam_role_use_name_prefix ? "${local.node_iam_role_name}-" : null
894+
path = var.node_iam_role_path
895+
description = var.node_iam_role_description
896+
897+
policy = data.aws_iam_policy_document.eks_auto_custom[0].json
898+
899+
tags = merge(var.tags, var.node_iam_role_tags)
900+
}

variables.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,18 @@ variable "node_iam_role_tags" {
620620
default = {}
621621
}
622622

623+
variable "enable_node_custom_tags_permissions" {
624+
description = "Determines whether to enable permissions for custom tags for the EKS Auto node IAM role"
625+
type = bool
626+
default = true
627+
}
628+
629+
variable "node_iam_role_policy_statements" {
630+
description = "A list of IAM policy [statements](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document#statement) - used for adding specific IAM permissions as needed"
631+
type = any
632+
default = []
633+
}
634+
623635
################################################################################
624636
# Fargate
625637
################################################################################

0 commit comments

Comments
 (0)