diff --git a/moto/ec2/models/route_tables.py b/moto/ec2/models/route_tables.py index 0932e2103b2d..9b4d7959615f 100644 --- a/moto/ec2/models/route_tables.py +++ b/moto/ec2/models/route_tables.py @@ -467,9 +467,7 @@ def replace_route( if destination_prefix_list_id: cidr = destination_prefix_list_id route_table = self.get_route_table(route_table_id) - route_id = generate_route_id( - route_table.id, destination_cidr_block, destination_ipv6_cidr_block - ) + route_id = generate_route_id(route_table.id, cidr, destination_ipv6_cidr_block) try: route = route_table.routes[route_id] except KeyError: diff --git a/tests/test_ec2/test_route_tables.py b/tests/test_ec2/test_route_tables.py index eb7b6605c484..cdf88ab17bf8 100644 --- a/tests/test_ec2/test_route_tables.py +++ b/tests/test_ec2/test_route_tables.py @@ -606,8 +606,14 @@ def test_routes_replace(): ] )["RouteTables"][0]["RouteTableId"] main_route_table = ec2.RouteTable(main_route_table_id) + + # Various route sources ROUTE_CIDR = "10.0.0.4/24" + prefix_list = client.create_managed_prefix_list( + PrefixListName="examplelist", MaxEntries=2, AddressFamily="?" + )["PrefixList"] + # Various route targets igw = ec2.create_internet_gateway() @@ -626,7 +632,8 @@ def get_target_route(): routes = [ route for route in route_table["Routes"] - if route["DestinationCidrBlock"] != vpc.cidr_block + if "DestinationCidrBlock" not in route + or route["DestinationCidrBlock"] != vpc.cidr_block ] assert len(routes) == 1 return routes[0] @@ -692,6 +699,30 @@ def get_target_route(): # equivalent, but for some reason AWS returns InvalidParameterValue instead. assert ex.value.response["Error"]["Code"] == "InvalidParameterValue" + # Remove route + client.delete_route( + RouteTableId=main_route_table.id, DestinationCidrBlock=ROUTE_CIDR + ) + + # Create prefix list source route + main_route_table.create_route( + DestinationPrefixListId=prefix_list["PrefixListId"], GatewayId=igw.id + ) + + # Replace... + client.replace_route( + RouteTableId=main_route_table.id, + DestinationPrefixListId=prefix_list["PrefixListId"], + InstanceId=instance.id, + ) + + target_route = get_target_route() + assert "GatewayId" not in target_route + assert target_route["InstanceId"] == instance.id + assert "NetworkInterfaceId" not in target_route + assert target_route["State"] == "active" + assert target_route["DestinationPrefixListId"] == prefix_list["PrefixListId"] + @mock_aws def test_routes_already_exist():