Skip to content
This repository was archived by the owner on May 17, 2024. It is now read-only.

Commit b471f5e

Browse files
committed
Use eks terraform provider for amazon cluster
Signed-off-by: JoshVanL <[email protected]>
1 parent 174bf2d commit b471f5e

File tree

7 files changed

+93
-318
lines changed

7 files changed

+93
-318
lines changed

demo/infrastructure/amazon/aws-auth.yaml

Lines changed: 0 additions & 12 deletions
This file was deleted.

demo/infrastructure/amazon/ouputs.tf

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ output "config" {
1010
value = "${jsonencode(local.config)}"
1111
}
1212

13-
output "kubeconfig_command" {
14-
value = "eksctl utils write-kubeconfig --name=cluster-${random_id.suffix.hex} --kubeconfig=$KUBECONFIG --set-kubeconfig-context=true --region=${var.region} && cat infrastructure/amazon/aws-auth.yaml | sed -e \"s~{{ROLE_ARN}}~${module.cluster.cluster_node_arn}~g\" | kubectl apply -f -"
15-
}
13+
#output "kubeconfig_command" {
14+
# value = "eksctl utils write-kubeconfig --name=cluster-${random_id.suffix.hex} --kubeconfig=$KUBECONFIG --set-kubeconfig-context=true --region=${var.region} && cat infrastructure/amazon/aws-auth.yaml | sed -e \"s~{{ROLE_ARN}}~${module.cluster.cluster_node_arn}~g\" | kubectl apply -f -"
15+
#}
1616

1717
output "kubeconfig" {
18-
value = "${module.cluster.kubeconfig}"
18+
description = "kubectl config as generated by the module."
19+
value = "${module.cluster.kubeconfig}"
1920
}
Lines changed: 85 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,105 @@
11
variable "suffix" {}
22

3-
data "aws_region" "current" {}
3+
data "aws_availability_zones" "available" {}
44

