Compare commits

...
1 Commits
Author SHA1 Message Date
Andrew Ferlitsch f2916d2c22 feat: CMEK training 2023-03-20 19:00:46 +00:00
@@ -0,0 +1,961 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "VBOfRw7ifk8w"
},
"outputs": [],
"source": [
"# Copyright 2022 Google LLC\n",
"#\n",
"# Licensed under the Apache License, Version 2.0 (the \"License\");\n",
"# you may not use this file except in compliance with the License.\n",
"# You may obtain a copy of the License at\n",
"#\n",
"# https://www.apache.org/licenses/LICENSE-2.0\n",
"#\n",
"# Unless required by applicable law or agreed to in writing, software\n",
"# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
"# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
"# See the License for the specific language governing permissions and\n",
"# limitations under the License."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "title:generic,gcp"
},
"source": [
"# AutoML image classfication training with Customer Managed Encryption Keys (CMEK)\n",
"<table align=\"left\">\n",
" <td>\n",
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/automl/get_started_automl_cmek_training.ipynb\">\n",
" <img src=\"https://cloud.google.com/ml-engine/images/github-logo-32px.png\" alt=\"GitHub logo\">\n",
" View on GitHub\n",
" </a>\n",
" </td>\n",
" <td>\n",
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/offical/automl/get_started_automl_cmek_training.ipynb\">\n",
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Colab logo\"> Run in Colab\n",
" </a>\n",
" </td>\n",
" <td>\n",
" <a href=\"https://console.cloud.google.com/vertex-ai/workbench/deploy-notebook?download_url=https://raw.githubusercontent.com/GoogleCloudPlatform/vertex-ai-samples/main/notebooks/official/automl/get_started_automl_cmek_training.ipynb\">\n",
" <img src=\"https://lh3.googleusercontent.com/UiNooY4LUgW_oTvpsNhPpQzsstV5W8F7rYgxgGBD85cWJoLmrOzhVs_ksK_vgx40SHs7jCqkTkCk=e14-rj-sc0xffffff-h130-w32\" alt=\"Vertex AI logo\">\n",
" Open in Vertex AI Workbench\n",
" </a>\n",
" </td>\n",
"</table>\n",
"<br/><br/><br/>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "overview:mlops"
},
"source": [
"## Overview\n",
"\n",
"\n",
"This tutorial demonstrates how to use AutoML training with a customer managed encyrption key CMEK.\n",
"\n",
"Learn more about []()."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "objective:mlops,stage3,get_started_automl_pipeline_components"
},
"source": [
"### Objective\n",
"\n",
"In this tutorial, you learn how to use a customer managed encryption key (CMEK) for `Vertex AI AutoML` training.\n",
"\n",
"This tutorial uses the following Google Cloud ML services:\n",
"\n",
"- `Vertex AI AutoML`\n",
"- Customer managed encryption key.\n",
"\n",
"The steps performed include:\n",
"\n",
"- Creating a customer managed encryption key.\n",
"- Creating an image dataset with CMEK encryption.\n",
"- Train an AutoML model with CMEK encryption."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "dataset:flowers,icn"
},
"source": [
"### Dataset\n",
"\n",
"The dataset used for this tutorial is the [Flowers dataset](https://www.tensorflow.org/datasets/catalog/tf_flowers) from [TensorFlow](https://www.tensorflow.org/datasets/catalog/overview). The version of the dataset in this tutorial is stored in a public #(GCS) bucket. The trained model predicts the type of flower an image is from a class of five flowers: daisy, dandelion, rose, sunflower, or tulip.\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "5e2eba58ad71"
},
"source": [
"### Costs \n",
"\n",
"This tutorial uses billable components of Google Cloud:\n",
"\n",
"* Vertex AI\n",
"* Cloud Storage\n",
"\n",
"Learn about [Vertex AI\n",
"pricing](https://cloud.google.com/vertex-ai/pricing) and [Cloud Storage\n",
"pricing](https://cloud.google.com/storage/pricing), and use the [Pricing\n",
"Calculator](https://cloud.google.com/products/calculator/)\n",
"to generate a cost estimate based on your projected usage."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "install_mlops"
},
"source": [
"## Installations\n",
"\n",
"Install the Vertex AI SDK and the KMS package for CMEK encryption."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "install_aip:mbsdk"
},
"outputs": [],
"source": [
"import os\n",
"\n",
"USER_FLAG = \"\"\n",
"\n",
"! pip3 install --upgrade google-cloud-aiplatform \\\n",
" google-cloud-kms $USER_FLAG \\\n",
" tensorflow"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "restart"
},
"source": [
"### Colab only: Uncomment the following cell to restart the kernel"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "D-ZBOjErv5mM"
},
"outputs": [],
"source": [
"# Automatically restart kernel after installs so that your environment can access the new packages\n",
"# import IPython\n",
"\n",
"# app = IPython.Application.instance()\n",
"# app.kernel.do_shutdown(True)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "013daf3de88e"
},
"source": [
"## Before you begin"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "before_you_begin:nogpu"
},
"source": [
"### Set your project ID\n",
"\n",
"**If you don't know your project ID**, try the following:\n",
"* Run `gcloud config list`.\n",
"* Run `gcloud projects list`.\n",
"* See the support page: [Locate the project ID](https://support.google.com/googleapi/answer/7014113)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "set_project_id"
},
"outputs": [],
"source": [
"PROJECT_ID = \"[your-project-id]\" # @param {type:\"string\"}\n",
"\n",
"# Set the project id\n",
"! gcloud config set project {PROJECT_ID}"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "region"
},
"source": [
"#### Region\n",
"\n",
"You can also change the `REGION` variable used by Vertex AI. Learn more about [Vertex AI regions](https://cloud.google.com/vertex-ai/docs/general/locations)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "region"
},
"outputs": [],
"source": [
"REGION = \"us-central1\" # @param {type: \"string\"}"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "gcp_authenticate"
},
"source": [
"### Authenticate your Google Cloud account\n",
"\n",
"Depending on your Jupyter environment, you may have to manually authenticate. Follow the relevant instructions below."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "FvQeFm3Gv5mR"
},
"source": [
"**1. Vertex AI Workbench**\n",
"* Do nothing as you are already authenticated."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ad1138a125ea"
},
"source": [
"**2. Local JupyterLab instance, uncomment and run:**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "ce6043da7b33"
},
"outputs": [],
"source": [
"# ! gcloud auth login"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "0367eac06a10"
},
"source": [
"**3. Colab, uncomment and run:**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "21ad4dbb4a61"
},
"outputs": [],
"source": [
"# from google.colab import auth\n",
"# auth.authenticate_user()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "c13224697bfb"
},
"source": [
"**4. Service account or other**\n",
"* See how to grant Cloud Storage permissions to your service account at https://cloud.google.com/storage/docs/gsutil/commands/iam#ch-examples."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "bucket:mbsdk"
},
"source": [
"### Create a Cloud Storage bucket\n",
"\n",
"Create a storage bucket to store intermediate artifacts such as datasets."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "bucket"
},
"outputs": [],
"source": [
"BUCKET_URI = f\"gs://your-bucket-name-unique-{PROJECT_ID}\" # @param {type:\"string\"}"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "create_bucket"
},
"source": [
"**Only if your bucket doesn't already exist**: Run the following cell to create your Cloud Storage bucket."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "create_bucket"
},
"outputs": [],
"source": [
"! gsutil mb -l $REGION $BUCKET_URI"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "setup_vars"
},
"source": [
"### Set up variables\n",
"\n",
"Next, set up some variables used throughout the tutorial.\n",
"### Import libraries and define constants"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "import_aip:mbsdk"
},
"outputs": [],
"source": [
"import google.cloud.aiplatform as aiplatform\n",
"from google.cloud import kms"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "init_aip:mbsdk,all"
},
"source": [
"### Initialize Vertex AI SDK for Python\n",
"\n",
"Initialize the Vertex AI SDK for Python for your project and corresponding bucket."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "init_aip:mbsdk,all"
},
"outputs": [],
"source": [
"aiplatform.init(project=PROJECT_ID, location=REGION, staging_bucket=BUCKET_URI)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "mRk9eoTm6Pyi"
},
"source": [
"## Setting up Customer Managed Encryption Keys\n",
"\n",
"By default, Google Cloud automatically encrypts data when it is stored in Cloud Storage using encryption keys managed by Google. If you have specific compliance or regulatory requirements related to the keys that protect your data, you can use customer-managed encryption keys (CMEK) for your training jobs.\n",
"\n",
"### Enable KMS API\n",
"\n",
"First, you enable the [Cloud Key Management Service (KMS)](https://console.cloud.google.com/flows/enableapi?apiid=cloudkms.googleapis.com)\n",
"\n",
"Learn more about [Customer managed encryption keys (CMEK)](https://cloud.google.com/vertex-ai/docs/general/cmek)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "RD_Pvrg584X3"
},
"source": [
"### Create a key ring\n",
"\n",
"After you have enabled the KMS API, you create a key ring and a key. Use the helper function `create_key_ring()` to create a key ring, with the following parameters:\n",
"\n",
"- `project_id`: Your project ID.\n",
"- `location`: Your region.\n",
"- `key_ring_id`: The unique identifier for your key ring.\n",
"\n",
"The helper function calls the KMS client method `create_key_ring()` to create your key ring.\n",
"\n",
"Learn more about [KMS: Create a key ring](https://cloud.google.com/kms/docs/samples/kms-create-key-ring)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "dxRZzbvQnZC7"
},
"outputs": [],
"source": [
"KEY_RING_ID = \"your_cmek_key_ring_id\"\n",
"\n",
"\n",
"def create_key_ring(project_id, location, key_ring_id):\n",
" \"\"\"\n",
" Creates a new key ring in Cloud KMS\n",
"\n",
" Args:\n",
" project_id (string): Google Cloud project ID (e.g. 'my-project').\n",
" location (string): Cloud KMS location (e.g. 'us-east1').\n",
" id (string): ID of the key ring to create (e.g. 'my-key-ring').\n",
"\n",
" Returns:\n",
" KeyRing: Cloud KMS key ring.\n",
"\n",
" \"\"\"\n",
"\n",
" # Create the client.\n",
" client = kms.KeyManagementServiceClient()\n",
"\n",
" # Build the parent location name.\n",
" location_name = f\"projects/{project_id}/locations/{location}\"\n",
"\n",
" # Build the key ring.\n",
" key_ring = {}\n",
"\n",
" # Call the API.\n",
" created_key_ring = client.create_key_ring(\n",
" request={\n",
" \"parent\": location_name,\n",
" \"key_ring_id\": key_ring_id,\n",
" \"key_ring\": key_ring,\n",
" }\n",
" )\n",
" print(\"Created key ring: {}\".format(created_key_ring.name))\n",
" return created_key_ring\n",
"\n",
"\n",
"key_ring = create_key_ring(\n",
" project_id=PROJECT_ID, location=REGION, key_ring_id=KEY_RING_ID\n",
")\n",
"print(key_ring)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "gCL1-IfFtWXl"
},
"source": [
"### Create a key\n",
"\n",
"Next, you create your key. Use the helper function `create_key()` with the following parameters:\n",
"\n",
"- `project_id`: Your project ID.\n",
"- `location`: Your region.\n",
"- `key_ring_id`: The unique identifier for your key ring.\n",
"- `key_id`: The unique identifier for your key.\n",
"\n",
"The helper function calls the KMS client method `create_cryto_key()` to create your key.\n",
"\n",
"Learn more about [](https://cloud.google.com/kms/docs/samples/kms-create-key-symmetric-encrypt-decrypt)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "LXcagdmSnYYW"
},
"outputs": [],
"source": [
"KEY_ID = \"your_cmek_key_id\"\n",
"\n",
"\n",
"def create_key(project_id, location, key_ring_id, key_id):\n",
" \"\"\"\n",
" Creates a new symmetric encryption/decryption key in Cloud KMS.\n",
"\n",
" Args:\n",
" project_id (string): Google Cloud project ID (e.g. 'my-project').\n",
" location (string): Cloud KMS location (e.g. 'us-east1').\n",
" key_ring_id (string): ID of the Cloud KMS key ring (e.g. 'my-key-ring').\n",
" key_id (string): ID of the key to create (e.g. 'my-symmetric-key').\n",
"\n",
" Returns:\n",
" CryptoKey: Cloud KMS key.\n",
"\n",
" \"\"\"\n",
"\n",
" # Create the client.\n",
" client = kms.KeyManagementServiceClient()\n",
"\n",
" # Build the parent key ring name.\n",
" key_ring_name = client.key_ring_path(project_id, location, key_ring_id)\n",
"\n",
" # Build the key.\n",
" purpose = kms.CryptoKey.CryptoKeyPurpose.ENCRYPT_DECRYPT\n",
" algorithm = (\n",
" kms.CryptoKeyVersion.CryptoKeyVersionAlgorithm.GOOGLE_SYMMETRIC_ENCRYPTION\n",
" )\n",
" key = {\n",
" \"purpose\": purpose,\n",
" \"version_template\": {\n",
" \"algorithm\": algorithm,\n",
" },\n",
" }\n",
"\n",
" # Call the API.\n",
" created_key = client.create_crypto_key(\n",
" request={\"parent\": key_ring_name, \"crypto_key_id\": key_id, \"crypto_key\": key}\n",
" )\n",
" print(\"Created symmetric key: {}\".format(created_key.name))\n",
" return created_key\n",
"\n",
"\n",
"key_id = create_key(\n",
" project_id=PROJECT_ID, location=REGION, key_ring_id=KEY_RING_ID, key_id=KEY_ID\n",
")\n",
"\n",
"print(key_id)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "3gKDBOqC8Gl5"
},
"source": [
"### Set service account permissions\n",
"\n",
"Next, you set permissions for your Vertex AI service account to encrypt and decrypt resources using your key.\n",
"\n",
"Note: Compute Engine default service account which is used by this notebook instance for authentication purposes during Google API calls, should be granted the role of Cloud KMS Admin.\n",
"\n",
"Learn more about [Grant Vertex AI permissions](https://cloud.google.com/vertex-ai/docs/general/cmek#grant_permissions)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "6QrRg08Vqfru"
},
"outputs": [],
"source": [
"# Reference: https://cloud.google.com/vertex-ai/docs/general/cmek#granting_permissions\n",
"# Get the service account\n",
"SERVICE_ACCOUNT = ! gcloud projects get-iam-policy {PROJECT_ID} \\\n",
" --flatten=\"bindings[].members\" \\\n",
" --format=\"table(bindings.members)\" \\\n",
" --filter=\"bindings.role:roles/aiplatform.serviceAgent\" \\\n",
" | grep -oP \"service-.+?@gcp-sa-aiplatform.iam.gserviceaccount.com\"\n",
"SERVICE_ACCOUNT = SERVICE_ACCOUNT[0]\n",
"\n",
"print(f\"Service account is: {SERVICE_ACCOUNT}\")\n",
"\n",
"# Give permissions\n",
"! gcloud kms keys add-iam-policy-binding {KEY_ID} \\\n",
" --keyring={KEY_RING_ID} \\\n",
" --location={REGION} \\\n",
" --project={PROJECT_ID} \\\n",
" --member=serviceAccount:{SERVICE_ACCOUNT} \\\n",
" --role=roles/cloudkms.cryptoKeyEncrypterDecrypter"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "1e8cd37e5f99"
},
"source": [
"Create the full resource identifier for the created key"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "ebAHZg2vlhXL"
},
"outputs": [],
"source": [
"ENCRYPTION_SPEC_KEY_NAME = key_id.name"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Aa_8wrqSkamz"
},
"source": [
"## Initialize Vertex SDK for Python\n",
"\n",
"Initialize the *client* for Vertex AI\n",
"\n",
"All resources created during this Notebook run will encrypted with the encryption key created above.\n",
"\n",
"You can override the encryption key at each function call."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "init_aip:mbsdk,all"
},
"source": [
"### Initialize Vertex AI SDK for Python\n",
"\n",
"Initialize the Vertex AI SDK for Python for your project, bucket, and corresponding encryption key.\n",
"\n",
"All resources created during this session are encrypted with the encryption key you created.\n",
"\n",
"*Note:* You can override the encryption key at each function call."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "ohdgOs69kGNU"
},
"outputs": [],
"source": [
"aiplatform.init(\n",
" project=PROJECT_ID,\n",
" staging_bucket=BUCKET_URI,\n",
" location=REGION,\n",
" encryption_spec_key_name=ENCRYPTION_SPEC_KEY_NAME,\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "import_file:u_dataset,csv"
},
"source": [
"#### Location of Cloud Storage training data.\n",
"\n",
"Now set the variable `IMPORT_FILE` to the location of the CSV index file in Cloud Storage."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "import_file:flowers,csv,icn"
},
"outputs": [],
"source": [
"IMPORT_FILE = (\n",
" \"gs://cloud-samples-data/vision/automl_classification/flowers/all_data_v2.csv\"\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "35QVNhACqcTJ"
},
"source": [
"# Create `Vertex AI ImageDataset` resource\n",
"\n",
"Next, you create an `ImageDataset` resource, which will be encrypted using your encryption key."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "4OfCqaYRqcTJ"
},
"outputs": [],
"source": [
"dataset = aiplatform.ImageDataset.create(\n",
" display_name=\"flowers\",\n",
" gcs_source=[IMPORT_FILE],\n",
" import_schema_uri=aiplatform.schema.dataset.ioformat.image.single_label_classification,\n",
")\n",
"\n",
"print(dataset.resource_name)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "6-bBqipfqcTS"
},
"source": [
"# Launch a Training Job to Create a Model\n",
"\n",
"Train an AutoML Image Classification model."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "aA41rT_mb-rV"
},
"outputs": [],
"source": [
"job = aiplatform.AutoMLImageTrainingJob(\n",
" display_name=\"flowers\",\n",
" prediction_type=\"classification\",\n",
" multi_label=False,\n",
" model_type=\"CLOUD\",\n",
" base_model=None,\n",
")\n",
"\n",
"# This will take around half an hour to run\n",
"model = job.run(\n",
" dataset=dataset,\n",
" model_display_name=\"flowers\",\n",
" training_fraction_split=0.6,\n",
" validation_fraction_split=0.2,\n",
" test_fraction_split=0.2,\n",
" budget_milli_node_hours=8000,\n",
" disable_early_stopping=False,\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "5vhDsMJNqcTW"
},
"source": [
"# Deploy Your Model\n",
"\n",
"Deploy your model, then wait until the model FINISHES deployment before proceeding to prediction."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "Y9GH72wWqcTX"
},
"outputs": [],
"source": [
"endpoint = model.deploy()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "nIw1ifPuqcTb"
},
"source": [
"# Predict on Endpoint\n",
"- Take one sample from the data imported to the dataset\n",
"- This sample will be encoded to base64 and passed to the endpoint for prediction"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "H23ISHdHVIZM"
},
"outputs": [],
"source": [
"test_item = !gsutil cat $IMPORT_FILE | head -n1\n",
"test_item, test_label = str(test_item[0]).split(\",\")\n",
"\n",
"print(test_item, test_label)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "TF_N0kqZU768"
},
"outputs": [],
"source": [
"import base64\n",
"\n",
"import tensorflow as tf\n",
"\n",
"with tf.io.gfile.GFile(test_item, \"rb\") as f:\n",
" content = f.read()\n",
"\n",
"# The format of each instance should conform to the deployed model's prediction input schema.\n",
"instances_list = [{\"content\": base64.b64encode(content).decode(\"utf-8\")}]\n",
"\n",
"prediction = endpoint.predict(instances=instances_list)\n",
"\n",
"print(prediction)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "nWA3qocXfk82"
},
"source": [
"# Undeploy Model from Endpoint"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "V1brMaO_fk82"
},
"outputs": [],
"source": [
"endpoint.undeploy_all()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ba77c4e02355"
},
"source": [
"## Cleaning up\n",
"\n",
"To clean up all Google Cloud resources used in this project, you can [delete the Google Cloud\n",
"project](https://cloud.google.com/resource-manager/docs/creating-managing-projects#shutting_down_projects) you used for the tutorial.\n",
"\n",
"Otherwise, you can delete the individual resources you created in this tutorial:\n",
"\n",
"- Model\n",
"- Dataset\n",
"- Cloud Storage Bucket\n",
"- Endpoint"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "e00750837ca8"
},
"outputs": [],
"source": [
"# Delete endpoint resource\n",
"endpoint.delete()\n",
"\n",
"# Delete model resource\n",
"model.delete()\n",
"\n",
"# Delete dataset resource\n",
"dataset.delete()\n",
"\n",
"# Set this to true only if you'd like to delete your bucket\n",
"delete_bucket = False\n",
"\n",
"if delete_bucket or os.getenv(\"IS_TESTING\"):\n",
" ! gsutil rm -r $BUCKET_URI"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "d88c8a053a90"
},
"source": [
"## Destroying CMEK by providing key-version value and other parameters."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "5d8d2fc34346"
},
"outputs": [],
"source": [
"! gcloud kms keys versions destroy 1 \\\n",
" --key {KEY_ID} \\\n",
" --keyring={KEY_RING_ID} \\\n",
" --location={REGION} "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "a0670ffc741c"
},
"outputs": [],
"source": [
"KEY_ID = \"your_cmek_key_id\""
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "f7e42e642ad3"
},
"source": [
"## List of keys "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "aa95b7fff9b5"
},
"outputs": [],
"source": [
"! gcloud kms keys list --location {REGION} --keyring {KEY_RING_ID}"
]
}
],
"metadata": {
"colab": {
"collapsed_sections": [],
"name": "get_started_automl_cmek_training.ipynb",
"toc_visible": true
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}