<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: AnamsKen</title>
    <description>The latest articles on DEV Community by AnamsKen (@anamsken).</description>
    <link>https://dev.to/anamsken</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F443842%2F97fbc7cb-550c-4721-9100-3474d00b4c2f.jpeg</url>
      <title>DEV Community: AnamsKen</title>
      <link>https://dev.to/anamsken</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/anamsken"/>
    <language>en</language>
    <item>
      <title>Setting up an EKS cluster and worker nodes with Terraform</title>
      <dc:creator>AnamsKen</dc:creator>
      <pubDate>Sat, 08 Nov 2025 20:46:12 +0000</pubDate>
      <link>https://dev.to/anamsken/setting-up-an-eks-cluster-and-worker-nodes-with-terraform-5682</link>
      <guid>https://dev.to/anamsken/setting-up-an-eks-cluster-and-worker-nodes-with-terraform-5682</guid>
      <description>&lt;p&gt;Kubernetes can be difficult even for the most experienced DevOps engineers; that stuff is a crazy monster! We will explore how to set up an Elastic Kubernetes cluster (EKS) and connect worker nodes using Terraform. &lt;br&gt;
 &lt;br&gt;
 &lt;br&gt;
First things first: Configure your AWS command line interface. This will give you access to run CLI commands against your AWS account, and this configuration will also allow Terraform to leverage your account to spin up infrastructures in your account. &lt;br&gt;
 &lt;br&gt;
You can check out this documentation on how to do that. &lt;br&gt;
 &lt;br&gt;
Alright, let's jump in and write some code.&lt;br&gt;
Create 3 major files :&lt;/p&gt;

&lt;p&gt;&lt;code&gt;touch main.tf variable.tf&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In the main.tf, create the provider block and add the resource needed to create EKS cluster. As part of the requirement, we need to create the required role that EKS needs to interact with other AWS resources. We will call this eks-cluster-role&lt;/p&gt;
&lt;h1&gt;
  
  
  Setting up the terraform block
&lt;/h1&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~&amp;gt; 6.0"
    }
  }
}

# Configure the AWS Provider
provider "aws" {
  region = "us-east-1"
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Create the eks-cluster-role and attach the AmazonEKSClusterPolicy to this role&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;resource "aws_iam_role" "eks-cluster-role" {
  name = "eeks-cluster-role"
  assume_role_policy = jsonencode({
    Version = "2012-10-17"
    Statement = [
      {
        Action = [
          "sts:AssumeRole",
          "sts:TagSession"
        ]
        Effect = "Allow"
        Principal = {
          Service = "eks.amazonaws.com"
        }
      },
    ]
  })
}

#Attaching  policy to role

resource "aws_iam_role_policy_attachment" "cluster_AmazonEKSClusterPolicy" {
  policy_arn = "arn:aws:iam::aws:policy/AmazonEKSClusterPolicy"
  role       = aws_iam_role.eks-cluster-role.name
}

Create the EKS resource code
resource "aws_eks_cluster" "my-eks" {
  name = "my-eks"

  access_config {
    authentication_mode = "API"
  }

#Attaching the role we created to EKS
  role_arn = aws_iam_role.eks-cluster-role.arn
  version  = "1.31"

#Include your subnet ids here
  vpc_config {
    subnet_ids = [
      var.subnet1,
      var.subnet2,

    ]
  }

# Using API for authentication mode

access_config {

 authentication_mode = "API"
 bootstrap_cluster_creator_admin_permissions = true
}

#Setting this to true to enable core addons
bootstrap_self_managed_addons = true

  # Ensure that IAM Role permissions are created before and deleted

  depends_on = [
    aws_iam_role_policy_attachment.cluster_AmazonEKSClusterPolicy,
  ]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you observed, we enabled some configuration to true, such as &lt;code&gt;bootstrap_self_managed_addons&lt;/code&gt;. Setting this to true will install the core addons we need to run Kubernetes, such as&lt;code&gt;aws-cni&lt;/code&gt;, &lt;code&gt;kube-proxy&lt;/code&gt; and &lt;code&gt;CoreDns&lt;/code&gt;. Ignoring this will mean we have to make these installations manually, which is not the goal of automation. &lt;br&gt;
 &lt;br&gt;
 Now let's move to setting up worker nodes using node groups. As usual, we will start by creating the roles we need for the worker nodes&lt;/p&gt;

&lt;p&gt;We will be attaching three policies to this role;&lt;br&gt;
 &lt;code&gt;AmazonEKSWorkerNodePolicy&lt;/code&gt;, &lt;code&gt;AmazonEKS_CNI_Policy&lt;/code&gt; and &lt;code&gt;AmazonEC2ContainerRegistryReadOnly&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#Creating Node-group

resource "aws_iam_role" "node-group-role" {
  name = "eks-node-group"

  assume_role_policy = jsonencode({
    Statement = [{
      Action = "sts:AssumeRole"
      Effect = "Allow"
      Principal = {
        Service = "ec2.amazonaws.com"
      }
    }]
    Version = "2012-10-17"
  })
}

#Attaching needed policies to role

resource "aws_iam_role_policy_attachment" "node-AmazonEKSWorkerNodePolicy" {
  policy_arn = "arn:aws:iam::aws:policy/AmazonEKSWorkerNodePolicy"
  role       = aws_iam_role.node-group-role.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-group-role.name
}

resource "aws_iam_role_policy_attachment" "node-AmazonEC2ContainerRegistryReadOnly" {
  policy_arn = "arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly"
  role       = aws_iam_role.node-group-role.name
}

Now we can go ahead and create our Node group
resource "aws_eks_node_group" "node-group" {
  cluster_name    = aws_eks_cluster.my-eks.name
  node_group_name = "my-node-group"
  node_role_arn   = aws_iam_role.node-group-role.arn
  subnet_ids      = [var.subnet1, var.subnet2] #You can add more subnets
  scaling_config {
    desired_size = 1
    max_size     = 2
    min_size     = 1
  }

  update_config {
    max_unavailable = 1
  }

#If you need remote access to your node groups
remote_access {
 #want to add custom security groups
 source_security_group_ids = [var.security_group_id] 
 ec2_ssh_key = "kube-demo" #create this ssh key in the account

}

  # Ensure that IAM Role permissions are created before and deleted after EKS Node Group handling.
  # Otherwise, EKS will not be able to properly delete EC2 Instances and Elastic Network Interfaces.
  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,
  ]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we have our &lt;code&gt;main.tf&lt;/code&gt; file is complete, ensure the variables are created in the &lt;code&gt;variable.tf&lt;/code&gt; file and then proceed to run your &lt;code&gt;terraform apply&lt;/code&gt; command. This resource creation usually takes time, so get a cup of coffee and give it some minutes.&lt;/p&gt;

&lt;p&gt;Connect To Control Node&lt;/p&gt;

&lt;p&gt;Once your command is successful, it is now time to connect to your control plane node, using this command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;aws eks update-kubeconfig - name &amp;lt;cluster name&amp;gt; - region us-east-1

#get the node info
kubectl get nodes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You are good to go!&lt;/p&gt;

</description>
      <category>aws</category>
      <category>devops</category>
      <category>terraform</category>
      <category>kubernetes</category>
    </item>
  </channel>
</rss>