5-
resource "aws_eks_cluster" "cluster" {
6-
name = "cluster-${var.suffix}"
7-
role_arn = "${aws_iam_role.cluster.arn}"
5+
locals {
6+
cluster_name = "cluster-${var.suffix}"
87

9-
vpc_config {
10-
security_group_ids = ["${aws_security_group.cluster.id}"]
11-
subnet_ids = ["${aws_subnet.cluster.*.id}"]
8+
worker_groups = [
9+
{
10+
instance_type = "t2.large"
11+
subnets = "${join(",", module.vpc.private_subnets)}"
12+
asg_desired_capacity = "2"
13+
},
14+
]
15+
tags = {
16+
Workspace = "${terraform.workspace}"
1217
}
13-
14-
depends_on = [
15-
"aws_iam_role_policy_attachment.cluster-AmazonEKSClusterPolicy",
16-
"aws_iam_role_policy_attachment.cluster-AmazonEKSServicePolicy",
18+
worker_groups_launch_template = [
19+
{
20+
# This will launch an autoscaling group with only Spot Fleet instances
21+
instance_type = "t2.small"
22+
additional_userdata = "echo foo bar"
23+
subnets = "${join(",", module.vpc.private_subnets)}"
24+
additional_security_group_ids = "${aws_security_group.worker_group_mgmt_one.id},${aws_security_group.worker_group_mgmt_two.id}"
25+
override_instance_type = "t3.small"
26+
asg_desired_capacity = "2"
27+
spot_instance_pools = 10
28+
on_demand_percentage_above_base_capacity = "0"
29+
},
1730
]
1831
}
1932

20-
data "aws_ami" "eks-worker" {
21-
filter {
22-
name = "name"
23-
values = ["amazon-eks-node-${aws_eks_cluster.cluster.version}-v*"]
24-
}
33+
resource "aws_security_group" "worker_group_mgmt_one" {
34+
name_prefix = "worker_group_mgmt_one"
35+
description = "SG to be applied to all *nix machines"
36+
vpc_id = "${module.vpc.vpc_id}"
2537

26-
most_recent = true
27-
owners = ["602401143452"] # Amazon EKS AMI Account ID
28-
}
38+
ingress {
39+
from_port = 22
40+
to_port = 22
41+
protocol = "tcp"
2942

30-
# EKS currently documents this required userdata for EKS worker nodes to
31-
# properly configure Kubernetes applications on the EC2 instance.
32-
# We utilize a Terraform local here to simplify Base64 encoding this
33-
# information into the AutoScaling Launch Configuration.
34-
# More information: https://docs.aws.amazon.com/eks/latest/userguide/launch-workers.html
35-
locals {
36-
cluster-node-userdata = <<USERDATA
37-
#!/bin/bash
38-
set -o xtrace
39-
/etc/eks/bootstrap.sh --apiserver-endpoint '${aws_eks_cluster.cluster.endpoint}' --b64-cluster-ca '${aws_eks_cluster.cluster.certificate_authority.0.data}' 'cluster-${var.suffix}'
40-
USERDATA
43+
cidr_blocks = [
44+
"10.0.0.0/8",
45+
]
46+
}
4147
}
4248

43-
resource "aws_launch_configuration" "cluster" {
44-
associate_public_ip_address = true
45-
iam_instance_profile = "${aws_iam_instance_profile.cluster-node.name}"
46-
image_id = "${data.aws_ami.eks-worker.id}"
47-
instance_type = "m4.large"
48-
name_prefix = "cluster-${var.suffix}"
49-
security_groups = ["${aws_security_group.cluster-node.id}"]
50-
user_data_base64 = "${base64encode(local.cluster-node-userdata)}"
49+
resource "aws_security_group" "worker_group_mgmt_two" {
50+
name_prefix = "worker_group_mgmt_two"
51+
vpc_id = "${module.vpc.vpc_id}"
5152

52-
lifecycle {
53-
create_before_destroy = true
53+
ingress {
54+
from_port = 22
55+
to_port = 22
56+
protocol = "tcp"
57+
58+
cidr_blocks = [
59+
"192.168.0.0/16",
60+
]
5461
}
5562
}
5663

57-
resource "aws_autoscaling_group" "cluster" {
58-
desired_capacity = 2
59-
launch_configuration = "${aws_launch_configuration.cluster.id}"
60-
max_size = 2
61-
min_size = 1
62-
name = "terraform-eks-cluster"
63-
vpc_zone_identifier = ["${aws_subnet.cluster.*.id}"]
64+
resource "aws_security_group" "all_worker_mgmt" {
65+
name_prefix = "all_worker_management"
66+
vpc_id = "${module.vpc.vpc_id}"
6467

65-
tag {
66-
key = "Name"
67-
value = "cluster-${var.suffix}"
68-
propagate_at_launch = true
69-
}
68+
ingress {
69+
from_port = 22
70+
to_port = 22
71+
protocol = "tcp"
7072

71-
tag {
72-
key = "kubernetes.io/cluster/cluster-${var.suffix}"
73-
value = "owned"
74-
propagate_at_launch = true
73+
cidr_blocks = [
74+
"10.0.0.0/8",
75+
"172.16.0.0/12",
76+
"192.168.0.0/16",
77+
]
7578
}
7679
}
80+
81+
module "vpc" {
82+
source = "terraform-aws-modules/vpc/aws"
83+
version = "1.60.0"
84+
name = "test-vpc"
85+
cidr = "10.0.0.0/16"
86+
azs = ["${data.aws_availability_zones.available.names[0]}", "${data.aws_availability_zones.available.names[1]}", "${data.aws_availability_zones.available.names[2]}"]
87+
private_subnets = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]
88+
public_subnets = ["10.0.4.0/24", "10.0.5.0/24", "10.0.6.0/24"]
89+
enable_nat_gateway = true
90+
single_nat_gateway = true
91+
tags = "${merge(local.tags, map("kubernetes.io/cluster/${local.cluster_name}", "shared"))}"
92+
}
93+
94+
module "eks" {
95+
source = "terraform-aws-modules/eks/aws"
96+
cluster_name = "${local.cluster_name}"
97+
subnets = ["${module.vpc.private_subnets}"]
98+
tags = "${local.tags}"
99+
vpc_id = "${module.vpc.vpc_id}"
100+
worker_groups = "${local.worker_groups}"
101+
worker_groups_launch_template = "${local.worker_groups_launch_template}"
102+
worker_group_count = "1"
103+
worker_group_launch_template_count = "1"
104+
worker_additional_security_group_ids = ["${aws_security_group.all_worker_mgmt.id}"]
105+
}

demo/infrastructure/modules/amazon-cluster/iam.tf

Lines changed: 0 additions & 67 deletions
This file was deleted.
Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,7 @@
1-
locals {
2-
kubeconfig = <<KUBECONFIG
3-
4-
5-
apiVersion: v1
6-
clusters:
7-
- cluster:
8-
server: ${aws_eks_cluster.cluster.endpoint}
9-
certificate-authority-data: ${aws_eks_cluster.cluster.certificate_authority.0.data}
10-
name: kubernetes
11-
contexts:
12-
- context:
13-
cluster: kubernetes
14-
user: aws
15-
name: aws
16-
current-context: aws
17-
kind: Config
18-
preferences: {}
19-
users:
20-
- name: aws
21-
user:
22-
exec:
23-
apiVersion: client.authentication.k8s.io/v1alpha1
24-
command: aws-iam-authenticator
25-
args:
26-
- "token"
27-
- "-i"
28-
- "cluster-${var.suffix}"
29-
KUBECONFIG
1+
output "cluster_node_arn" {
2+
value = "${module.eks.worker_iam_role_arn}"
303
}
314

325
output "kubeconfig" {
33-
value = "${local.kubeconfig}"
34-
}
35-
36-
output "cluster_node_arn" {
37-
value = "${aws_iam_role.cluster-node.arn}"
6+
value = "${module.eks.kubeconfig}"
387
}

demo/infrastructure/modules/amazon-cluster/security_groups.tf

Lines changed: 0 additions & 94 deletions
This file was deleted.

0 commit comments

Comments
 (0)