Skip to content
Latchkey

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".

packer
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

  1. Pick a subnet in the VPC you want to build in.
  2. Set subnet_id (and vpc_id if needed) on the builder.
  3. Re-run the build so EC2 has a placement.
build.pkr.hcl
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.

build.pkr.hcl
subnet_filter {
  filters   = { "tag:Name" = "build-*" }
  most_free = true
  random    = false
}

How to prevent it

  • Set subnet_id or subnet_filter for non-default VPCs.
  • Standardize a build subnet and reference it in templates.
  • Confirm the VPC has a route to reach the instance for provisioning.

Frequently asked questions

What causes ""VPCResourceNotSpecified""?
Without a default VPC, EC2 cannot auto-place the instance, so a subnet id must be provided.
How do I fix "VPCResourceNotSpecified"?
Set an explicit subnet_id

Related guides

References

Run this faster and cheaper on Latchkey managed runners - self-healing included. Start free → 30-day trial · No credit card