Creating Worker Nodes
Learn how to create worker nodes and view them using Terraform.
We'll cover the following...
We'll cover the following...
Defining worker nodes
We can manage worker nodes through the aws_eks_node_group module. We use the following definition for this purpose.
Press + to interact
resource "aws_eks_node_group" "primary" {cluster_name = aws_eks_cluster.primary.nameversion = var.k8s_versionrelease_version = var.release_versionnode_group_name = "devops-catalog"node_role_arn = aws_iam_role.worker.arnsubnet_ids = aws_subnet.worker[*].idinstance_types = [var.machine_type]scaling_config {desired_size = var.min_node_countmax_size = var.max_node_countmin_size = var.min_node_count}depends_on = [aws_iam_role_policy_attachment.worker,aws_iam_role_policy_attachment.cni,aws_iam_role_policy_attachment.registry,]timeouts {create = "15m"update = "1h"}}resource "aws_iam_role" "worker" {name = "devops-catalog-worker"assume_role_policy = jsonencode({Statement = [{Action = "sts:AssumeRole"Effect = "Allow"Principal = {Service = "ec2.amazonaws.com"}}]Version = "2012-10-17"})}resource "aws_iam_role_policy_attachment" "worker" {policy_arn = "arn:aws:iam::aws:policy/AmazonEKSWorkerNodePolicy"role = aws_iam_role.worker.name}resource "aws_iam_role_policy_attachment" "cni" {policy_arn = "arn:aws:iam::aws:policy/AmazonEKS_CNI_Policy"role = aws_iam_role.worker.name}resource "aws_iam_role_policy_attachment" "registry" {policy_arn = "arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly"role = aws_iam_role.worker.name}resource "aws_internet_gateway" "worker" {vpc_id = aws_vpc.worker.idtags = {Name = "devops-catalog"}}resource "aws_route_table" "worker" {vpc_id = aws_vpc.worker.idroute {cidr_block = "0.0.0.0/0"gateway_id = aws_internet_gateway.worker.id}}resource "aws_route_table_association" "worker" {count = 3subnet_id = aws_subnet.worker[count.index].idroute_table_id = aws_route_table.worker.id}
Like with the control plane, we have to define a few additional resources besides the aws_eks_node_group
. Just like before, we won’t go into details, but only do a brief overview. We can see that the node group definition follows a similar pattern to the one we used for the control plane.
- Line 2: We have the cluster name (
cluster_name
) that references the name field of theaws_eks_cluster.primary
resource. - Lines 3–4: The version is the value of the
k8s_version
variable, and the AMI release version is provided through