Search code examples
terraformterraform-provider-awsamazon-eks

Create a EKS Cluster Using existing VPC


Trying to create a EKS cluster using Terraform using Gitlab using existing VPC which was created manually. Getting the below error. Please help.

creating EKS Cluster (example): InvalidParameterException: The subnet ID 'subnet- 0250558d209750998' does not exist (Service: AmazonEC2; Status Code: 400; Error Code: InvalidSubnetID.NotFound; Request ID: da43167e-7430-4f7a-85e5-3ea185468c0a; Proxy: null)

Cluster.tf is as bellow`

  resource "aws_eks_cluster" "example" {
    name     = "example"
    role_arn = aws_iam_role.example.arn

    vpc_config {
    subnet_ids = ["subnet-0250558d209750998","subnet-0aab7bc16ef569ef1"]
    }

    depends_on = [
    aws_iam_role_policy_attachment.example-AmazonEKSClusterPolicy,
    aws_iam_role_policy_attachment.example-AmazonEKSVPCResourceController,
    ]
    }

     output "endpoint" {
     value = aws_eks_cluster.example.endpoint
    }

     output "kubeconfig-certificate-authority-data" {
     value = aws_eks_cluster.example.certificate_authority[0].data
    }

     data "aws_iam_policy_document" "assume_role" {
     statement {
     effect = "Allow"

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

     actions = ["sts:AssumeRole"]
     }
    }

     resource "aws_iam_role" "example" {
     name               = "eks-cluster-example"
     assume_role_policy = data.aws_iam_policy_document.assume_role.json
    }

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

# Optionally, enable Security Groups for Pods
# Reference: https://docs.aws.amazon.com/eks/latest/userguide/security-groups-for-pods.html
resource "aws_iam_role_policy_attachment" "example-AmazonEKSVPCResourceController" {
  policy_arn = "arn:aws:iam::aws:policy/AmazonEKSVPCResourceController"
  role       = aws_iam_role.example.name
}


data "aws_eks_clusters" "example" {}


resource "aws_launch_template" "this" {
  name = "this"

  metadata_options {
    http_endpoint               = "enabled"
    http_tokens                 = "required"
    http_put_response_hop_limit = 1
    instance_metadata_tags      = "enabled"
  }
}


resource "aws_eks_node_group" "example" {
  cluster_name    = aws_eks_cluster.example.name
  node_group_name = "example"
  node_role_arn   = aws_iam_role.example2.arn
  ami_type = "AL2_x86_64"
  subnet_ids = ["subnet-0250558d209750998","subnet-0aab7bc16ef569ef1"]

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

  update_config {
    max_unavailable = 1
  }
  
  launch_template {
    version = aws_launch_template.this.latest_version
    id      = aws_launch_template.this.id
  }
  
}

resource "aws_iam_role" "example2" {
  name = "eks-node-group-example"

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

resource "aws_iam_role_policy_attachment" "example-AmazonEKS_CNI_Policy" {
  policy_arn = "arn:aws:iam::aws:policy/AmazonEKS_CNI_Policy"
  role       = aws_iam_role.example.name
}

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

Solution

  • If you are sure of the existence of the subnets, then the problem might be caused by the fact that you are running the terraform script in a different region than the one that is hosting the two subnets. I would check your provider configuration.