Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Better handling of missing default VPCs #427

Merged
merged 2 commits into from
Apr 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions backend/dataall/aws/handlers/ec2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import logging

from .sts import SessionHelper


log = logging.getLogger(__name__)


class EC2:
@staticmethod
def client(account_id: str, region: str, role=None):
session = SessionHelper.remote_session(accountid=account_id, role=role)
return session.client('ec2', region_name=region)

@staticmethod
def check_default_vpc_exists(AwsAccountId: str, region: str, role=None):
log.info("Check that default VPC exists..")
client = EC2.client(account_id=AwsAccountId, region=region, role=role)
response = client.describe_vpcs(
Filters=[{'Name': 'isDefault', 'Values': ['true']}]
)
vpcs = response['Vpcs']
log.info(f"Default VPCs response: {vpcs}")
if vpcs:
return True
return False
20 changes: 12 additions & 8 deletions backend/dataall/cdkproxy/stacks/sagemakerstudio.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@
from ...db.api import Environment
from ...aws.handlers.parameter_store import ParameterStoreManager
from ...aws.handlers.sts import SessionHelper
from ...aws.handlers.sagemaker_studio import (
SagemakerStudio,
)
from ...aws.handlers.sagemaker_studio import SagemakerStudio
from ...aws.handlers.ec2 import EC2
from ...utils.cdk_nag_utils import CDKNagUtil
from ...utils.runtime_stacks_tagging import TagsUtil

Expand Down Expand Up @@ -58,17 +57,22 @@ def check_existing_sagemaker_studio_domain(self):

def create_sagemaker_domain_resources(self, sagemaker_principals):
logger.info('Creating SageMaker base resources..')
try:
cdk_look_up_role_arn = SessionHelper.get_cdk_look_up_role_arn(
accountid=self.environment.AwsAccountId, region=self.environment.region
)
existing_default_vpc = EC2.check_default_vpc_exists(
AwsAccountId=self.environment.AwsAccountId, region=self.environment.region, role=cdk_look_up_role_arn
)
if existing_default_vpc:
logger.info("Using default VPC for Sagemaker Studio domain")
# Use default VPC - initial configuration (to be migrated)
vpc = ec2.Vpc.from_lookup(self.stack, 'VPCStudio', is_default=True)
subnet_ids = [private_subnet.subnet_id for private_subnet in vpc.private_subnets]
subnet_ids += [public_subnet.subnet_id for public_subnet in vpc.public_subnets]
subnet_ids += [isolated_subnet.subnet_id for isolated_subnet in vpc.isolated_subnets]
security_groups = []
logger.info("Using default VPC for Sagemaker Studio domain")
except Exception as e:
logger.info(
f"Default VPC not found, Exception: {e}. Creating a VPC for SageMaker resources...")
else:
logger.info("Default VPC not found, Exception. Creating a VPC for SageMaker resources...")
# Create VPC with 3 Public Subnets and 3 Private subnets wit NAT Gateways
log_group = logs.LogGroup(
self.stack,
Expand Down