Bootstrap FreeKB - Amazon Web Services (AWS) Elastic Kubernetes Service (EKS) - Create Node Groups using Terraform
Amazon Web Services (AWS) Elastic Kubernetes Service (EKS) - Create Node Groups using Terraform


This assumes you have setup Terraform as described in Amazon Web Services (AWS) - Getting Started with Terraform.

A Node Group contains one or more modes, which is often an EC2 Instance.

Let's say you have the following files on your Terraform server.

├── required_providers.tf
├── elastic_kubernetes_service (directory)
│   ├── iam.tf
│   ├── node_groups.tf
│   ├── provider.tf

 

required_providers.tf will almost always have this.

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
    }
  }
}

 

Let's say provider.tf has the following. In this example, the "default" profile in /home/username/.aws/config and /home/username/.aws/credentials is being used. This assumes you have setup Terraform as described in Amazon Web Services (AWS) - Getting Started with Terraform.

provider "aws" {
  alias   = "default"
  profile = "default"
  region  = "default"
}

 

And iam.tf could have the following.

resource "aws_iam_role" "node_group_role" {
  name               = "node_group_role"
  assume_role_policy = data.aws_iam_policy_document.eks_node_group_assume_role_policy.json

  tags = {
    Name = "node_group_role"
  }
}


data "aws_iam_policy_document" "eks_node_group_assume_role_policy" {
  statement {
    effect  = "Allow"
    actions = ["sts:AssumeRole"]

    principals {
      type        = "Service"
      identifiers = ["ec2.amazonaws.com"]
    }
  }
}

 

And node_groups.tf could have the following.

resource "aws_eks_node_group" "node_group" {
  cluster_name    = "my-cluster"
  node_group_name = "my-node-group"
  node_role_arn   = aws_iam_role.node_group_role.arn
  subnet_ids      = ["subnet-111222333", "subnet-222333444"]

  capacity_type  = "ON_DEMAND"
  instance_types = ["t3.medium"]

  scaling_config {
    desired_size = 2
    max_size     = 3
    min_size     = 1
  }

  tags = {
    environment = "staging"
  }
}

 

The terraform plan command can be used to see what Terraform will try to do.

terraform plan

 

The terraform apply command can be used to create or update EKS Node Group.

terraform apply -auto-approve

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 1996bc in the box below so that we can be sure you are a human.