fix(ec2): reject a duplicate route destination with RouteAlreadyExists (#2546)

CreateRoute accepted the same destination twice and persisted both copies,
for all three destination kinds. AWS rejects the second with
RouteAlreadyExists.

A destination identifies a route, so duplicates leave the two operations
that match on one disagreeing about which copy they mean: ReplaceRoute
updates only the first, so the stale copy survives the replace, and
DeleteRoute removes every copy, so deleting one of two loses both. A table
holding duplicates therefore has no defined behaviour for either verb,
which is what the check restores rather than only tidying the table.

The check reuses matchesDestination and destinationLabel, already used by
replaceRoute and deleteRoute, so all three verbs agree on what "the same
destination" means. requireExactlyOneDestination runs first, so exactly one
destination kind is set and no cross-kind match is possible.

The local route seeded by CreateRouteTable is covered too, and deliberately:
AWS reports the same error for it, which is the RouteAlreadyExists that
Terraform surfaces when a configuration tries to route over the VPC CIDR.

Follows the RouteAlreadyExists precedent already in this file for transit
gateway routes. Filed as #2524.
This commit is contained in:
Allen Sanborn authored and GitHub committed 2026-08-26 19:17:37 -05:00
1 parent 009ac0b4a6
commit 1be97c9333
2 files changed
+230

No files matched your search

@@ -4673,6 +4673,18 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
synchronized (lockFor(key(region, routeTableId))) {
RouteTable current = getRequiredRouteTable(region, routeTableId);
List<Route> next = new ArrayList<>(current.getRoutes());
// A destination identifies a route: ReplaceRoute matches the first copy and DeleteRoute
// removes every copy, so a table holding two routes with the same destination has no
// well-defined behaviour for either. AWS rejects the second CreateRoute instead, and it
// makes no exception for the local route seeded by CreateRouteTable.
if (next.stream().anyMatch(r -> matchesDestination(r, destinationCidrBlock,
destinationIpv6CidrBlock, destinationPrefixListId))) {
throw new AwsException("RouteAlreadyExists",
"The route identified by "
+ destinationLabel(destinationCidrBlock, destinationIpv6CidrBlock,
destinationPrefixListId)
+ " already exists", 400);
}
Route route = new Route(destinationCidrBlock, gatewayId, "CreateRoute");
route.setDestinationIpv6CidrBlock(destinationIpv6CidrBlock);
route.setDestinationPrefixListId(destinationPrefixListId);