|
| 1 | +# |
| 2 | +# EKS Worker Nodes Resources |
| 3 | +# * IAM role allowing Kubernetes actions to access other AWS services |
| 4 | +# * EC2 Security Group to allow networking traffic |
| 5 | +# * Data source to fetch latest EKS worker AMI |
| 6 | +# * AutoScaling Launch Configuration to configure worker instances |
| 7 | +# * AutoScaling Group to launch worker instances |
| 8 | +# |
| 9 | + |
| 10 | +resource "aws_iam_role" "demo-node" { |
| 11 | + name = "terraform-eks-demo-node" |
| 12 | + |
| 13 | + assume_role_policy = <<POLICY |
| 14 | +{ |
| 15 | + "Version": "2012-10-17", |
| 16 | + "Statement": [ |
| 17 | + { |
| 18 | + "Effect": "Allow", |
| 19 | + "Principal": { |
| 20 | + "Service": "ec2.amazonaws.com" |
| 21 | + }, |
| 22 | + "Action": "sts:AssumeRole" |
| 23 | + } |
| 24 | + ] |
| 25 | +} |
| 26 | +POLICY |
| 27 | +} |
| 28 | + |
| 29 | +resource "aws_iam_role_policy_attachment" "demo-node-AmazonEKSWorkerNodePolicy" { |
| 30 | + policy_arn = "arn:aws:iam::aws:policy/AmazonEKSWorkerNodePolicy" |
| 31 | + role = "${aws_iam_role.demo-node.name}" |
| 32 | +} |
| 33 | + |
| 34 | +resource "aws_iam_role_policy_attachment" "demo-node-AmazonEKS_CNI_Policy" { |
| 35 | + policy_arn = "arn:aws:iam::aws:policy/AmazonEKS_CNI_Policy" |
| 36 | + role = "${aws_iam_role.demo-node.name}" |
| 37 | +} |
| 38 | + |
| 39 | +resource "aws_iam_role_policy_attachment" "demo-node-AmazonEC2ContainerRegistryReadOnly" { |
| 40 | + policy_arn = "arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly" |
| 41 | + role = "${aws_iam_role.demo-node.name}" |
| 42 | +} |
| 43 | + |
| 44 | +resource "aws_iam_instance_profile" "demo-node" { |
| 45 | + name = "terraform-eks-demo" |
| 46 | + role = "${aws_iam_role.demo-node.name}" |
| 47 | +} |
| 48 | + |
| 49 | +resource "aws_security_group" "demo-node" { |
| 50 | + name = "terraform-eks-demo-node" |
| 51 | + description = "Security group for all nodes in the cluster" |
| 52 | + vpc_id = "${aws_vpc.demo.id}" |
| 53 | + |
| 54 | + egress { |
| 55 | + from_port = 0 |
| 56 | + to_port = 0 |
| 57 | + protocol = "-1" |
| 58 | + cidr_blocks = ["0.0.0.0/0"] |
| 59 | + } |
| 60 | + |
| 61 | + tags = "${ |
| 62 | + map( |
| 63 | + "Name", "terraform-eks-demo-node", |
| 64 | + "kubernetes.io/cluster/${var.cluster-name}", "owned", |
| 65 | + ) |
| 66 | + }" |
| 67 | +} |
| 68 | + |
| 69 | +resource "aws_security_group_rule" "demo-node-ingress-self" { |
| 70 | + description = "Allow node to communicate with each other" |
| 71 | + from_port = 0 |
| 72 | + protocol = "-1" |
| 73 | + security_group_id = "${aws_security_group.demo-node.id}" |
| 74 | + source_security_group_id = "${aws_security_group.demo-node.id}" |
| 75 | + to_port = 65535 |
| 76 | + type = "ingress" |
| 77 | +} |
| 78 | + |
| 79 | +resource "aws_security_group_rule" "demo-node-ingress-cluster" { |
| 80 | + description = "Allow worker Kubelets and pods to receive communication from the cluster control plane" |
| 81 | + from_port = 1025 |
| 82 | + protocol = "tcp" |
| 83 | + security_group_id = "${aws_security_group.demo-node.id}" |
| 84 | + source_security_group_id = "${aws_security_group.demo-cluster.id}" |
| 85 | + to_port = 65535 |
| 86 | + type = "ingress" |
| 87 | +} |
| 88 | + |
| 89 | +data "aws_ami" "eks-worker" { |
| 90 | + filter { |
| 91 | + name = "name" |
| 92 | + values = ["amazon-eks-node-${aws_eks_cluster.demo.version}-v*"] |
| 93 | + } |
| 94 | + |
| 95 | + most_recent = true |
| 96 | + owners = ["602401143452"] # Amazon EKS AMI Account ID |
| 97 | +} |
| 98 | + |
| 99 | +# EKS currently documents this required userdata for EKS worker nodes to |
| 100 | +# properly configure Kubernetes applications on the EC2 instance. |
| 101 | +# We utilize a Terraform local here to simplify Base64 encoding this |
| 102 | +# information into the AutoScaling Launch Configuration. |
| 103 | +# More information: https://docs.aws.amazon.com/eks/latest/userguide/launch-workers.html |
| 104 | +locals { |
| 105 | + demo-node-userdata = <<USERDATA |
| 106 | +#!/bin/bash |
| 107 | +set -o xtrace |
| 108 | +/etc/eks/bootstrap.sh --apiserver-endpoint '${aws_eks_cluster.demo.endpoint}' --b64-cluster-ca '${aws_eks_cluster.demo.certificate_authority.0.data}' '${var.cluster-name}' |
| 109 | +USERDATA |
| 110 | +} |
| 111 | + |
| 112 | +resource "aws_launch_configuration" "demo" { |
| 113 | + associate_public_ip_address = true |
| 114 | + iam_instance_profile = "${aws_iam_instance_profile.demo-node.name}" |
| 115 | + image_id = "${data.aws_ami.eks-worker.id}" |
| 116 | + instance_type = "m4.large" |
| 117 | + name_prefix = "terraform-eks-demo" |
| 118 | + security_groups = ["${aws_security_group.demo-node.id}"] |
| 119 | + user_data_base64 = "${base64encode(local.demo-node-userdata)}" |
| 120 | + |
| 121 | + lifecycle { |
| 122 | + create_before_destroy = true |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +resource "aws_autoscaling_group" "demo" { |
| 127 | + desired_capacity = 2 |
| 128 | + launch_configuration = "${aws_launch_configuration.demo.id}" |
| 129 | + max_size = 2 |
| 130 | + min_size = 1 |
| 131 | + name = "terraform-eks-demo" |
| 132 | + vpc_zone_identifier = ["${aws_subnet.demo-0.id}","${aws_subnet.demo-1.id}"] |
| 133 | + |
| 134 | + tag { |
| 135 | + key = "Name" |
| 136 | + value = "terraform-eks-demo" |
| 137 | + propagate_at_launch = true |
| 138 | + } |
| 139 | + |
| 140 | + tag { |
| 141 | + key = "kubernetes.io/cluster/${var.cluster-name}" |
| 142 | + value = "owned" |
| 143 | + propagate_at_launch = true |
| 144 | + } |
| 145 | +} |
0 commit comments