Last active
June 7, 2022 11:39
-
-
Save a-patel/6d6c3a867371ba68968758e129cd93b0 to your computer and use it in GitHub Desktop.
Amazon EKS Terraform Workshop: Worker Nodes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# EKS Node Groups | |
resource "aws_eks_node_group" "this" { | |
cluster_name = aws_eks_cluster.this.name | |
node_group_name = var.project | |
node_role_arn = aws_iam_role.node.arn | |
subnet_ids = aws_subnet.private[*].id | |
scaling_config { | |
desired_size = 2 | |
max_size = 5 | |
min_size = 1 | |
} | |
ami_type = "AL2_x86_64" # AL2_x86_64, AL2_x86_64_GPU, AL2_ARM_64, CUSTOM | |
capacity_type = "ON_DEMAND" # ON_DEMAND, SPOT | |
disk_size = 20 | |
instance_types = ["t2.medium"] | |
tags = merge( | |
var.tags | |
) | |
depends_on = [ | |
aws_iam_role_policy_attachment.node_AmazonEKSWorkerNodePolicy, | |
aws_iam_role_policy_attachment.node_AmazonEKS_CNI_Policy, | |
aws_iam_role_policy_attachment.node_AmazonEC2ContainerRegistryReadOnly, | |
] | |
} | |
# EKS Node IAM Role | |
resource "aws_iam_role" "node" { | |
name = "${var.project}-Worker-Role" | |
assume_role_policy = <<POLICY | |
{ | |
"Version": "2012-10-17", | |
"Statement": [ | |
{ | |
"Effect": "Allow", | |
"Principal": { | |
"Service": "ec2.amazonaws.com" | |
}, | |
"Action": "sts:AssumeRole" | |
} | |
] | |
} | |
POLICY | |
} | |
resource "aws_iam_role_policy_attachment" "node_AmazonEKSWorkerNodePolicy" { | |
policy_arn = "arn:aws:iam::aws:policy/AmazonEKSWorkerNodePolicy" | |
role = aws_iam_role.node.name | |
} | |
resource "aws_iam_role_policy_attachment" "node_AmazonEKS_CNI_Policy" { | |
policy_arn = "arn:aws:iam::aws:policy/AmazonEKS_CNI_Policy" | |
role = aws_iam_role.node.name | |
} | |
resource "aws_iam_role_policy_attachment" "node_AmazonEC2ContainerRegistryReadOnly" { | |
policy_arn = "arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly" | |
role = aws_iam_role.node.name | |
} | |
# EKS Node Security Group | |
resource "aws_security_group" "eks_nodes" { | |
name = "${var.project}-node-sg" | |
description = "Security group for all nodes in the cluster" | |
vpc_id = aws_vpc.this.id | |
egress { | |
from_port = 0 | |
to_port = 0 | |
protocol = "-1" | |
cidr_blocks = ["0.0.0.0/0"] | |
} | |
tags = { | |
Name = "${var.project}-node-sg" | |
"kubernetes.io/cluster/${var.project}-cluster" = "owned" | |
} | |
} | |
resource "aws_security_group_rule" "nodes_internal" { | |
description = "Allow nodes to communicate with each other" | |
from_port = 0 | |
protocol = "-1" | |
security_group_id = aws_security_group.eks_nodes.id | |
source_security_group_id = aws_security_group.eks_nodes.id | |
to_port = 65535 | |
type = "ingress" | |
} | |
resource "aws_security_group_rule" "nodes_cluster_inbound" { | |
description = "Allow worker Kubelets and pods to receive communication from the cluster control plane" | |
from_port = 1025 | |
protocol = "tcp" | |
security_group_id = aws_security_group.eks_nodes.id | |
source_security_group_id = aws_security_group.eks_cluster.id | |
to_port = 65535 | |
type = "ingress" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment