{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Personalize Workshop Cleanup - Lab 9 (optional)\n", "\n", "This notebook will walk through deleting all of the resources created by the Personalization Labs in this workshop. You should only need to perform these steps if you have deployed the Retail Demo Store in your own AWS account and want to deprovision the Personalize resources. If you are participating in an AWS-led workshop, this process is likely not necessary.\n", "\n", "Resources have to deleted in a specific sequence to avoid dependency errors. In order, we will delete recommenders and campaigns, solutions, event trackers, filters, datasets, and the dataset group. In addition, we need to make sure that each resource type is fully deleted before moving on to the next resource type. We'll also delete the schemas for our datasets and reset the SSM parameter values that are used by the Recommendations service and Web UI for personalization features.\n", "\n", "We will be leveraging a utility module written on python that provides an orderly delete process for deleting all resources in each dataset group." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Import depedencies and adjust path\n", "\n", "The following code cell ensures that we are working with the latest version of the boto3 library and that the path is updated so we can load the `delete_dataset_groups` module." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import logging\n", "import os\n", "import sys\n", "\n", "!{sys.executable} -m pip install --upgrade pip\n", "!{sys.executable} -m pip install --upgrade --no-deps --force-reinstall botocore\n", "sys.path.insert(0, os.path.abspath('../../src/aws-lambda/personalize-pre-create-resources'))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Reset SSM parameters\n", "\n", "Before deleting resources, we will reset the SSM parameter values so that the Recommendations service will no longer attempt to make requests to deleted resources." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import boto3\n", "\n", "param_names = [\n", " '/retaildemostore/personalize/event-tracker-id'\n", " '/retaildemostore/personalize/filters/filter-purchased-arn',\n", " '/retaildemostore/personalize/filters/filter-cstore-arn',\n", " '/retaildemostore/personalize/filters/filter-purchased-and-cstore-arn',\n", " '/retaildemostore/personalize/filters/promoted-items-filter-arn',\n", " '/retaildemostore/personalize/filters/promoted-items-no-cstore-filter-arn',\n", " '/retaildemostore/personalize/recommended-for-you-arn',\n", " '/retaildemostore/personalize/popular-items-arn',\n", " '/retaildemostore/personalize/related-items-arn',\n", " '/retaildemostore/personalize/personalized-ranking-arn',\n", " '/retaildemostore/personalize/personalized-offers-arn'\n", "]\n", "\n", "ssm = boto3.client('ssm')\n", "\n", "for param_name in param_names:\n", " ssm.put_parameter(\n", " Name=param_name,\n", " Value='NONE',\n", " Type='String',\n", " Overwrite=True\n", " )\n", " \n", "print('SSM parameters have been reset')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Prepare and delete dataset groups\n", "\n", "We are now ready to delete the active workshop dataset groups created by the labs involving Amazon Personalize. The `delete_dataset_groups` function will delete all dependent resources within the dataset groups starting with recommenders and campaigns, then solutions and datasets, filters and event trackers, and ending with schemas and the dataset groups themselves." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Identify active workshop dataset groups\n", "\n", "Let's start by checking the active dataset groups in the current account and check which of the two potential dataset groups are active. This will tell us which dataset group(s) need to be deleted." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "personalize = boto3.client('personalize')\n", "\n", "# Dataset group names that may have been created by workshop labs.\n", "possible_dataset_groups = [\n", " 'retaildemostore-products',\n", " 'retaildemostore-offers'\n", "]\n", "\n", "# Actual workshop dataset groups that exist. The logic below will add to this list.\n", "active_dataset_groups = []\n", "\n", "# Check dataset groups to see which workshop dataset groups were actually created.\n", "paginator = personalize.get_paginator('list_dataset_groups')\n", "for paginate_result in paginator.paginate():\n", " for dataset_group in paginate_result[\"datasetGroups\"]:\n", " if dataset_group['name'] in possible_dataset_groups:\n", " active_dataset_groups.append(dataset_group['name'])\n", "\n", "print(f'Active workshop dataset groups that need to be deleted: {active_dataset_groups}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Prepare logging output\n", "\n", "Next we will import the [delete_dataset_groups](../../src/aws-lambda/personalize-pre-create-resources/delete_dataset_groups.py) module and update the logging in the module so we can see its output here in the notebook." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import delete_dataset_groups" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "handler = logging.StreamHandler(sys.stdout)\n", "handler.setLevel(logging.INFO)\n", "\n", "delete_dataset_groups.logger.setLevel(logging.INFO)\n", "delete_dataset_groups.logger.addHandler(handler)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Delete dataset groups\n", "\n", "Now we can delete the active workshop dataset groups. This can take several minutes depending on the number of dataset groups and resources within each dataset group. The function below will log its progress until finished." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%time\n", "\n", "delete_dataset_groups.delete_dataset_groups(\n", " dataset_group_names = active_dataset_groups, \n", " wait_for_resources = True\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Cleanup Complete\n", "\n", "All resources created by the Personalize workshop(s) have been deleted." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "conda_python3", "language": "python", "name": "conda_python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.12" } }, "nbformat": 4, "nbformat_minor": 4 }