{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Part 4 - Resources clean up\n", "\n", "This part of the lab is meant to help you clean up the resources we've created during set up notably.\n", "\n", "These are the resources that will get deleted by executing this notebook:\n", "- SNS policy\n", "- SNS topic\n", "- S3 bucket policy\n", "- S3 policy\n", "- the created S3 bucket (not yours) including all its content\n", "- IAM role that was used by Rekognition notably\n", "\n", "IMPORTANT: Please note that the Amazon Neptune GraphDB and the notebook instance you're using to run this code will NOT be deleted by this notebook. Please delete them if you're not planning to use it in order to not incur additional costs." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import boto3\n", "s3 = boto3.client('s3')\n", "sns = boto3.client('sns')\n", "iam = boto3.client(\"iam\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#load stored variable from lab0 notebook\n", "%store -r" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## SNS Policy deletion" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#detaching policy from role first\n", "iam.detach_role_policy(\n", " RoleName=role_name,\n", " PolicyArn=sns_policy_arn\n", ")\n", "#deleting the policy\n", "iam.delete_policy(PolicyArn=sns_policy_arn)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## SNS Topic deletion" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sns.delete_topic(TopicArn=sns_topic_arn)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## S3 Policy deletion" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#detaching policy from role first\n", "iam.detach_role_policy(\n", " RoleName=role_name,\n", " PolicyArn=s3_policy_arn\n", ")\n", "#deleting the policy\n", "iam.delete_policy(PolicyArn=s3_policy_arn)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## S3 bucket deletion" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#deleting all files within the input folder in the bucket\n", "response = s3.list_objects_v2(Bucket=bucket)\n", "for object in response['Contents']:\n", " print('Deleting', object['Key'])\n", " s3.delete_object(Bucket=bucket, Key=object['Key'])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#delete the bucket\n", "s3.delete_bucket(Bucket=bucket)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Role deletion" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#delete role\n", "iam.delete_role(RoleName=role_name)" ] } ], "metadata": { "instance_type": "ml.t3.medium", "kernelspec": { "display_name": "Python 3 (Data Science)", "language": "python", "name": "python3__SAGEMAKER_INTERNAL__arn:aws:sagemaker:ap-southeast-2:452832661640:image/datascience-1.0" }, "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.7.10" } }, "nbformat": 4, "nbformat_minor": 5 }