<h2>Background and Context</h2>
<p>I am working on a Terraform project that has an end goal of an EKS cluster with the following properties:</p>
<ol>
<li>Private to the outside internet</li>
<li>Accessible via a bastion host</li>
<li>Uses worker groups</li>
<li>Resources (deployments, cron jobs, etc) configurable via the Terraform Kubernetes module</li>
</ol>
<p>To accomplish this, I've modified the Terraform EKS example slightly (code at bottom of the question). The problems that I am encountering is that after SSH-ing into the bastion, I cannot ping the cluster and any commands like <code>kubectl get pods</code> timeout after about 60 seconds.</p>
<p>Here are the facts/things I know to be true:</p>
<ol>
<li>I have (for the time being) switched the cluster to a public cluster for testing purposes. Previously when I had <code>cluster_endpoint_public_access</code> set to <code>false</code> the <code>terraform apply</code> command would not even complete as it could not access the <code>/healthz</code> endpoint on the cluster.</li>
<li>The Bastion configuration works in the sense that the user data runs successfully and installs <code>kubectl</code> and the kubeconfig file</li>
<li>I am able to SSH into the bastion via my static IP (that's the <code>var.company_vpn_ips</code> in the code)</li>
<li>It's entirely possible this is fully a networking problem and not an EKS/Terraform problem as my understanding of how the VPC and its security groups fit into this picture is not entirely mature.</li>
</ol>
<h2>Code</h2>
<p>Here is the VPC configuration:</p>
<pre><code>locals {
  vpc_name            = &quot;my-vpc&quot;
  vpc_cidr            = &quot;10.0.0.0/16&quot;
  public_subnet_cidr  = [&quot;10.0.4.0/24&quot;, &quot;10.0.5.0/24&quot;, &quot;10.0.6.0/24&quot;]
  private_subnet_cidr = [&quot;10.0.1.0/24&quot;, &quot;10.0.2.0/24&quot;, &quot;10.0.3.0/24&quot;]
}

# The definition of the VPC to create

module &quot;vpc&quot; {

  source  = &quot;terraform-aws-modules/vpc/aws&quot;
  version = &quot;3.2.0&quot;

  name                 = local.vpc_name
  cidr                 = local.vpc_cidr
  azs                  = data.aws_availability_zones.available.names
  private_subnets      = local.private_subnet_cidr
  public_subnets       = local.public_subnet_cidr
  enable_nat_gateway   = true
  single_nat_gateway   = true
  enable_dns_hostnames = true

  tags = {
    &quot;kubernetes.io/cluster/${var.cluster_name}&quot; = &quot;shared&quot;
  }

  public_subnet_tags = {
    &quot;kubernetes.io/cluster/${var.cluster_name}&quot; = &quot;shared&quot;
    &quot;kubernetes.io/role/elb&quot;                    = &quot;1&quot;
  }

  private_subnet_tags = {
    &quot;kubernetes.io/cluster/${var.cluster_name}&quot; = &quot;shared&quot;
    &quot;kubernetes.io/role/internal-elb&quot;           = &quot;1&quot;
  }
}

data &quot;aws_availability_zones&quot; &quot;available&quot; {}
</code></pre>
<p>Then the security groups I create for the cluster:</p>
<pre><code>resource &quot;aws_security_group&quot; &quot;ssh_sg&quot; {
  name_prefix = &quot;ssh-sg&quot;
  vpc_id      = module.vpc.vpc_id

  ingress {
    from_port = 22
    to_port   = 22
    protocol  = &quot;tcp&quot;

    cidr_blocks = [
      &quot;10.0.0.0/8&quot;,
    ]
  }
}

resource &quot;aws_security_group&quot; &quot;all_worker_mgmt&quot; {
  name_prefix = &quot;all_worker_management&quot;
  vpc_id      = module.vpc.vpc_id

  ingress {
    from_port = 22
    to_port   = 22
    protocol  = &quot;tcp&quot;

    cidr_blocks = [
      &quot;10.0.0.0/8&quot;,
      &quot;172.16.0.0/12&quot;,
      &quot;192.168.0.0/16&quot;,
    ]
  }
}
</code></pre>
<p>Here's the cluster configuration:</p>
<pre><code>locals {
  cluster_version = &quot;1.21&quot;
}

# Create the EKS resource that will setup the EKS cluster
module &quot;eks_cluster&quot; {
  source = &quot;terraform-aws-modules/eks/aws&quot;

  # The name of the cluster to create
  cluster_name = var.cluster_name

  # Disable public access to the cluster API endpoint
  cluster_endpoint_public_access = true

  # Enable private access to the cluster API endpoint
  cluster_endpoint_private_access = true

  # The version of the cluster to create
  cluster_version = local.cluster_version

  # The VPC ID to create the cluster in
  vpc_id = var.vpc_id

  # The subnets to add the cluster to
  subnets = var.private_subnets

  # Default information on the workers
  workers_group_defaults = {
    root_volume_type = &quot;gp2&quot;
  }

  worker_additional_security_group_ids = [var.all_worker_mgmt_id]

  # Specify the worker groups
  worker_groups = [
    {
      # The name of this worker group
      name = &quot;default-workers&quot;
      # The instance type for this worker group
      instance_type = var.eks_worker_instance_type
      # The number of instances to raise up
      asg_desired_capacity = var.eks_num_workers
      asg_max_size         = var.eks_num_workers
      asg_min_size         = var.eks_num_workers
      # The security group IDs for these instances
      additional_security_group_ids = [var.ssh_sg_id]
    }
  ]
}

data &quot;aws_eks_cluster&quot; &quot;cluster&quot; {
  name = module.eks_cluster.cluster_id
}

data &quot;aws_eks_cluster_auth&quot; &quot;cluster&quot; {
  name = module.eks_cluster.cluster_id
}

output &quot;worker_iam_role_name&quot; {
  value = module.eks_cluster.worker_iam_role_name
}
</code></pre>
<p>And the finally the bastion:</p>
<pre><code>locals {
  ami           = &quot;ami-0f19d220602031aed&quot; # Amazon Linux 2 AMI (us-east-2)
  instance_type = &quot;t3.small&quot;
  key_name      = &quot;bastion-kp&quot;
}

resource &quot;aws_iam_instance_profile&quot; &quot;bastion&quot; {
  name = &quot;bastion&quot;
  role = var.role_name
}

resource &quot;aws_instance&quot; &quot;bastion&quot; {
  ami           = local.ami
  instance_type = local.instance_type

  key_name                    = local.key_name
  associate_public_ip_address = true
  subnet_id                   = var.public_subnet
  iam_instance_profile        = aws_iam_instance_profile.bastion.name

  security_groups = [aws_security_group.bastion-sg.id]

  tags = {
    Name = &quot;K8s Bastion&quot;
  }

  lifecycle {
    ignore_changes = all
  }

  user_data = &lt;&lt;EOF
      #! /bin/bash

      # Install Kubectl
      curl -LO &quot;https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl&quot;
      install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
      kubectl version --client

      # Install Helm
      curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3
      chmod 700 get_helm.sh
      ./get_helm.sh
      helm version

      # Install AWS
      curl &quot;https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip&quot; -o &quot;awscliv2.zip&quot;
      unzip awscliv2.zip
      ./aws/install
      aws --version

      # Install aws-iam-authenticator
      curl -o aws-iam-authenticator https://amazon-eks.s3.us-west-2.amazonaws.com/1.21.2/2021-07-05/bin/linux/amd64/aws-iam-authenticator
      chmod +x ./aws-iam-authenticator
      mkdir -p $HOME/bin &amp;&amp; cp ./aws-iam-authenticator $HOME/bin/aws-iam-authenticator &amp;&amp; export PATH=$PATH:$HOME/bin
      echo 'export PATH=$PATH:$HOME/bin' &gt;&gt; ~/.bashrc
      aws-iam-authenticator help

      # Add the kube config file 
      mkdir ~/.kube
      echo &quot;${var.kubectl_config}&quot; &gt;&gt; ~/.kube/config
  EOF
}

resource &quot;aws_security_group&quot; &quot;bastion-sg&quot; {
  name   = &quot;bastion-sg&quot;
  vpc_id = var.vpc_id
}

resource &quot;aws_security_group_rule&quot; &quot;sg-rule-ssh&quot; {
  security_group_id = aws_security_group.bastion-sg.id
  from_port         = 22
  protocol          = &quot;tcp&quot;
  to_port           = 22
  type              = &quot;ingress&quot;
  cidr_blocks       = var.company_vpn_ips
  depends_on        = [aws_security_group.bastion-sg]
}

resource &quot;aws_security_group_rule&quot; &quot;sg-rule-egress&quot; {
  security_group_id = aws_security_group.bastion-sg.id
  type              = &quot;egress&quot;
  from_port         = 0
  protocol          = &quot;all&quot;
  to_port           = 0
  cidr_blocks       = [&quot;0.0.0.0/0&quot;]
  ipv6_cidr_blocks  = [&quot;::/0&quot;]
  depends_on        = [aws_security_group.bastion-sg]
}
</code></pre>
<h2>Ask</h2>
<p>The most pressing issue for me is finding a way to interact with the cluster via the bastion so that the other part of the Terraform code can run (the resources to spin up in the cluster itself). I am also hoping to understand how to setup a private cluster when it ends up being inaccessible to the <code>terraform apply</code> command. Thank you in advance for any help you can provide!</p>

Jimmy McDermott
<p>See how your node group is communicate with the control plane, you need to add the same cluster security group to your bastion host in order for it to communicate with the control plane. You can find the SG id on the EKS console - Networking tab.</p>
