Skip to content

Commit dd44d25

Browse files
mmeidlmvsusp
authored andcommitted
Enable setting VPC config when creating/deploying models (aws#412)
* Create SageMaker Models including optional VpcConfig Models created from Estimator or Training Job use the same VpcConfig * Adjust naming, preparing, and passing vpc_config in base classes Estimator, Model, Session * Add optional vpc_config parameter for Amazon/Frameworks models * Improve integ test coverage of VPC features Configure subnets and security groups to support multi-node VPC jobs and endpoints Implement more VPC integ tests covering several Estimators and Predictors * Update changelog * Add vpc_utils with constants and utilities for handling VpcConfig * Rename/refactor parameter vpc_config_override for models created from estimators * Consolidate VPC integ tests to a single TensorFlow multi-instance case * Tweaks to vpc_utils: make VPC_CONFIG_DEFAULT immutable and rename validate to sanitize * Add integ test for transform with vpc config * Update README with SageMaker VPC documentation and examples * Remove tests/integ/test_vpc.py
1 parent 4d6a4a4 commit dd44d25

34 files changed

+853
-198
lines changed

CHANGELOG.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
CHANGELOG
33
=========
44

5+
=========
6+
1.11.2dev
7+
=========
8+
* enhancement: Enable setting VPC config when creating/deploying models
9+
10+
=======
511
1.11.1
612
======
713

README.rst

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ Table of Contents
3636
8. `BYO Docker Containers with SageMaker Estimators <#byo-docker-containers-with-sagemaker-estimators>`__
3737
9. `SageMaker Automatic Model Tuning <#sagemaker-automatic-model-tuning>`__
3838
10. `SageMaker Batch Transform <#sagemaker-batch-transform>`__
39-
11. `BYO Model <#byo-model>`__
39+
11. `Secure Training and Inference with VPC <#secure-training-and-inference-with-vpc>`__
40+
12. `BYO Model <#byo-model>`__
4041

4142

4243
Installing the SageMaker Python SDK
@@ -458,6 +459,71 @@ You can also specify other attributes of your data, such as the content type.
458459
For more details about what can be specified here, see `API docs <https://sagemaker.readthedocs.io/en/latest/transformer.html#sagemaker.transformer.Transformer.transform>`__.
459460

460461

462+
Secure Training and Inference with VPC
463+
--------------------------------------
464+
465+
Amazon SageMaker allows you to control network traffic to and from model container instances using Amazon Virtual Private Cloud (VPC).
466+
You can configure SageMaker to use your own private VPC in order to further protect and monitor traffic.
467+
468+
For more information about Amazon SageMaker VPC features, and guidelines for configuring your VPC,
469+
see the following documentation:
470+
471+
- `Protect Training Jobs by Using an Amazon Virtual Private Cloud <https://docs.aws.amazon.com/sagemaker/latest/dg/train-vpc.html>`__
472+
- `Protect Endpoints by Using an Amazon Virtual Private Cloud <https://docs.aws.amazon.com/sagemaker/latest/dg/host-vpc.html>`__
473+
- `Protect Data in Batch Transform Jobs by Using an Amazon Virtual Private Cloud <https://docs.aws.amazon.com/sagemaker/latest/dg/batch-vpc.html>`__
474+
- `Working with VPCs and Subnets <https://docs.aws.amazon.com/vpc/latest/userguide/working-with-vpcs.html>`__
475+
476+
You can also reference or reuse the example VPC created for integration tests: `tests/integ/vpc_test_utils.py <tests/integ/vpc_test_utils.py>`__
477+
478+
To train a model using your own VPC, set the optional parameters ``subnets`` and ``security_group_ids`` on an ``Estimator``:
479+
480+
.. code:: python
481+
482+
from sagemaker.mxnet import MXNet
483+
484+
# Configure an MXNet Estimator with subnets and security groups from your VPC
485+
mxnet_vpc_estimator = MXNet('train.py',
486+
train_instance_type='ml.p2.xlarge',
487+
train_instance_count = 1,
488+
subnets=['subnet-1', 'subnet-2'],
489+
security_group_ids=['sg-1'])
490+
491+
# SageMaker Training Job will set VpcConfig and container instances will run in your VPC
492+
mxnet_vpc_estimator.fit('s3://my_bucket/my_training_data/')
493+
494+
When you create a ``Predictor`` from the ``Estimator`` using ``deploy()``, the same VPC configurations will be set on the SageMaker Model:
495+
496+
.. code:: python
497+
498+
# Creates a SageMaker Model and Endpoint using the same VpcConfig
499+
# Endpoint container instances will run in your VPC
500+
mxnet_vpc_predictor = mxnet_vpc_estimator.deploy(initial_instance_count=1,
501+
instance_type='ml.p2.xlarge')
502+
503+
# You can also set ``vpc_config_override`` to use a different VpcConfig
504+
other_vpc_config = {'Subnets': ['subnet-3', 'subnet-4'],
505+
'SecurityGroupIds': ['sg-2']}
506+
mxnet_predictor_other_vpc = mxnet_vpc_estimator.deploy(initial_instance_count=1,
507+
instance_type='ml.p2.xlarge',
508+
vpc_config_override=other_vpc_config)
509+
510+
# Setting ``vpc_config_override=None`` will disable VpcConfig
511+
mxnet_predictor_no_vpc = mxnet_vpc_estimator.deploy(initial_instance_count=1,
512+
instance_type='ml.p2.xlarge',
513+
vpc_config_override=None)
514+
515+
Likewise, when you create ``Transformer`` from the ``Estimator`` using ``transformer()``, the same VPC configurations will be set on the SageMaker Model:
516+
517+
.. code:: python
518+
519+
# Creates a SageMaker Model using the same VpcConfig
520+
mxnet_vpc_transformer = mxnet_vpc_estimator.transformer(instance_count=1,
521+
instance_type='ml.p2.xlarge')
522+
523+
# Transform Job container instances will run in your VPC
524+
mxnet_vpc_transformer.transform('s3://my-bucket/batch-transform-input')
525+
526+
461527
FAQ
462528
---
463529

src/sagemaker/amazon/factorization_machines.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from sagemaker.predictor import RealTimePredictor
2020
from sagemaker.model import Model
2121
from sagemaker.session import Session
22+
from sagemaker.vpc_utils import VPC_CONFIG_DEFAULT
2223

2324

2425
class FactorizationMachines(AmazonAlgorithmEstimatorBase):
@@ -163,11 +164,19 @@ def __init__(self, role, train_instance_count, train_instance_type,
163164
self.factors_init_sigma = factors_init_sigma
164165
self.factors_init_value = factors_init_value
165166

166-
def create_model(self):
167+
def create_model(self, vpc_config_override=VPC_CONFIG_DEFAULT):
167168
"""Return a :class:`~sagemaker.amazon.FactorizationMachinesModel` referencing the latest
168-
s3 model data produced by this Estimator."""
169+
s3 model data produced by this Estimator.
169170
170-
return FactorizationMachinesModel(self.model_data, self.role, sagemaker_session=self.sagemaker_session)
171+
Args:
172+
vpc_config_override (dict[str, list[str]]): Optional override for VpcConfig set on the model.
173+
Default: use subnets and security groups from this Estimator.
174+
* 'Subnets' (list[str]): List of subnet ids.
175+
* 'SecurityGroupIds' (list[str]): List of security group ids.
176+
177+
"""
178+
return FactorizationMachinesModel(self.model_data, self.role, sagemaker_session=self.sagemaker_session,
179+
vpc_config=self.get_vpc_config(vpc_config_override))
171180

172181

173182
class FactorizationMachinesPredictor(RealTimePredictor):
@@ -195,12 +204,13 @@ class FactorizationMachinesModel(Model):
195204
"""Reference S3 model data created by FactorizationMachines estimator. Calling :meth:`~sagemaker.model.Model.deploy`
196205
creates an Endpoint and returns :class:`FactorizationMachinesPredictor`."""
197206

198-
def __init__(self, model_data, role, sagemaker_session=None):
207+
def __init__(self, model_data, role, sagemaker_session=None, **kwargs):
199208
sagemaker_session = sagemaker_session or Session()
200209
repo = '{}:{}'.format(FactorizationMachines.repo_name, FactorizationMachines.repo_version)
201210
image = '{}/{}'.format(registry(sagemaker_session.boto_session.region_name), repo)
202211
super(FactorizationMachinesModel, self).__init__(model_data,
203212
image,
204213
role,
205214
predictor_cls=FactorizationMachinesPredictor,
206-
sagemaker_session=sagemaker_session)
215+
sagemaker_session=sagemaker_session,
216+
**kwargs)

src/sagemaker/amazon/kmeans.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from sagemaker.predictor import RealTimePredictor
2020
from sagemaker.model import Model
2121
from sagemaker.session import Session
22+
from sagemaker.vpc_utils import VPC_CONFIG_DEFAULT
2223

2324

2425
class KMeans(AmazonAlgorithmEstimatorBase):
@@ -102,10 +103,18 @@ def __init__(self, role, train_instance_count, train_instance_type, k, init_meth
102103
self.center_factor = center_factor
103104
self.eval_metrics = eval_metrics
104105

105-
def create_model(self):
106+
def create_model(self, vpc_config_override=VPC_CONFIG_DEFAULT):
106107
"""Return a :class:`~sagemaker.amazon.kmeans.KMeansModel` referencing the latest
107-
s3 model data produced by this Estimator."""
108-
return KMeansModel(self.model_data, self.role, self.sagemaker_session)
108+
s3 model data produced by this Estimator.
109+
110+
Args:
111+
vpc_config_override (dict[str, list[str]]): Optional override for VpcConfig set on the model.
112+
Default: use subnets and security groups from this Estimator.
113+
* 'Subnets' (list[str]): List of subnet ids.
114+
* 'SecurityGroupIds' (list[str]): List of security group ids.
115+
"""
116+
return KMeansModel(self.model_data, self.role, self.sagemaker_session,
117+
vpc_config=self.get_vpc_config(vpc_config_override))
109118

110119
def _prepare_for_training(self, records, mini_batch_size=5000, job_name=None):
111120
super(KMeans, self)._prepare_for_training(records, mini_batch_size=mini_batch_size, job_name=job_name)
@@ -138,9 +147,10 @@ class KMeansModel(Model):
138147
"""Reference KMeans s3 model data. Calling :meth:`~sagemaker.model.Model.deploy` creates an Endpoint and return
139148
a Predictor to performs k-means cluster assignment."""
140149

141-
def __init__(self, model_data, role, sagemaker_session=None):
150+
def __init__(self, model_data, role, sagemaker_session=None, **kwargs):
142151
sagemaker_session = sagemaker_session or Session()
143152
repo = '{}:{}'.format(KMeans.repo_name, KMeans.repo_version)
144153
image = '{}/{}'.format(registry(sagemaker_session.boto_session.region_name), repo)
145154
super(KMeansModel, self).__init__(model_data, image, role, predictor_cls=KMeansPredictor,
146-
sagemaker_session=sagemaker_session)
155+
sagemaker_session=sagemaker_session,
156+
**kwargs)

src/sagemaker/amazon/knn.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from sagemaker.predictor import RealTimePredictor
2020
from sagemaker.model import Model
2121
from sagemaker.session import Session
22+
from sagemaker.vpc_utils import VPC_CONFIG_DEFAULT
2223

2324

2425
class KNN(AmazonAlgorithmEstimatorBase):
@@ -97,11 +98,18 @@ def __init__(self, role, train_instance_count, train_instance_type, k, sample_si
9798
if dimension_reduction_type and not dimension_reduction_target:
9899
raise ValueError('"dimension_reduction_target" is required when "dimension_reduction_type" is set.')
99100

100-
def create_model(self):
101+
def create_model(self, vpc_config_override=VPC_CONFIG_DEFAULT):
101102
"""Return a :class:`~sagemaker.amazon.KNNModel` referencing the latest
102-
s3 model data produced by this Estimator."""
103+
s3 model data produced by this Estimator.
103104
104-
return KNNModel(self.model_data, self.role, sagemaker_session=self.sagemaker_session)
105+
Args:
106+
vpc_config_override (dict[str, list[str]]): Optional override for VpcConfig set on the model.
107+
Default: use subnets and security groups from this Estimator.
108+
* 'Subnets' (list[str]): List of subnet ids.
109+
* 'SecurityGroupIds' (list[str]): List of security group ids.
110+
"""
111+
return KNNModel(self.model_data, self.role, sagemaker_session=self.sagemaker_session,
112+
vpc_config=self.get_vpc_config(vpc_config_override))
105113

106114
def _prepare_for_training(self, records, mini_batch_size=None, job_name=None):
107115
super(KNN, self)._prepare_for_training(records, mini_batch_size=mini_batch_size, job_name=job_name)
@@ -128,9 +136,9 @@ class KNNModel(Model):
128136
"""Reference S3 model data created by KNN estimator. Calling :meth:`~sagemaker.model.Model.deploy`
129137
creates an Endpoint and returns :class:`KNNPredictor`."""
130138

131-
def __init__(self, model_data, role, sagemaker_session=None):
139+
def __init__(self, model_data, role, sagemaker_session=None, **kwargs):
132140
sagemaker_session = sagemaker_session or Session()
133141
repo = '{}:{}'.format(KNN.repo_name, KNN.repo_version)
134142
image = '{}/{}'.format(registry(sagemaker_session.boto_session.region_name, KNN.repo_name), repo)
135143
super(KNNModel, self).__init__(model_data, image, role, predictor_cls=KNNPredictor,
136-
sagemaker_session=sagemaker_session)
144+
sagemaker_session=sagemaker_session, **kwargs)

src/sagemaker/amazon/lda.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from sagemaker.predictor import RealTimePredictor
2020
from sagemaker.model import Model
2121
from sagemaker.session import Session
22+
from sagemaker.vpc_utils import VPC_CONFIG_DEFAULT
2223

2324

2425
class LDA(AmazonAlgorithmEstimatorBase):
@@ -89,11 +90,18 @@ def __init__(self, role, train_instance_type, num_topics,
8990
self.max_iterations = max_iterations
9091
self.tol = tol
9192

92-
def create_model(self):
93+
def create_model(self, vpc_config_override=VPC_CONFIG_DEFAULT):
9394
"""Return a :class:`~sagemaker.amazon.LDAModel` referencing the latest
94-
s3 model data produced by this Estimator."""
95+
s3 model data produced by this Estimator.
9596
96-
return LDAModel(self.model_data, self.role, sagemaker_session=self.sagemaker_session)
97+
Args:
98+
vpc_config_override (dict[str, list[str]]): Optional override for VpcConfig set on the model.
99+
Default: use subnets and security groups from this Estimator.
100+
* 'Subnets' (list[str]): List of subnet ids.
101+
* 'SecurityGroupIds' (list[str]): List of security group ids.
102+
"""
103+
return LDAModel(self.model_data, self.role, sagemaker_session=self.sagemaker_session,
104+
vpc_config=self.get_vpc_config(vpc_config_override))
97105

98106
def _prepare_for_training(self, records, mini_batch_size, job_name=None):
99107
# mini_batch_size is required, prevent explicit calls with None
@@ -124,9 +132,9 @@ class LDAModel(Model):
124132
"""Reference LDA s3 model data. Calling :meth:`~sagemaker.model.Model.deploy` creates an Endpoint and return
125133
a Predictor that transforms vectors to a lower-dimensional representation."""
126134

127-
def __init__(self, model_data, role, sagemaker_session=None):
135+
def __init__(self, model_data, role, sagemaker_session=None, **kwargs):
128136
sagemaker_session = sagemaker_session or Session()
129137
repo = '{}:{}'.format(LDA.repo_name, LDA.repo_version)
130138
image = '{}/{}'.format(registry(sagemaker_session.boto_session.region_name, LDA.repo_name), repo)
131139
super(LDAModel, self).__init__(model_data, image, role, predictor_cls=LDAPredictor,
132-
sagemaker_session=sagemaker_session)
140+
sagemaker_session=sagemaker_session, **kwargs)

src/sagemaker/amazon/linear_learner.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from sagemaker.predictor import RealTimePredictor
2020
from sagemaker.model import Model
2121
from sagemaker.session import Session
22+
from sagemaker.vpc_utils import VPC_CONFIG_DEFAULT
2223

2324

2425
class LinearLearner(AmazonAlgorithmEstimatorBase):
@@ -246,11 +247,18 @@ def __init__(self, role, train_instance_count, train_instance_type, predictor_ty
246247
raise ValueError(
247248
"For predictor_type 'multiclass_classifier', 'num_classes' should be set to a value greater than 2.")
248249

249-
def create_model(self):
250+
def create_model(self, vpc_config_override=VPC_CONFIG_DEFAULT):
250251
"""Return a :class:`~sagemaker.amazon.LinearLearnerModel` referencing the latest
251-
s3 model data produced by this Estimator."""
252+
s3 model data produced by this Estimator.
252253
253-
return LinearLearnerModel(self.model_data, self.role, self.sagemaker_session)
254+
Args:
255+
vpc_config_override (dict[str, list[str]]): Optional override for VpcConfig set on the model.
256+
Default: use subnets and security groups from this Estimator.
257+
* 'Subnets' (list[str]): List of subnet ids.
258+
* 'SecurityGroupIds' (list[str]): List of security group ids.
259+
"""
260+
return LinearLearnerModel(self.model_data, self.role, self.sagemaker_session,
261+
vpc_config=self.get_vpc_config(vpc_config_override))
254262

255263
def _prepare_for_training(self, records, mini_batch_size=None, job_name=None):
256264
num_records = None
@@ -293,10 +301,11 @@ class LinearLearnerModel(Model):
293301
"""Reference LinearLearner s3 model data. Calling :meth:`~sagemaker.model.Model.deploy` creates an Endpoint
294302
and returns a :class:`LinearLearnerPredictor`"""
295303

296-
def __init__(self, model_data, role, sagemaker_session=None):
304+
def __init__(self, model_data, role, sagemaker_session=None, **kwargs):
297305
sagemaker_session = sagemaker_session or Session()
298306
repo = '{}:{}'.format(LinearLearner.repo_name, LinearLearner.repo_version)
299307
image = '{}/{}'.format(registry(sagemaker_session.boto_session.region_name), repo)
300308
super(LinearLearnerModel, self).__init__(model_data, image, role,
301309
predictor_cls=LinearLearnerPredictor,
302-
sagemaker_session=sagemaker_session)
310+
sagemaker_session=sagemaker_session,
311+
**kwargs)

src/sagemaker/amazon/ntm.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from sagemaker.predictor import RealTimePredictor
2020
from sagemaker.model import Model
2121
from sagemaker.session import Session
22+
from sagemaker.vpc_utils import VPC_CONFIG_DEFAULT
2223

2324

2425
class NTM(AmazonAlgorithmEstimatorBase):
@@ -107,11 +108,18 @@ def __init__(self, role, train_instance_count, train_instance_type, num_topics,
107108
self.weight_decay = weight_decay
108109
self.learning_rate = learning_rate
109110

110-
def create_model(self):
111+
def create_model(self, vpc_config_override=VPC_CONFIG_DEFAULT):
111112
"""Return a :class:`~sagemaker.amazon.NTMModel` referencing the latest
112-
s3 model data produced by this Estimator."""
113+
s3 model data produced by this Estimator.
113114
114-
return NTMModel(self.model_data, self.role, sagemaker_session=self.sagemaker_session)
115+
Args:
116+
vpc_config_override (dict[str, list[str]]): Optional override for VpcConfig set on the model.
117+
Default: use subnets and security groups from this Estimator.
118+
* 'Subnets' (list[str]): List of subnet ids.
119+
* 'SecurityGroupIds' (list[str]): List of security group ids.
120+
"""
121+
return NTMModel(self.model_data, self.role, sagemaker_session=self.sagemaker_session,
122+
vpc_config=self.get_vpc_config(vpc_config_override))
115123

116124
def _prepare_for_training(self, records, mini_batch_size, job_name=None):
117125
if mini_batch_size is not None and (mini_batch_size < 1 or mini_batch_size > 10000):
@@ -140,9 +148,10 @@ class NTMModel(Model):
140148
"""Reference NTM s3 model data. Calling :meth:`~sagemaker.model.Model.deploy` creates an Endpoint and return
141149
a Predictor that transforms vectors to a lower-dimensional representation."""
142150

143-
def __init__(self, model_data, role, sagemaker_session=None):
151+
def __init__(self, model_data, role, sagemaker_session=None, **kwargs):
144152
sagemaker_session = sagemaker_session or Session()
145153
repo = '{}:{}'.format(NTM.repo_name, NTM.repo_version)
146154
image = '{}/{}'.format(registry(sagemaker_session.boto_session.region_name, NTM.repo_name), repo)
147155
super(NTMModel, self).__init__(model_data, image, role, predictor_cls=NTMPredictor,
148-
sagemaker_session=sagemaker_session)
156+
sagemaker_session=sagemaker_session,
157+
**kwargs)

0 commit comments

Comments
 (0)