{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Cleanup\n", "\n", "After building completing the notebooks you may want to delete the following to prevent any unwanted charges:\n", "\n", "* Forecast Exports\n", "* Forecasts\n", "* Predictors\n", "* Datasets\n", "* Dataset Groups\n", "\n", "The code snippets below will cover the base use case of creating items in notebooks 1 - 3. You can expand upon this to delete content created in other notebooks.\n", "\n", "## Imports and Connecting to AWS\n", "\n", "The following lines import all the necessary libraries and then connect you to Amazon Forecast.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import sys\n", "import os\n", "import json\n", "import time\n", "import boto3\n", "import pandas as pd\n", "import botocore.exceptions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The line below will retrieve your shared variables from the earlier notebooks." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%store -r" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Once again connect to the Forecast APIs via the SDK." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "session = boto3.Session(region_name=region) \n", "forecast = session.client(service_name='forecast') \n", "forecastquery = session.client(service_name='forecastquery')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Defining the Things to Cleanup\n", "\n", "In the previous notebooks you stored several variables at the end of each, now that they have been retrieved above, the cells below will delete the items that were created one at a time until all items that were created have been removed." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def wait_till_delete(callback, check_time = 5, timeout = None):\n", "\n", " elapsed_time = 0\n", " while timeout is None or elapsed_time < timeout:\n", " try:\n", " out = callback()\n", " except botocore.exceptions.ClientError as e:\n", " # When given the resource not found exception, deletion has occured\n", " if e.response['Error']['Code'] == 'ResourceNotFoundException':\n", " print('Successful delete')\n", " return\n", " else:\n", " raise\n", " time.sleep(check_time) # units of seconds\n", " elapsed_time += check_time\n", "\n", " raise TimeoutError( \"Forecast resource deletion timed-out.\" )" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Delete the Exports\n", "exports = forecast.list_forecast_export_jobs()\n", "exports = [deeparp_export_forecast_arn, arima_export_forecast_arn, prophet_export_forecast_arn]\n", "for job in exports:\n", " forecast.delete_forecast_export_job(ForecastExportJobArn=job)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Delete the Foreacst:\n", "wait_till_delete(lambda: forecast.delete_forecast(ForecastArn=prophet_forecast_arn))\n", "wait_till_delete(lambda: forecast.delete_forecast(ForecastArn=arima_forecast_arn))\n", "wait_till_delete(lambda: forecast.delete_forecast(ForecastArn=deeparp_forecast_arn))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Delete the Predictor:\n", "wait_till_delete(lambda: forecast.delete_predictor(PredictorArn=arima_arn))\n", "wait_till_delete(lambda: forecast.delete_predictor(PredictorArn=prophet_arn))\n", "wait_till_delete(lambda: forecast.delete_predictor(PredictorArn=deeparp_arn))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Delete Import\n", "wait_till_delete(lambda: forecast.delete_dataset_import_job(DatasetImportJobArn=ds_import_job_arn))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Delete the Dataset:\n", "wait_till_delete(lambda: forecast.delete_dataset(DatasetArn=target_datasetArn))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Delete the DatasetGroup:\n", "wait_till_delete(lambda: forecast.delete_dataset_group(DatasetGroupArn=datasetGroupArn))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Delete your file in S3\n", "s3 = boto3.resource('s3')\n", "bucket = s3.Bucket(bucket_name)\n", "bucket.objects.all().delete()\n", "# Delete the bucket\n", "bucket.delete()\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "All that remains to cleanup here is to now go back to the CloudFormation console and delete the stack. You have successfully removed all resources that were created." ] } ], "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 }