|
5 | 5 | "metadata": {},
|
6 | 6 | "source": [
|
7 | 7 | "# SageMaker and AWS KMS–Managed Keys\n",
|
8 |
| - "_**Handling KMS encrypted data with SageMaker model training and encrypting the generated model artifacts**_\n", |
9 |
| - "\n", |
10 |
| - "---\n", |
| 8 | + "_**End-to-end encryption using SageMaker and KMS-Managed keys**_\n", |
11 | 9 | "\n",
|
12 | 10 | "---\n",
|
13 | 11 | "\n",
|
|
19 | 17 | "1. [Training the XGBoost model](#Training-the-XGBoost-model)\n",
|
20 | 18 | "1. [Set up hosting for the model](#Set-up-hosting-for-the-model)\n",
|
21 | 19 | "1. [Validate the model for use](#Validate-the-model-for-use)\n",
|
| 20 | + "1. [Run batch prediction using batch transform](#Run-batch-prediction-using-batch-transform)\n", |
22 | 21 | "\n",
|
23 | 22 | "---\n",
|
24 | 23 | "## Background\n",
|
25 | 24 | "\n",
|
26 | 25 | "AWS Key Management Service ([AWS KMS](http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingKMSEncryption.html)) enables \n",
|
27 |
| - "Server-side encryption to protect your data at rest. Amazon SageMaker training works with KMS encrypted data if the IAM role used for S3 access has permissions to encrypt and decrypt data with the KMS key. Further, a KMS key can also be used to encrypt the model artifacts at rest using Amazon S3 server-side encryption. In this notebook, we demonstrate SageMaker training with KMS encrypted data. \n", |
| 26 | + "Server-side encryption to protect your data at rest. Amazon SageMaker training works with KMS encrypted data if the IAM role used for S3 access has permissions to encrypt and decrypt data with the KMS key. Further, a KMS key can also be used to encrypt the model artifacts at rest using Amazon S3 server-side encryption. Additionally, a KMS key can also be used to encrypt the storage volume attached to training, endpoint, and transform instances. In this notebook, we demonstrate SageMaker encryption capabilities using KMS-managed keys. \n", |
28 | 27 | "\n",
|
29 | 28 | "---\n",
|
30 | 29 | "\n",
|
|
36 | 35 | "\n",
|
37 | 36 | "1. Have an existing KMS key from AWS IAM console or create one ([learn more](http://docs.aws.amazon.com/kms/latest/developerguide/create-keys.html)).\n",
|
38 | 37 | "2. Allow the IAM role used for SageMaker to encrypt and decrypt data with this key from within applications and when using AWS services integrated with KMS ([learn more](http://docs.aws.amazon.com/console/kms/key-users)).\n",
|
| 38 | + "3. Allow the IAM role for this notebook to create grants with this key ([learn more](https://docs.aws.amazon.com/sagemaker/latest/dg/api-permissions-reference.html)).\n", |
39 | 39 | "\n",
|
40 | 40 | "We use the `key-id` from the KMS key ARN `arn:aws:kms:region:acct-id:key/key-id`.\n",
|
41 | 41 | "\n",
|
42 | 42 | "### General Setup\n",
|
43 | 43 | "Let's start by specifying:\n",
|
44 | 44 | "* AWS region.\n",
|
45 | 45 | "* The IAM role arn used to give learning and hosting access to your data. See the documentation for how to specify these.\n",
|
| 46 | + "* The KMS key arn that you want to use for encryption.\n", |
46 | 47 | "* The S3 bucket that you want to use for training and model data."
|
47 | 48 | ]
|
48 | 49 | },
|
|
68 | 69 | "\n",
|
69 | 70 | "role = get_execution_role()\n",
|
70 | 71 | "\n",
|
71 |
| - "kms_key_id = '<your-kms-key-id>'\n", |
| 72 | + "kms_key_arn = '<your-kms-key-arn>'\n", |
72 | 73 | "\n",
|
73 | 74 | "bucket='<s3-bucket>' # put your s3 bucket name here, and create s3 bucket\n",
|
74 | 75 | "prefix = 'sagemaker/DEMO-kms'\n",
|
|
174 | 175 | "\n",
|
175 | 176 | "data_train = open(train_file, 'rb')\n",
|
176 | 177 | "key_train = '{}/train/{}'.format(prefix,train_file)\n",
|
177 |
| - "\n", |
| 178 | + "kms_key_id = kms_key_arn.split(':key/')[1]\n", |
178 | 179 | "\n",
|
179 | 180 | "print(\"Put object...\")\n",
|
180 | 181 | "s3.put_object(Bucket=bucket,\n",
|
|
215 | 216 | "source": [
|
216 | 217 | "## Training the SageMaker XGBoost model\n",
|
217 | 218 | "\n",
|
218 |
| - "Now that we have our data in S3, we can begin training. We'll use Amazon SageMaker XGboost algorithm as an example to demonstrate model training. Note that nothing needs to be changed in the way you'd call the training algorithm. The only requirement for training to succeed is that the IAM role (`role`) used for S3 access has permissions to encrypt and decrypt data with the KMS key (`kms_key_id`). You can set these permissions using the instructions [here](http://docs.aws.amazon.com/kms/latest/developerguide/key-policies.html#key-policy-default-allow-users). If the permissions aren't set, you'll get the `Data download failed` error." |
| 219 | + "Now that we have our data in S3, we can begin training. We'll use Amazon SageMaker XGboost algorithm as an example to demonstrate model training. Note that nothing needs to be changed in the way you'd call the training algorithm. The only requirement for training to succeed is that the IAM role (`role`) used for S3 access has permissions to encrypt and decrypt data with the KMS key (`kms_key_arn`). You can set these permissions using the instructions [here](http://docs.aws.amazon.com/kms/latest/developerguide/key-policies.html#key-policy-default-allow-users). If the permissions aren't set, you'll get the `Data download failed` error. Specify a `VolumeKmsKeyId` in the training job parameters to have the volume attached to the ML compute instance encrypted using key provided." |
219 | 220 | ]
|
220 | 221 | },
|
221 | 222 | {
|
|
254 | 255 | " \"ResourceConfig\": {\n",
|
255 | 256 | " \"InstanceCount\": 1,\n",
|
256 | 257 | " \"InstanceType\": \"ml.m4.4xlarge\",\n",
|
257 |
| - " \"VolumeSizeInGB\": 5\n", |
| 258 | + " \"VolumeSizeInGB\": 5,\n", |
| 259 | + " \"VolumeKmsKeyId\": kms_key_arn\n", |
258 | 260 | " },\n",
|
259 | 261 | " \"TrainingJobName\": job_name,\n",
|
260 | 262 | " \"HyperParameters\": {\n",
|
|
362 | 364 | "source": [
|
363 | 365 | "### Create endpoint configuration\n",
|
364 | 366 | "\n",
|
365 |
| - "SageMaker supports configuring REST endpoints in hosting with multiple models, e.g. for A/B testing purposes. In order to support this, customers create an endpoint configuration, that describes the distribution of traffic across the models, whether split, shadowed, or sampled in some way. In addition, the endpoint configuration describes the instance type required for model deployment." |
| 367 | + "SageMaker supports configuring REST endpoints in hosting with multiple models, e.g. for A/B testing purposes. In order to support this, customers create an endpoint configuration, that describes the distribution of traffic across the models, whether split, shadowed, or sampled in some way. In addition, the endpoint configuration describes the instance type required for model deployment and the key used to encrypt the volume attached to the endpoint instance." |
366 | 368 | ]
|
367 | 369 | },
|
368 | 370 | {
|
|
377 | 379 | "print(endpoint_config_name)\n",
|
378 | 380 | "create_endpoint_config_response = client.create_endpoint_config(\n",
|
379 | 381 | " EndpointConfigName = endpoint_config_name,\n",
|
| 382 | + " KmsKeyId = kms_key_arn,\n", |
380 | 383 | " ProductionVariants=[{\n",
|
381 | 384 | " 'InstanceType':'ml.m4.xlarge',\n",
|
382 | 385 | " 'InitialVariantWeight':1,\n",
|
|
499 | 502 | "metadata": {},
|
500 | 503 | "source": [
|
501 | 504 | "## Run batch prediction using batch transform\n",
|
502 |
| - "Create a transform job to do batch prediction using the trained model. Similar to the training section above, the execution role assumed by this notebook must have permissions to encrypt and decrypt data with the KMS key (`kms_key_id`) used for S3 server-side encryption." |
| 505 | + "Create a transform job to do batch prediction using the trained model. Similar to the training section above, the execution role assumed by this notebook must have permissions to encrypt and decrypt data with the KMS key (`kms_key_arn`) used for S3 server-side encryption. Similar to training, specify a `VolumeKmsKeyId` so that the volume attached to the transform instance is encrypted using the key provided." |
503 | 506 | ]
|
504 | 507 | },
|
505 | 508 | {
|
|
532 | 535 | " },\n",
|
533 | 536 | " \"TransformResources\": {\n",
|
534 | 537 | " \"InstanceCount\": 1,\n",
|
535 |
| - " \"InstanceType\": \"ml.c4.xlarge\"\n", |
| 538 | + " \"InstanceType\": \"ml.c4.xlarge\",\n", |
| 539 | + " \"VolumeKmsKeyId\": kms_key_arn\n", |
536 | 540 | " }\n",
|
537 | 541 | "}\n",
|
538 | 542 | "\n",
|
|
547 | 551 | " print(\"Transform job completed!\")\n",
|
548 | 552 | " break\n",
|
549 | 553 | " else:\n",
|
550 |
| - " print(\"Unexpected transform job status: \" + status)" |
| 554 | + " print(\"Unexpected transform job status: \" + status)\n", |
| 555 | + " break" |
551 | 556 | ]
|
552 | 557 | },
|
553 | 558 | {
|
|
0 commit comments