I’ve got the following HCL:
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "2.66.0"
name = "eks_demo_vpc"
cidr = "10.0.0.0/16"
azs = data.aws_availability_zones.available.names
private_subnets = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]
public_subnets = ["10.0.4.0/24", "10.0.5.0/24", "10.0.6.0/24"]
enable_nat_gateway = true
single_nat_gateway = true
enable_dns_hostnames = true
tags = {
"kubernetes.io/cluster/${local.cluster_name}" = "shared"
"Name" = "eks_demo_vpc"
}
public_subnet_tags = {
"kubernetes.io/cluster/${local.cluster_name}" = "shared"
"kubernetes.io/role/elb" = "1"
}
private_subnet_tags = {
"kubernetes.io/cluster/${local.cluster_name}" = "shared"
"kubernetes.io/role/internal-elb" = "1"
}
}
data "aws_vpc" "selected" {
filter {
name = "tag:Name"
values = ["eks_demo_vpc"]
}
}
data "aws_subnet_ids" "selected" {
vpc_id = "${data.aws_vpc.selected.id}"
}
The aim is to create a data source containing the subnets for an AWS EKS node group resource:
resource "aws_eks_node_group" "eks_demo_node_group" {
for_each = data.aws_subnet_ids.selected.ids
cluster_name = local.cluster_name
node_group_name = local.node_group_name
node_role_arn = aws_iam_role.eks_demo.arn
subnet_ids = each.value
scaling_config {
desired_size = 3
max_size = 3
min_size = 3
}
depends_on = [
aws_iam_role_policy_attachment.eks_demo_AmazonEKSWorkerNodePolicy,
aws_iam_role_policy_attachment.eks_demo_AmazonEKS_CNI_Policy,
aws_iam_role_policy_attachment.eks_demo_AmazonEC2ContainerRegistryReadOnly,
aws_iam_role_policy_attachment.eks_demo_ElasticLoadBalancingFullAccess,
aws_iam_role_policy_attachment.eks_demo_EKSPortworxEC2mgmt,
aws_iam_role_policy_attachment.eks_demo_EKSPXBackupPermissions,
]
}
When I issue terraform plan, I get no matching VPC found:
╷
│ Error: no matching VPC found
│
│ with data.aws_vpc.selected,
│ on vpc.tf line 54, in data “aws_vpc” “selected”:
│ 54: data “aws_vpc” “selected” {
│
╵
I’m speculating that the VPC needs to exist before terraform plan is issued, can someone point me in the right direction in order to get this to work.