|
1 | 1 | variable "suffix" {}
|
2 | 2 |
|
3 |
| -data "aws_region" "current" {} |
| 3 | +data "aws_availability_zones" "available" {} |
4 | 4 |
|
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}" |
8 | 7 |
|
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}" |
12 | 17 | }
|
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 | + }, |
17 | 30 | ]
|
18 | 31 | }
|
19 | 32 |
|
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}" |
25 | 37 |
|
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" |
29 | 42 |
|
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 | + } |
41 | 47 | }
|
42 | 48 |
|
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}" |
51 | 52 |
|
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 | + ] |
54 | 61 | }
|
55 | 62 | }
|
56 | 63 |
|
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}" |
64 | 67 |
|
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" |
70 | 72 |
|
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 | + ] |
75 | 78 | }
|
76 | 79 | }
|
| 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 | +} |
0 commit comments