Skip to content

Commit eaf9b8c

Browse files
bryantbiggsspr-mweber3
authored andcommitted
feat: Allow users to selectively attach the EKS created cluster primary security group to nodes (terraform-aws-modules#1952)
1 parent bf01956 commit eaf9b8c

File tree

9 files changed

+45
-26
lines changed

9 files changed

+45
-26
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,7 @@ Module provided default templates can be found under the [templates directory](h
658658
- Users have the ability to opt out of the security group creation and instead provide their own externally created security group if so desired
659659
- The security group that is created is designed to handle the bare minimum communication necessary between the control plane and the nodes, as well as any external egress to allow the cluster to successfully launch without error
660660
- Users also have the option to supply additional, externally created security groups to the cluster as well via the `cluster_additional_security_group_ids` variable
661+
- Lastly, users are able to opt in to attaching the primary security group automatically created by the EKS service by setting `attach_cluster_primary_security_group` = `true` from the root module for the respective node group (or set it within the node group defaults). This security group is not managed by the module; it is created by the EKS service. It permits all traffic within the domain of the security group as well as all egress traffic to the internet.
661662

662663
- Node Group Security Group(s)
663664
- Each node group (EKS Managed Node Group and Self Managed Node Group) by default creates its own security group. By default, this security group does not contain any additional security group rules. It is merely an "empty container" that offers users the ability to opt into any addition inbound our outbound rules as necessary

examples/complete/main.tf

+9-7
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,12 @@ module "eks" {
108108

109109
# EKS Managed Node Group(s)
110110
eks_managed_node_group_defaults = {
111-
ami_type = "AL2_x86_64"
112-
disk_size = 50
113-
instance_types = ["m6i.large", "m5.large", "m5n.large", "m5zn.large"]
114-
vpc_security_group_ids = [aws_security_group.additional.id]
111+
ami_type = "AL2_x86_64"
112+
disk_size = 50
113+
instance_types = ["m6i.large", "m5.large", "m5n.large", "m5zn.large"]
114+
115+
attach_cluster_primary_security_group = true
116+
vpc_security_group_ids = [aws_security_group.additional.id]
115117
}
116118

117119
eks_managed_node_groups = {
@@ -188,10 +190,10 @@ module "eks_managed_node_group" {
188190
cluster_name = module.eks.cluster_id
189191
cluster_version = local.cluster_version
190192

191-
vpc_id = module.vpc.vpc_id
192-
subnet_ids = module.vpc.private_subnets
193+
vpc_id = module.vpc.vpc_id
194+
subnet_ids = module.vpc.private_subnets
195+
cluster_primary_security_group_id = module.eks.cluster_primary_security_group_id
193196
vpc_security_group_ids = [
194-
module.eks.cluster_primary_security_group_id,
195197
module.eks.cluster_security_group_id,
196198
]
197199

modules/eks-managed-node-group/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ module "eks_managed_node_group" {
9393
| <a name="input_cluster_endpoint"></a> [cluster\_endpoint](#input\_cluster\_endpoint) | Endpoint of associated EKS cluster | `string` | `""` | no |
9494
| <a name="input_cluster_ip_family"></a> [cluster\_ip\_family](#input\_cluster\_ip\_family) | The IP family used to assign Kubernetes pod and service addresses. Valid values are `ipv4` (default) and `ipv6` | `string` | `null` | no |
9595
| <a name="input_cluster_name"></a> [cluster\_name](#input\_cluster\_name) | Name of associated EKS cluster | `string` | `null` | no |
96+
| <a name="input_cluster_primary_security_group_id"></a> [cluster\_primary\_security\_group\_id](#input\_cluster\_primary\_security\_group\_id) | The ID of the EKS cluster primary security group to associate with the instance(s). This is the security group that is automatically created by the EKS service | `string` | `null` | no |
9697
| <a name="input_cluster_security_group_id"></a> [cluster\_security\_group\_id](#input\_cluster\_security\_group\_id) | Cluster control plane security group ID | `string` | `null` | no |
9798
| <a name="input_cluster_service_ipv4_cidr"></a> [cluster\_service\_ipv4\_cidr](#input\_cluster\_service\_ipv4\_cidr) | The CIDR block to assign Kubernetes service IP addresses from. If you don't specify a block, Kubernetes assigns addresses from either the 10.100.0.0/16 or 172.20.0.0/16 CIDR blocks | `string` | `null` | no |
9899
| <a name="input_cluster_version"></a> [cluster\_version](#input\_cluster\_version) | Kubernetes version. Defaults to EKS Cluster Kubernetes version | `string` | `null` | no |

modules/eks-managed-node-group/main.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ resource "aws_launch_template" "this" {
5454
key_name = var.key_name
5555
user_data = module.user_data.user_data
5656

57-
vpc_security_group_ids = compact(concat([try(aws_security_group.this[0].id, "")], var.vpc_security_group_ids))
57+
vpc_security_group_ids = compact(concat([try(aws_security_group.this[0].id, ""), var.cluster_primary_security_group_id], var.vpc_security_group_ids))
5858

5959
default_version = var.launch_template_default_version
6060
update_default_version = var.update_launch_template_default_version

modules/eks-managed-node-group/variables.tf

+6
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@ variable "vpc_security_group_ids" {
126126
default = []
127127
}
128128

129+
variable "cluster_primary_security_group_id" {
130+
description = "The ID of the EKS cluster primary security group to associate with the instance(s). This is the security group that is automatically created by the EKS service"
131+
type = string
132+
default = null
133+
}
134+
129135
variable "launch_template_default_version" {
130136
description = "Default version of the launch template"
131137
type = string

modules/self-managed-node-group/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ module "self_managed_node_group" {
8686
| <a name="input_cluster_endpoint"></a> [cluster\_endpoint](#input\_cluster\_endpoint) | Endpoint of associated EKS cluster | `string` | `""` | no |
8787
| <a name="input_cluster_ip_family"></a> [cluster\_ip\_family](#input\_cluster\_ip\_family) | The IP family used to assign Kubernetes pod and service addresses. Valid values are `ipv4` (default) and `ipv6` | `string` | `null` | no |
8888
| <a name="input_cluster_name"></a> [cluster\_name](#input\_cluster\_name) | Name of associated EKS cluster | `string` | `""` | no |
89+
| <a name="input_cluster_primary_security_group_id"></a> [cluster\_primary\_security\_group\_id](#input\_cluster\_primary\_security\_group\_id) | The ID of the EKS cluster primary security group to associate with the instance(s). This is the security group that is automatically created by the EKS service | `string` | `null` | no |
8990
| <a name="input_cluster_security_group_id"></a> [cluster\_security\_group\_id](#input\_cluster\_security\_group\_id) | Cluster control plane security group ID | `string` | `null` | no |
9091
| <a name="input_cluster_version"></a> [cluster\_version](#input\_cluster\_version) | Kubernetes cluster version - used to lookup default AMI ID if one is not provided | `string` | `null` | no |
9192
| <a name="input_cpu_options"></a> [cpu\_options](#input\_cpu\_options) | The CPU options for the instance | `map(string)` | `null` | no |

modules/self-managed-node-group/main.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ resource "aws_launch_template" "this" {
5757
key_name = var.key_name
5858
user_data = module.user_data.user_data
5959

60-
vpc_security_group_ids = compact(concat([try(aws_security_group.this[0].id, "")], var.vpc_security_group_ids))
60+
vpc_security_group_ids = compact(concat([try(aws_security_group.this[0].id, ""), var.cluster_primary_security_group_id], var.vpc_security_group_ids))
6161

6262
default_version = var.launch_template_default_version
6363
update_default_version = var.update_launch_template_default_version

modules/self-managed-node-group/variables.tf

+6
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,12 @@ variable "vpc_security_group_ids" {
234234
default = []
235235
}
236236

237+
variable "cluster_primary_security_group_id" {
238+
description = "The ID of the EKS cluster primary security group to associate with the instance(s). This is the security group that is automatically created by the EKS service"
239+
type = string
240+
default = null
241+
}
242+
237243
variable "enable_monitoring" {
238244
description = "Enables/disables detailed monitoring"
239245
type = bool

node_groups.tf

+19-17
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,6 @@ module "eks_managed_node_group" {
281281

282282
ebs_optimized = try(each.value.ebs_optimized, var.eks_managed_node_group_defaults.ebs_optimized, null)
283283
key_name = try(each.value.key_name, var.eks_managed_node_group_defaults.key_name, null)
284-
vpc_security_group_ids = compact(concat([local.node_security_group_id], try(each.value.vpc_security_group_ids, var.eks_managed_node_group_defaults.vpc_security_group_ids, [])))
285284
launch_template_default_version = try(each.value.launch_template_default_version, var.eks_managed_node_group_defaults.launch_template_default_version, null)
286285
update_launch_template_default_version = try(each.value.update_launch_template_default_version, var.eks_managed_node_group_defaults.update_launch_template_default_version, true)
287286
disable_api_termination = try(each.value.disable_api_termination, var.eks_managed_node_group_defaults.disable_api_termination, null)
@@ -315,13 +314,15 @@ module "eks_managed_node_group" {
315314
iam_role_additional_policies = try(each.value.iam_role_additional_policies, var.eks_managed_node_group_defaults.iam_role_additional_policies, [])
316315

317316
# Security group
318-
create_security_group = try(each.value.create_security_group, var.eks_managed_node_group_defaults.create_security_group, true)
319-
security_group_name = try(each.value.security_group_name, var.eks_managed_node_group_defaults.security_group_name, null)
320-
security_group_use_name_prefix = try(each.value.security_group_use_name_prefix, var.eks_managed_node_group_defaults.security_group_use_name_prefix, true)
321-
security_group_description = try(each.value.security_group_description, var.eks_managed_node_group_defaults.security_group_description, "EKS managed node group security group")
322-
vpc_id = try(each.value.vpc_id, var.eks_managed_node_group_defaults.vpc_id, var.vpc_id)
323-
security_group_rules = try(each.value.security_group_rules, var.eks_managed_node_group_defaults.security_group_rules, {})
324-
security_group_tags = try(each.value.security_group_tags, var.eks_managed_node_group_defaults.security_group_tags, {})
317+
vpc_security_group_ids = compact(concat([local.node_security_group_id], try(each.value.vpc_security_group_ids, var.eks_managed_node_group_defaults.vpc_security_group_ids, [])))
318+
cluster_primary_security_group_id = try(each.value.attach_cluster_primary_security_group, var.eks_managed_node_group_defaults.attach_cluster_primary_security_group, false) ? aws_eks_cluster.this[0].vpc_config[0].cluster_security_group_id : null
319+
create_security_group = try(each.value.create_security_group, var.eks_managed_node_group_defaults.create_security_group, true)
320+
security_group_name = try(each.value.security_group_name, var.eks_managed_node_group_defaults.security_group_name, null)
321+
security_group_use_name_prefix = try(each.value.security_group_use_name_prefix, var.eks_managed_node_group_defaults.security_group_use_name_prefix, true)
322+
security_group_description = try(each.value.security_group_description, var.eks_managed_node_group_defaults.security_group_description, "EKS managed node group security group")
323+
vpc_id = try(each.value.vpc_id, var.eks_managed_node_group_defaults.vpc_id, var.vpc_id)
324+
security_group_rules = try(each.value.security_group_rules, var.eks_managed_node_group_defaults.security_group_rules, {})
325+
security_group_tags = try(each.value.security_group_tags, var.eks_managed_node_group_defaults.security_group_tags, {})
325326

326327
tags = merge(var.tags, try(each.value.tags, var.eks_managed_node_group_defaults.tags, {}))
327328
}
@@ -405,8 +406,6 @@ module "self_managed_node_group" {
405406
instance_type = try(each.value.instance_type, var.self_managed_node_group_defaults.instance_type, "m6i.large")
406407
key_name = try(each.value.key_name, var.self_managed_node_group_defaults.key_name, null)
407408

408-
vpc_security_group_ids = compact(concat([local.node_security_group_id], try(each.value.vpc_security_group_ids, var.self_managed_node_group_defaults.vpc_security_group_ids, [])))
409-
cluster_security_group_id = local.cluster_security_group_id
410409
launch_template_default_version = try(each.value.launch_template_default_version, var.self_managed_node_group_defaults.launch_template_default_version, null)
411410
update_launch_template_default_version = try(each.value.update_launch_template_default_version, var.self_managed_node_group_defaults.update_launch_template_default_version, true)
412411
disable_api_termination = try(each.value.disable_api_termination, var.self_managed_node_group_defaults.disable_api_termination, null)
@@ -442,13 +441,16 @@ module "self_managed_node_group" {
442441
iam_role_additional_policies = try(each.value.iam_role_additional_policies, var.self_managed_node_group_defaults.iam_role_additional_policies, [])
443442

444443
# Security group
445-
create_security_group = try(each.value.create_security_group, var.self_managed_node_group_defaults.create_security_group, true)
446-
security_group_name = try(each.value.security_group_name, var.self_managed_node_group_defaults.security_group_name, null)
447-
security_group_use_name_prefix = try(each.value.security_group_use_name_prefix, var.self_managed_node_group_defaults.security_group_use_name_prefix, true)
448-
security_group_description = try(each.value.security_group_description, var.self_managed_node_group_defaults.security_group_description, "Self managed node group security group")
449-
vpc_id = try(each.value.vpc_id, var.self_managed_node_group_defaults.vpc_id, var.vpc_id)
450-
security_group_rules = try(each.value.security_group_rules, var.self_managed_node_group_defaults.security_group_rules, {})
451-
security_group_tags = try(each.value.security_group_tags, var.self_managed_node_group_defaults.security_group_tags, {})
444+
vpc_security_group_ids = compact(concat([local.node_security_group_id], try(each.value.vpc_security_group_ids, var.self_managed_node_group_defaults.vpc_security_group_ids, [])))
445+
cluster_security_group_id = local.cluster_security_group_id
446+
cluster_primary_security_group_id = try(each.value.attach_cluster_primary_security_group, var.self_managed_node_group_defaults.attach_cluster_primary_security_group, false) ? aws_eks_cluster.this[0].vpc_config[0].cluster_security_group_id : null
447+
create_security_group = try(each.value.create_security_group, var.self_managed_node_group_defaults.create_security_group, true)
448+
security_group_name = try(each.value.security_group_name, var.self_managed_node_group_defaults.security_group_name, null)
449+
security_group_use_name_prefix = try(each.value.security_group_use_name_prefix, var.self_managed_node_group_defaults.security_group_use_name_prefix, true)
450+
security_group_description = try(each.value.security_group_description, var.self_managed_node_group_defaults.security_group_description, "Self managed node group security group")
451+
vpc_id = try(each.value.vpc_id, var.self_managed_node_group_defaults.vpc_id, var.vpc_id)
452+
security_group_rules = try(each.value.security_group_rules, var.self_managed_node_group_defaults.security_group_rules, {})
453+
security_group_tags = try(each.value.security_group_tags, var.self_managed_node_group_defaults.security_group_tags, {})
452454

453455
tags = merge(var.tags, try(each.value.tags, var.self_managed_node_group_defaults.tags, {}))
454456
}

0 commit comments

Comments
 (0)