Packer amazon-ebs subnet/VPC required in a non-default VPC in CI
When an account has no default VPC, or you target a non-default VPC, EC2 cannot pick a subnet automatically. Packer must be given a subnet_id, or the launch fails.
What this error means
packer build fails with "VPCResourceNotSpecified: The specified instance type can only be used in a VPC. A subnet ID or network interface ID is required" or "VPCIdNotSpecified: No default VPC".
Error: Error launching source instance: VPCResourceNotSpecified: The specified
instance type can only be used in a VPC. A subnet ID or network interface ID is
required to carry out the request.
status code: 400, request id: ...Common causes
The account has no default VPC
Without a default VPC, EC2 cannot auto-place the instance, so a subnet id must be provided.
A non-default VPC without a chosen subnet
Targeting a specific VPC requires naming the subnet inside it, otherwise the launch is ambiguous.
How to fix it
Set an explicit subnet_id
- Pick a subnet in the VPC you want to build in.
- Set
subnet_id(andvpc_idif needed) on the builder. - Re-run the build so EC2 has a placement.
source "amazon-ebs" "ubuntu" {
vpc_id = "vpc-0abc123"
subnet_id = "subnet-0def456"
}Select a subnet with a filter
Use a subnet_filter so the builder picks a matching subnet without a hardcoded id.
subnet_filter {
filters = { "tag:Name" = "build-*" }
most_free = true
random = false
}How to prevent it
- Set
subnet_idorsubnet_filterfor non-default VPCs. - Standardize a build subnet and reference it in templates.
- Confirm the VPC has a route to reach the instance for provisioning.