Skip to content

Commit 6bda7ee

Browse files
workers can now be specified as multiple asgs of different flavors. BYO security group now possible for both workers and cluster
1 parent 1b92893 commit 6bda7ee

File tree

15 files changed

+358
-457
lines changed

15 files changed

+358
-457
lines changed

cluster.tf

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ resource "aws_eks_cluster" "this" {
44
version = "${var.cluster_version}"
55

66
vpc_config {
7-
security_group_ids = ["${aws_security_group.cluster.id}"]
7+
security_group_ids = ["${local.cluster_security_group_id}"]
88
subnet_ids = ["${var.subnets}"]
99
}
1010

@@ -16,39 +16,43 @@ resource "aws_eks_cluster" "this" {
1616

1717
resource "aws_security_group" "cluster" {
1818
name_prefix = "${var.cluster_name}"
19-
description = "Cluster communication with workers nodes"
19+
description = "EKS cluster security group."
2020
vpc_id = "${var.vpc_id}"
2121
tags = "${merge(var.tags, map("Name", "${var.cluster_name}-eks_cluster_sg"))}"
22+
count = "${var.cluster_security_group_id == "" ? 1 : 0}"
2223
}
2324

2425
resource "aws_security_group_rule" "cluster_egress_internet" {
25-
description = "Allow cluster egress to the Internet."
26+
description = "Allow cluster egress access to the Internet."
2627
protocol = "-1"
2728
security_group_id = "${aws_security_group.cluster.id}"
2829
cidr_blocks = ["0.0.0.0/0"]
2930
from_port = 0
3031
to_port = 0
3132
type = "egress"
33+
count = "${var.cluster_security_group_id == "" ? 1 : 0}"
3234
}
3335

3436
resource "aws_security_group_rule" "cluster_https_worker_ingress" {
35-
description = "Allow pods to communicate with the cluster API Server."
37+
description = "Allow pods to communicate with the EKS cluster API."
3638
protocol = "tcp"
3739
security_group_id = "${aws_security_group.cluster.id}"
38-
source_security_group_id = "${aws_security_group.workers.id}"
40+
source_security_group_id = "${local.worker_security_group_id}"
3941
from_port = 443
4042
to_port = 443
4143
type = "ingress"
44+
count = "${var.cluster_security_group_id == "" ? 1 : 0}"
4245
}
4346

4447
resource "aws_security_group_rule" "cluster_https_cidr_ingress" {
45-
cidr_blocks = ["${var.cluster_ingress_cidrs}"]
46-
description = "Allow communication with the cluster API Server."
48+
cidr_blocks = ["${local.workstation_external_cidr}"]
49+
description = "Allow kubectl communication with the EKS cluster API."
4750
protocol = "tcp"
4851
security_group_id = "${aws_security_group.cluster.id}"
4952
from_port = 443
5053
to_port = 443
5154
type = "ingress"
55+
count = "${var.cluster_security_group_id == "" ? 1 : 0}"
5256
}
5357

5458
resource "aws_iam_role" "cluster" {

data.tf

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
data "aws_region" "current" {}
22

3+
data "http" "workstation_external_ip" {
4+
url = "http://icanhazip.com"
5+
}
6+
37
data "aws_iam_policy_document" "workers_assume_role_policy" {
48
statement {
59
sid = "EKSWorkerAssumeRole"
@@ -15,6 +19,16 @@ data "aws_iam_policy_document" "workers_assume_role_policy" {
1519
}
1620
}
1721

22+
data "aws_ami" "eks_worker" {
23+
filter {
24+
name = "name"
25+
values = ["eks-worker-*"]
26+
}
27+
28+
most_recent = true
29+
owners = ["602401143452"] # Amazon
30+
}
31+
1832
data "aws_iam_policy_document" "cluster_assume_role_policy" {
1933
statement {
2034
sid = "EKSClusterAssumeRole"
@@ -48,3 +62,17 @@ data template_file config_map_aws_auth {
4862
role_arn = "${aws_iam_role.workers.arn}"
4963
}
5064
}
65+
66+
data template_file userdata {
67+
template = "${file("${path.module}/templates/userdata.sh.tpl")}"
68+
count = "${length(var.worker_groups)}"
69+
70+
vars {
71+
region = "${data.aws_region.current.name}"
72+
cluster_name = "${var.cluster_name}"
73+
endpoint = "${aws_eks_cluster.this.endpoint}"
74+
cluster_auth_base64 = "${aws_eks_cluster.this.certificate_authority.0.data}"
75+
max_pod_count = "${lookup(local.max_pod_per_node, lookup(var.worker_groups[count.index], "instance_type", lookup(var.workers_group_defaults, "instance_type")))}"
76+
additional_userdata = "${lookup(var.worker_groups[count.index], "additional_userdata",lookup(var.workers_group_defaults, "additional_userdata"))}"
77+
}
78+
}

examples/eks_test_fixture/main.tf

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,16 @@ provider "random" {
1111
version = "= 1.3.1"
1212
}
1313

14-
provider "http" {}
15-
provider "local" {}
16-
1714
data "aws_availability_zones" "available" {}
1815

19-
data "http" "workstation_external_ip" {
20-
url = "http://icanhazip.com"
21-
}
22-
2316
locals {
24-
workstation_external_cidr = "${chomp(data.http.workstation_external_ip.body)}/32"
25-
cluster_name = "test-eks-${random_string.suffix.result}"
17+
cluster_name = "test-eks-${random_string.suffix.result}"
18+
19+
worker_groups = "${list(
20+
map("instance_type","t2.small",
21+
"additional_userdata","echo foo bar"
22+
),
23+
)}"
2624

2725
tags = "${map("Environment", "test",
2826
"GithubRepo", "terraform-aws-eks",
@@ -50,13 +48,10 @@ module "vpc" {
5048
}
5149

5250
module "eks" {
53-
source = "../.."
54-
cluster_name = "${local.cluster_name}"
55-
subnets = "${module.vpc.public_subnets}"
56-
tags = "${local.tags}"
57-
vpc_id = "${module.vpc.vpc_id}"
58-
cluster_ingress_cidrs = ["${local.workstation_external_cidr}"]
59-
workers_instance_type = "t2.small"
60-
additional_userdata = "echo hello world"
61-
configure_kubectl_session = true
51+
source = "../.."
52+
cluster_name = "${local.cluster_name}"
53+
subnets = "${module.vpc.public_subnets}"
54+
tags = "${local.tags}"
55+
vpc_id = "${module.vpc.vpc_id}"
56+
worker_groups = "${local.worker_groups}"
6257
}

examples/eks_test_fixture/outputs.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ output "cluster_endpoint" {
33
value = "${module.eks.cluster_endpoint}"
44
}
55

6-
output "cluster_security_group_ids" {
6+
output "cluster_security_group_id" {
77
description = "Security group ids attached to the cluster control plane."
8-
value = "${module.eks.cluster_security_group_ids}"
8+
value = "${module.eks.cluster_security_group_id}"
99
}
1010

1111
output "kubectl_config" {

kubectl.tf

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
resource "local_file" "kubeconfig" {
2+
content = "${data.template_file.kubeconfig.rendered}"
3+
filename = "${var.config_output_path}/kubeconfig"
4+
count = "${var.configure_kubectl_session ? 1 : 0}"
5+
}
6+
7+
resource "local_file" "config_map_aws_auth" {
8+
content = "${data.template_file.config_map_aws_auth.rendered}"
9+
filename = "${var.config_output_path}/config-map-aws-auth.yaml"
10+
count = "${var.configure_kubectl_session ? 1 : 0}"
11+
}
12+
13+
resource "null_resource" "configure_kubectl" {
14+
provisioner "local-exec" {
15+
command = "kubectl apply -f ${var.config_output_path}/config-map-aws-auth.yaml --kubeconfig ${var.config_output_path}/kubeconfig"
16+
}
17+
18+
triggers {
19+
config_map_rendered = "${data.template_file.config_map_aws_auth.rendered}"
20+
kubeconfig_rendered = "${data.template_file.kubeconfig.rendered}"
21+
}
22+
23+
count = "${var.configure_kubectl_session ? 1 : 0}"
24+
}

0 commit comments

Comments
 (0)