Skip to content

Commit 0180644

Browse files
mr-joshuamax-rocket-internet
authored andcommitted
Allow additional security groups to be included in worker launch configurations (#112)
* Allow additional security groups to be included for all workers and each worker group #47 * update changelog with reference to issue and be more descriptive * Update CHANGELOG.md * address pr comments and rebase * rebase * fix bug introduced by PR#115 that sets the AMI id to the default value of "" always * rebase * align default value of additional_security_group_ids to be pulled from local var workers_group_defaults_defaults
1 parent da6ff7d commit 0180644

File tree

6 files changed

+99
-35
lines changed

6 files changed

+99
-35
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ project adheres to [Semantic Versioning](http://semver.org/).
1010
### Added
1111

1212
- add support for [`amazon-eks-node-*` AMI with bootstrap script](https://aws.amazon.com/blogs/opensource/improvements-eks-worker-node-provisioning/) (by @erks)
13-
- expose `kubelet_extra_args` worker group option (replacing `kubelet_node_labels`) to allow specifying arbitrary kubelet options (e.g. taints and labels) (by @erks)
13+
- expose `kubelet_extra_args` worker group option (replacing `kubelet_node_labels`) to allow specifying arbitrary kubelet options (e.g. taints and labels) (by @erks)
14+
- add optional input `worker_additional_security_group_ids` to allow one or more additional security groups to be added to all worker launch configurations - #47 (by @hhobbsh @mr-joshua)
15+
- add optional input `additional_security_group_ids` to allow one or more additional security groups to be added to a specific worker launch configuration - #47 (by @mr-joshua)
1416

1517
### Changed
1618

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ MIT Licensed. See [LICENSE](https://github.com/terraform-aws-modules/terraform-a
114114
| subnets | A list of subnets to place the EKS cluster and workers within. | list | - | yes |
115115
| tags | A map of tags to add to all resources. | map | `<map>` | no |
116116
| vpc_id | VPC where the cluster and workers will be deployed. | string | - | yes |
117+
| worker_additional_security_group_ids | A list of additional security group ids to attach to worker instances | list | `<list>` | no |
117118
| worker_group_count | The number of maps contained within the worker_groups list. | string | `1` | no |
118119
| worker_groups | A list of maps defining worker group configurations. See workers_group_defaults for valid keys. | list | `<list>` | no |
119120
| worker_security_group_id | If provided, all workers will be attached to this security group. If not given, a security group will be created with necessary ingres/egress to work with the EKS cluster. | string | `` | no |

examples/eks_test_fixture/main.tf

Lines changed: 68 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,15 @@ locals {
3636
# )}"
3737

3838
worker_groups = "${list(
39-
map("instance_type","t2.small",
40-
"additional_userdata","echo foo bar",
41-
"subnets", "${join(",", module.vpc.private_subnets)}",
42-
),
39+
map("instance_type","t2.small",
40+
"additional_userdata","echo foo bar",
41+
"subnets", "${join(",", module.vpc.private_subnets)}",
42+
),
43+
map("instance_type","t2.small",
44+
"additional_userdata","echo foo bar",
45+
"subnets", "${join(",", module.vpc.private_subnets)}",
46+
"additional_security_group_ids", "${aws_security_group.worker_group_mgmt_one.id},${aws_security_group.worker_group_mgmt_two.id}"
47+
)
4348
)}"
4449
tags = "${map("Environment", "test",
4550
"GithubRepo", "terraform-aws-eks",
@@ -53,6 +58,54 @@ resource "random_string" "suffix" {
5358
special = false
5459
}
5560

61+
resource "aws_security_group" "worker_group_mgmt_one" {
62+
name_prefix = "worker_group_mgmt_one"
63+
description = "SG to be applied to all *nix machines"
64+
vpc_id = "${module.vpc.vpc_id}"
65+
66+
ingress {
67+
from_port = 22
68+
to_port = 22
69+
protocol = "tcp"
70+
71+
cidr_blocks = [
72+
"10.0.0.0/8",
73+
]
74+
}
75+
}
76+
77+
resource "aws_security_group" "worker_group_mgmt_two" {
78+
name_prefix = "worker_group_mgmt_two"
79+
vpc_id = "${module.vpc.vpc_id}"
80+
81+
ingress {
82+
from_port = 22
83+
to_port = 22
84+
protocol = "tcp"
85+
86+
cidr_blocks = [
87+
"192.168.0.0/16",
88+
]
89+
}
90+
}
91+
92+
resource "aws_security_group" "all_worker_mgmt" {
93+
name_prefix = "all_worker_management"
94+
vpc_id = "${module.vpc.vpc_id}"
95+
96+
ingress {
97+
from_port = 22
98+
to_port = 22
99+
protocol = "tcp"
100+
101+
cidr_blocks = [
102+
"10.0.0.0/8",
103+
"172.16.0.0/12",
104+
"192.168.0.0/16",
105+
]
106+
}
107+
}
108+
56109
module "vpc" {
57110
source = "terraform-aws-modules/vpc/aws"
58111
version = "1.14.0"
@@ -67,14 +120,15 @@ module "vpc" {
67120
}
68121

69122
module "eks" {
70-
source = "../.."
71-
cluster_name = "${local.cluster_name}"
72-
subnets = ["${module.vpc.private_subnets}"]
73-
tags = "${local.tags}"
74-
vpc_id = "${module.vpc.vpc_id}"
75-
worker_groups = "${local.worker_groups}"
76-
worker_group_count = "1"
77-
map_roles = "${var.map_roles}"
78-
map_users = "${var.map_users}"
79-
map_accounts = "${var.map_accounts}"
123+
source = "../.."
124+
cluster_name = "${local.cluster_name}"
125+
subnets = ["${module.vpc.private_subnets}"]
126+
tags = "${local.tags}"
127+
vpc_id = "${module.vpc.vpc_id}"
128+
worker_groups = "${local.worker_groups}"
129+
worker_group_count = "2"
130+
worker_additional_security_group_ids = ["${aws_security_group.all_worker_mgmt.id}"]
131+
map_roles = "${var.map_roles}"
132+
map_users = "${var.map_users}"
133+
map_accounts = "${var.map_accounts}"
80134
}

local.tf

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,26 @@ locals {
99
kubeconfig_name = "${var.kubeconfig_name == "" ? "eks_${var.cluster_name}" : var.kubeconfig_name}"
1010

1111
workers_group_defaults_defaults = {
12-
name = "count.index" # Name of the worker group. Literal count.index will never be used but if name is not set, the count.index interpolation will be used.
13-
ami_id = "${data.aws_ami.eks_worker.id}" # AMI ID for the eks workers. If none is provided, Terraform will search for the latest version of their EKS optimized worker AMI.
14-
asg_desired_capacity = "1" # Desired worker capacity in the autoscaling group.
15-
asg_max_size = "3" # Maximum worker capacity in the autoscaling group.
16-
asg_min_size = "1" # Minimum worker capacity in the autoscaling group.
17-
instance_type = "m4.large" # Size of the workers instances.
18-
spot_price = "" # Cost of spot instance.
19-
root_volume_size = "100" # root volume size of workers instances.
20-
root_volume_type = "gp2" # root volume type of workers instances, can be 'standard', 'gp2', or 'io1'
21-
root_iops = "0" # The amount of provisioned IOPS. This must be set with a volume_type of "io1".
22-
key_name = "" # The key name that should be used for the instances in the autoscaling group
23-
pre_userdata = "" # userdata to pre-append to the default userdata.
24-
additional_userdata = "" # userdata to append to the default userdata.
25-
ebs_optimized = true # sets whether to use ebs optimization on supported types.
26-
enable_monitoring = true # Enables/disables detailed monitoring.
27-
public_ip = false # Associate a public ip address with a worker
28-
kubelet_extra_args = "" # This string is passed directly to kubelet if set. Useful for adding labels or taints.
29-
subnets = "" # A comma delimited string of subnets to place the worker nodes in. i.e. subnet-123,subnet-456,subnet-789
30-
autoscaling_enabled = false # Sets whether policy and matching tags will be added to allow autoscaling.
12+
name = "count.index" # Name of the worker group. Literal count.index will never be used but if name is not set, the count.index interpolation will be used.
13+
ami_id = "${data.aws_ami.eks_worker.id}" # AMI ID for the eks workers. If none is provided, Terraform will search for the latest version of their EKS optimized worker AMI.
14+
asg_desired_capacity = "1" # Desired worker capacity in the autoscaling group.
15+
asg_max_size = "3" # Maximum worker capacity in the autoscaling group.
16+
asg_min_size = "1" # Minimum worker capacity in the autoscaling group.
17+
instance_type = "m4.large" # Size of the workers instances.
18+
spot_price = "" # Cost of spot instance.
19+
root_volume_size = "100" # root volume size of workers instances.
20+
root_volume_type = "gp2" # root volume type of workers instances, can be 'standard', 'gp2', or 'io1'
21+
root_iops = "0" # The amount of provisioned IOPS. This must be set with a volume_type of "io1".
22+
key_name = "" # The key name that should be used for the instances in the autoscaling group
23+
pre_userdata = "" # userdata to pre-append to the default userdata.
24+
additional_userdata = "" # userdata to append to the default userdata.
25+
ebs_optimized = true # sets whether to use ebs optimization on supported types.
26+
enable_monitoring = true # Enables/disables detailed monitoring.
27+
public_ip = false # Associate a public ip address with a worker
28+
kubelet_extra_args = "" # This string is passed directly to kubelet if set. Useful for adding labels or taints.
29+
subnets = "" # A comma delimited string of subnets to place the worker nodes in. i.e. subnet-123,subnet-456,subnet-789
30+
autoscaling_enabled = false # Sets whether policy and matching tags will be added to allow autoscaling.
31+
additional_security_group_ids = "" # A comman delimited list of additional security group ids to include in worker launch config
3132
}
3233

3334
workers_group_defaults = "${merge(local.workers_group_defaults_defaults, var.workers_group_defaults)}"

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ variable "worker_security_group_id" {
9191
default = ""
9292
}
9393

94+
variable "worker_additional_security_group_ids" {
95+
description = "A list of additional security group ids to attach to worker instances"
96+
type = "list"
97+
default = []
98+
}
99+
94100
variable "worker_sg_ingress_from_port" {
95101
description = "Minimum port number from which pods will accept communication. Must be changed to a lower value if some pods in your cluster will expose a port lower than 1025 (e.g. 22, 80, or 443)."
96102
default = "1025"

workers.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ resource "aws_autoscaling_group" "workers" {
2424
resource "aws_launch_configuration" "workers" {
2525
name_prefix = "${aws_eks_cluster.this.name}-${lookup(var.worker_groups[count.index], "name", count.index)}"
2626
associate_public_ip_address = "${lookup(var.worker_groups[count.index], "public_ip", lookup(local.workers_group_defaults, "public_ip"))}"
27-
security_groups = ["${local.worker_security_group_id}"]
27+
security_groups = ["${local.worker_security_group_id}", "${var.worker_additional_security_group_ids}", "${compact(split(",",lookup(var.worker_groups[count.index],"additional_security_group_ids",lookup(local.workers_group_defaults, "additional_security_group_ids"))))}"]
2828
iam_instance_profile = "${aws_iam_instance_profile.workers.id}"
2929
image_id = "${lookup(var.worker_groups[count.index], "ami_id", lookup(local.workers_group_defaults, "ami_id"))}"
3030
instance_type = "${lookup(var.worker_groups[count.index], "instance_type", lookup(local.workers_group_defaults, "instance_type"))}"

0 commit comments

Comments
 (0)