This is how my directory is structured:
├── directory
├── vpc
│ └── main.tf
├── eks
└── main.tf
Inside vpc/main.tf
, I have:
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
name = "other-tools-vpc"
cidr = "10.0.0.0/16"
# inside 2 azs, create two private subnets and two public subnets each
azs = ["us-east-1a", "us-east-1b"]
private_subnets = ["10.0.1.0/24", "10.0.2.0/24"]
public_subnets = ["10.0.101.0/24", "10.0.102.0/24"]
// remaining code
}
Inside eks/main.tf
:
module "eks" {
source = "terraform-aws-modules/eks/aws"
version = "18.29.0"
cluster_name = "other-tools-eks"
cluster_version = "1.23"
cluster_endpoint_private_access = true
cluster_endpoint_public_access = true
vpc_id = module.vpc.vpc_id
subnet_ids = module.vpc.private_subnets
// remaining code
}
I want to assign my vpc created above to my eks cluster. I have tried the following above vpc_id = module.vpc.vpc_id
but it produces an error of:
Reference to undeclared module. No module call named "vpc"
is declared in the root module.
The error is correct. You can't reference variables or outputs across different modules. Instead you should have parent main.tf
folder:
├── directory
|-- parent_main.tf
├── vpc
│ └── main.tf
├── eks
└── main.tf
and in the parent_main.tf
you have to instantiate both modules:
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
name = "other-tools-vpc"
cidr = "10.0.0.0/16"
# inside 2 azs, create two private subnets and two public subnets each
azs = ["us-east-1a", "us-east-1b"]
private_subnets = ["10.0.1.0/24", "10.0.2.0/24"]
public_subnets = ["10.0.101.0/24", "10.0.102.0/24"]
// remaining code
}
module "eks" {
source = "terraform-aws-modules/eks/aws"
version = "18.29.0"
cluster_name = "other-tools-eks"
cluster_version = "1.23"
cluster_endpoint_private_access = true
cluster_endpoint_public_access = true
vpc_id = module.vpc.vpc_id
subnet_ids = module.vpc.private_subnets
// remaining code
}
Once you do this, vpc_id = module.vpc.vpc_id
will work.
alternatively
Deploy vpc
module first, and then deploy eks
module and pass in vpc_id
as an input variabile into eks/main.tf
:
variable "vpc_id" {}
variable "private_subnets" {}
module "eks" {
source = "terraform-aws-modules/eks/aws"
version = "18.29.0"
cluster_name = "other-tools-eks"
cluster_version = "1.23"
cluster_endpoint_private_access = true
cluster_endpoint_public_access = true
vpc_id = var.vpc_id
subnet_ids = var.private_subnets
// remaining code
}