Skip to content

Commit

Permalink
[Develop] Fix No route tables found bug when specifying default VPC s…
Browse files Browse the repository at this point in the history
…ubnet to LoginNodes/Networking/SubnetIds (#6423)

* Add fetch the VPC-level route table step when the subnet doesn't have a specific route table
  • Loading branch information
hehe7318 committed Sep 5, 2024
1 parent b5bb86a commit 025853f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ CHANGELOG
- Fix issue with login nodes being marked unhealthy when restricting SSH access.
- Fix `retrieve_supported_regions` so that it can get the correct S3 url.
- Fix `describe_images` so that it uses pagination.
- Fix `No route tables found` bug when specifying default VPC subnet to LoginNodes/Networking/SubnetIds.

3.10.1
------
Expand Down
28 changes: 22 additions & 6 deletions cli/src/pcluster/aws/ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,14 +549,30 @@ def describe_route_tables(self, filters=None):

@AWSExceptionHandler.handle_client_exception
def is_subnet_public(self, subnet_id):
"""Check if a subnet is public."""
route_tables = self.describe_route_tables(filters=[{"Name": "association.subnet-id", "Values": [subnet_id]}])
if not route_tables:
raise Exception("No route tables found. The subnet configuration may be incorrect.")
"""
Check if a subnet is public by determining if its route table has an internet gateway (igw).
route_table = route_tables[0]
:param subnet_id: The ID of the subnet to check.
:return: True if the subnet is public (associated with an internet gateway); False otherwise.
:raises Exception: If no route tables are found for the subnet or its VPC.
"""
route_tables = self.describe_route_tables(filters=[{"Name": "association.subnet-id", "Values": [subnet_id]}])

for route in route_table.get("Routes", []):
# If the subnet doesn't have a specific route table, this typically happens when
# the subnet is using the VPC's main route table. Needs to fetch the VPC-level route table.
if not route_tables:
# Retrieve the VPC ID for the given subnet ID
subnets = self.describe_subnets([subnet_id])
if not subnets:
raise Exception(f"No subnet found with ID {subnet_id}")
vpc_id = subnets[0].get("VpcId")

route_tables = self.describe_route_tables(filters=[{"Name": "vpc-id", "Values": [vpc_id]}])
if not route_tables:
raise Exception("No route tables found. The subnet or VPC configuration may be incorrect.")

# Check if any route contains an internet gateway (igw)
for route in route_tables[0].get("Routes", []):
if "GatewayId" in route and route["GatewayId"].startswith("igw-"):
return True

Expand Down

0 comments on commit 025853f

Please sign in to comment.