resource "aws_iam_role" "nodes" {
    name = "eks-node-group-nodes"
  
    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" "nodes-AmazonEKSWorkerNodePolicy" {
    policy_arn = "arn:aws:iam::aws:policy/AmazonEKSWorkerNodePolicy"
    role       = aws_iam_role.nodes.name
  }
  
  resource "aws_iam_role_policy_attachment" "nodes-AmazonEKS_CNI_Policy" {
    policy_arn = "arn:aws:iam::aws:policy/AmazonEKS_CNI_Policy"
    role       = aws_iam_role.nodes.name
  }
  
  resource "aws_iam_role_policy_attachment" "nodes-AmazonEC2ContainerRegistryReadOnly" {
    policy_arn = "arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly"
    role       = aws_iam_role.nodes.name
  }
  
  resource "aws_eks_node_group" "private-nodes" {
    cluster_name    = aws_eks_cluster.{{public_cluster.name}}.name
    node_group_name = "private-nodes"
    node_role_arn   = aws_iam_role.nodes.arn
  
    subnet_ids = [
    {%if public_cluster.net_config == True %}
      aws_subnet.private-us-east-1a.id,
      aws_subnet.private-us-east-1b.id
    {%else%}
      {%for subnet in subnets%}
      "???"{%if not loop.last%},{%endif%}
      {%endfor%}
      {%endif%}
    ]
  
    capacity_type  = "ON_DEMAND"
    instance_types = ["t3.small"]
  
    scaling_config {
      desired_size = 1
      max_size     = 5
      min_size     = 0
    }
  
    update_config {
      max_unavailable = 1
    }
  
    labels = {
      role = "general"
    }
  
  
    depends_on = [
      aws_iam_role_policy_attachment.nodes-AmazonEKSWorkerNodePolicy,
      aws_iam_role_policy_attachment.nodes-AmazonEKS_CNI_Policy,
      aws_iam_role_policy_attachment.nodes-AmazonEC2ContainerRegistryReadOnly,
    ]
  }

output "load_balancer_ip" {
  value = kubernetes_service_v1.default-1.status.0.load_balancer.0.ingress.0.hostname
}