Skip to content

change:enable network isolation for amazon estimators #1293

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

Merged
merged 3 commits into from
Feb 14, 2020
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
29 changes: 20 additions & 9 deletions src/sagemaker/amazon/amazon_estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@ class AmazonAlgorithmEstimatorBase(EstimatorBase):
repo_version = None

def __init__(
self, role, train_instance_count, train_instance_type, data_location=None, **kwargs
self,
role,
train_instance_count,
train_instance_type,
data_location=None,
enable_network_isolation=False,
**kwargs
):
"""Initialize an AmazonAlgorithmEstimatorBase.

Expand All @@ -63,6 +69,10 @@ def __init__(
"s3://example-bucket/some-key-prefix/". Objects will be saved in
a unique sub-directory of the specified location. If None, a
default data location will be used.
enable_network_isolation (bool): Specifies whether container will
run in network isolation mode. Network isolation mode restricts
the container access to outside networks (such as the internet).
Also known as internet-free mode (default: ``False``).
**kwargs: Additional parameters passed to
:class:`~sagemaker.estimator.EstimatorBase`.

Expand All @@ -71,14 +81,6 @@ def __init__(
You can find additional parameters for initializing this class at
:class:`~sagemaker.estimator.EstimatorBase`.
"""

if "enable_network_isolation" in kwargs:
logger.debug(
"removing unused enable_network_isolation argument: %s",
str(kwargs["enable_network_isolation"]),
)
del kwargs["enable_network_isolation"]

super(AmazonAlgorithmEstimatorBase, self).__init__(
role, train_instance_count, train_instance_type, **kwargs
)
Expand All @@ -87,6 +89,7 @@ def __init__(
self.sagemaker_session.default_bucket()
)
self._data_location = data_location
self._enable_network_isolation = enable_network_isolation

def train_image(self):
"""Placeholder docstring"""
Expand All @@ -98,6 +101,14 @@ def hyperparameters(self):
"""Placeholder docstring"""
return hp.serialize_all(self)

def enable_network_isolation(self):
"""If this Estimator can use network isolation when running.

Returns:
bool: Whether this Estimator can use network isolation or not.
"""
return self._enable_network_isolation

@property
def data_location(self):
"""Placeholder docstring"""
Expand Down
6 changes: 5 additions & 1 deletion tests/integ/test_pca.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def test_pca(sagemaker_session, cpu_instance_type):
train_instance_type=cpu_instance_type,
num_components=48,
sagemaker_session=sagemaker_session,
enable_network_isolation=True,
)

pca.algorithm_mode = "randomized"
Expand All @@ -50,7 +51,10 @@ def test_pca(sagemaker_session, cpu_instance_type):

with timeout_and_delete_endpoint_by_name(job_name, sagemaker_session):
pca_model = sagemaker.amazon.pca.PCAModel(
model_data=pca.model_data, role="SageMakerRole", sagemaker_session=sagemaker_session
model_data=pca.model_data,
role="SageMakerRole",
sagemaker_session=sagemaker_session,
enable_network_isolation=True,
)
predictor = pca_model.deploy(
initial_instance_count=1, instance_type=cpu_instance_type, endpoint_name=job_name
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/test_amazon_estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,18 @@ def test_gov_ecr_uri():
def test_init(sagemaker_session):
pca = PCA(num_components=55, sagemaker_session=sagemaker_session, **COMMON_ARGS)
assert pca.num_components == 55
assert pca.enable_network_isolation() is False


def test_init_enable_network_isolation(sagemaker_session):
pca = PCA(
num_components=55,
sagemaker_session=sagemaker_session,
enable_network_isolation=True,
**COMMON_ARGS
)
assert pca.num_components == 55
assert pca.enable_network_isolation() is True


def test_init_all_pca_hyperparameters(sagemaker_session):
Expand Down