{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Clean up\n", "❗ This notebook removes all resources created by a data transformation and ingestion project. The following code cells will:\n", "- permanently delete project or projects you provisioned in your Studio environment\n", "- permanently delete feature group or groups\n", "- permanently delete project-provisioned S3 buckets\n", "- permanently delete objects in S3 buckets under project-related prefixes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
💡 This notebook will delete resources in your AWS account. Please double check the names of resources to be deleted! \n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import sagemaker\n", "import boto3\n", "import time\n", "import json\n", "import os" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# load environment variables from %store\n", "%store -r " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%store" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "try:\n", " s3_data_prefix\n", " s3_flow_prefix\n", " s3_input_data_prefix\n", " s3_fs_query_output_prefix\n", "except NameError:\n", " print(\"+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\")\n", " print(\"[ERROR] YOU HAVE TO RUN 00-setup.ipynb and 01-feature-store-ingest-pipeline notebooks\")\n", " print(\"+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sm = boto3.client(\"sagemaker\")\n", "s3 = boto3.resource('s3')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Delete projects" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Get all projects created by the current domain\n", "projects = [\n", " {\"ProjectName\":p[\"ProjectName\"], \"ProjectId\":p[\"ProjectId\"]} for p in sm.list_projects(MaxResults=100, SortBy=\"CreationTime\")[\"ProjectSummaryList\"] \n", " if sm.describe_project(ProjectName=p[\"ProjectName\"])[\"CreatedBy\"][\"DomainId\"] == domain_id and p[\"ProjectStatus\"] == \"CreateCompleted\"\n", "]\n", "\n", "print(f\"These projects have been created by domain {domain_id}: {json.dumps(projects, indent=2)}\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Select projects to be deleted\n", "projects_to_delete = []\n", "\n", "for p in projects:\n", " print(f\"Are you sure you want to delete this project: {p['ProjectName']}? (y/n)\")\n", " choice = input()\n", " if choice == 'y':\n", " projects_to_delete.append(p)\n", " \n", "print(f\"***************************************\")\n", "print(f\"The following projects will be deleted:\\n{json.dumps(projects_to_delete, indent=2)}\")\n", "print(f\"***************************************\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
💡 The following code cell will delete all selected projects. All project CodeCommit code repositories and CI/CD CodePipeline pipelines will be deleted! \n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for p in projects_to_delete:\n", " try:\n", " print(f\"Deleting project {p['ProjectName']}:{sm.delete_project(ProjectName=p['ProjectName'])}\")\n", " except Exception:\n", " pass" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Delete feature groups" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "feature_groups = sm.list_feature_groups(\n", " FeatureGroupStatusEquals=\"Created\", \n", " SortOrder=\"Descending\", \n", " SortBy=\"CreationTime\"\n", ")[\"FeatureGroupSummaries\"]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "feature_groups" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Select feature groups to be deleted\n", "feature_groups_to_delete = []\n", "\n", "for fg in feature_groups:\n", " print(f\"Are you sure you want to delete this feature group: {fg['FeatureGroupName']}? (y/n)\")\n", " choice = input()\n", " if choice == 'y':\n", " feature_groups_to_delete.append(fg[\"FeatureGroupName\"])\n", " \n", "print(f\"********************************************\")\n", "print(f\"The following feature groups will be deleted:\\n{json.dumps(feature_groups_to_delete, indent=2)}\")\n", "print(f\"********************************************\")\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def delete_offline_store(feature_group_name: str):\n", " try:\n", " offline_store_config = sm.describe_feature_group(FeatureGroupName=feature_group_name)['OfflineStoreConfig']\n", "\n", " except Exception:\n", " print(f'Feature group: {feature_group_name} does NOT have an offline store!')\n", " return\n", " \n", " offline_store_s3_uri = offline_store_config['S3StorageConfig']['ResolvedOutputS3Uri']\n", " print(f\"all feature store objects under {offline_store_s3_uri} will be deleted!\")\n", " print(\"Are you sure you want to these objects ? (y/n)\")\n", " \n", " choice = input()\n", " if choice == 'y':\n", " !aws s3 rm {offline_store_s3_uri} --recursive" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
💡 The following code cell will delete the selected feature groups!\n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for fg in feature_groups_to_delete:\n", " print(f\"Deleting the feature group: {fg}\")\n", " delete_offline_store(fg)\n", " sm.delete_feature_group(FeatureGroupName=fg)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Delete project-provisioned S3 buckets" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
💡 The following code cell will delete all S3 buckets created by a project!\n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(f\"*****************************************************\")\n", "print(f\"The following S3 buckets will be removed permanently!\")\n", "print(f\"*****************************************************\")\n", "for p in projects_to_delete:\n", " print(f\"sagemaker-cp-{p['ProjectName']}-{p['ProjectId']}\")\n", " print(f\"sagemaker-ct-{p['ProjectName']}-{p['ProjectId']}\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for p in projects_to_delete:\n", " !aws s3 rb s3://sagemaker-cp-{p['ProjectName']}-{p['ProjectId']} --force \n", " !aws s3 rb s3://sagemaker-ct-{p['ProjectName']}-{p['ProjectId']} --force" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Remove project-related objects from S3 data bucket" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
💡 The following code cells will delete all objects under specified S3 prefixes!\n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "prefixes_to_delete = [\n", " s3_data_prefix,\n", " s3_flow_prefix,\n", " s3_input_data_prefix,\n", " s3_fs_query_output_prefix\n", "]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(f\"************************************************************************\")\n", "print(f\"All objects under the following S3 prefixes will be removed permanently!\")\n", "print(f\"************************************************************************\")\n", "for p in prefixes_to_delete:\n", " print(p)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for p in prefixes_to_delete:\n", " !aws s3 rm s3://{p} --recursive" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Release resources" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%html\n", "\n", "

Shutting down your kernel for this notebook to release resources.

\n", "\n", " \n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Continue with clean up as documented in [README](../README.md#clean-up)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3.9.12 64-bit", "language": "python", "name": "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.9.12" }, "vscode": { "interpreter": { "hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49" } } }, "nbformat": 4, "nbformat_minor": 4 }