The error message:
InvalidSubnetID.NotFound: The subnet ID 'aws_subnet.public.id' does not exist
is because your module input for subnet_id_value is the literal string "aws_subnet.public.id" rather than the actual subnet ID value.
Explanation
In your root module main.tf, you have:
module "ec2_instance" {
source = "/home/vagrant/terraform/module/ec2-instance"
ami_value = "ami-0c55b159cbfafe1f0"
instance_type_value = "t2.micro"
subnet_id_value = "aws_subnet.public.id"
}
Here the value passed to subnet_id_value is the string "aws_subnet.public.id", which Terraform treats literally, not as a reference.
aws_subnet.public.id is a resource reference inside the module, but your root module does not have access to that resource since it is declared in the module itself.
How to fix this?
You have two ways:
1. Pass the subnet ID from root module if subnet is created outside the module
- Create subnet in root module or somewhere else
- Then pass subnet ID properly as:
subnet_id_value = aws_subnet.public.id
But since your root module is not creating a subnet, it doesn't have the resource aws_subnet.public to reference.
2. Remove passing subnet_id_value from root module, and use the subnet created inside the module
Since the subnet is created inside the module at:
resource "aws_subnet" "public" {
# ...
}
and you want the instance inside the same subnet:
- Remove the variable
subnet_id_value and just refer to aws_subnet.public.id directly inside the module's EC2 resource.
OR
- Pass the subnet ID output from the module itself. That means your module creates subnet and outputs
subnet_id, so you cannot pass the subnet ID from the root into the module (because it's created inside the module).
Suggested fixes:
Option A: Change the module code (main.tf inside the module) to not use the variable for subnet ID but rather use the created subnet directly.
In /home/vagrant/terraform/module/ec2-instance/main.tf:
resource "aws_instance" "example" {
ami = var.ami_value
instance_type = var.instance_type_value
subnet_id = aws_subnet.public.id # use subnet created inside module
}
Remove the variable subnet_id_value from variables:
# remove variable "subnet_id_value" {...}
In your root module main.tf:
module "ec2_instance" {
source = "/home/vagrant/terraform/module/ec2-instance"
ami_value = "ami-0c55b159cbfafe1f0"
instance_type_value = "t2.micro"
# Remove subnet_id_value argument from module block
}
Option B: Create the subnet outside module and pass subnet ID into the module
Root module main.tf:
resource "aws_subnet" "public" {
vpc_id = aws_vpc.main.id # You need to create VPC in root or import VPC id
cidr_block = "10.0.1.0/24"
availability_zone = "us-east-2a"
}
module "ec2_instance" {
source = "/home/vagrant/terraform/module/ec2-instance"
ami_value = "ami-0c55b159cbfafe1f0"
instance_type_value = "t2.micro"
subnet_id_value = aws_subnet.public.id
}
And inside the module, leave the resource for subnet out (remove it), expect subnet as module input variable.
Why is your current approach wrong?
You are:
- Creating subnet inside module, but
- Trying to pass subnet ID as an input variable from outside, using
"aws_subnet.public.id" string which does not exist in root.
Terraform does not dynamically evaluate strings as interpolations; you must pass the actual values or references.
Summary
- Fix the reference inside the root module: Do NOT use
"aws_subnet.public.id" as a string literal.
- Create subnet in root and pass actual resource ID as
subnet_id_value.
- Or create subnet inside the module and have EC2 instance resource use the subnet resource directly, removing the variable for subnet ID.
Final working example (Option A):
module/ec2-instance/main.tf:
provider "aws" {
region = "us-east-2"
}
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
}
resource "aws_subnet" "public" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.1.0/24"
availability_zone = "us-east-2a"
}
resource "aws_instance" "example" {
ami = var.ami_value
instance_type = var.instance_type_value
subnet_id = aws_subnet.public.id
}
output "subnet_id" {
value = aws_subnet.public.id
}
output "public_ip" {
value = aws_instance.example.public_ip
}
module/ec2-instance/variable.tf:
variable "ami_value" {
description = "value of ami"
type = string
}
variable "instance_type_value" {
description = "value of instance type"
type = string
}
# remove subnet_id_value variable entirely
root main.tf:
provider "aws" {
region = "us-east-2"
}
module "ec2_instance" {
source = "/home/vagrant/terraform/module/ec2-instance"
ami_value = "ami-0c55b159cbfafe1f0"
instance_type_value = "t2.micro"
# do not pass subnet_id_value here
}
This will:
- Create VPC → subnet → instance in proper order.
- Use the subnet created inside the module for the EC2 instance.
- No invalid subnet ID errors.
If you want me to help you refactor for the other option or provide a full working example, just ask!