src/kelp/__init__.py
def cleanup(args):
    """Remove resources created, s3 bucket, cloudtrail, stacks"""
    logger.info("Cleaning up all AWS resources used")
    s3_client = boto3.client("s3", region_name="us-east-1")
    s3 = boto3.resource("s3")

    # Cleanup logging bucket
    if s3.Bucket("kelp-bucket-logging") in s3.buckets.all():
        logger.info("Deleting logging bucket...")
        bucket = s3.Bucket("kelp-bucket-logging")
        bucket.objects.all().delete()
        response = s3_client.delete_bucket(Bucket="kelp-bucket-logging")

    # Cleanup cfn stacks for trail and accompanying logging bucket
    client = boto3.client("cloudformation", region_name="us-east-1")
    response = client.list_stacks(
        StackStatusFilter=[
            "ROLLBACK_COMPLETE",
            "CREATE_COMPLETE",
            "UPDATE_COMPLETE",
            "UPDATE_ROLLBACK_COMPLETE",
            "DELETE_FAILED",
        ]
    )
    if "kelp-bucket-stack" in [stack["StackName"] for stack in response["StackSummaries"]]:
        logger.info("Deleting logging bucket stack...")
        client.delete_stack(StackName="kelp-bucket-stack")
    if "kelp-trail-stack" in [stack["StackName"] for stack in response["StackSummaries"]]:
        logger.info("Deleting cloudtrail stack...")
        client.delete_stack(StackName="kelp-trail-stack")

    # Destroy application
    # NOTE - when this is in localstack I won't need to worry about this
    if s3.Bucket("kelp-bucket") in s3.buckets.all():
        logger.info("Deleting application bucket...")
        bucket = s3.Bucket("kelp-bucket")
        bucket.objects.all().delete()

    os.chdir(args.path)
    if args.debug:
        result = subprocess.run(["sls", "remove", "--stage=prod"])
    else:
        result = subprocess.run(["sls", "remove", "--stage=prod"], stdout=subprocess.DEVNULL)
    if result.returncode != 0:
        logger.exception("Failed to remove application from AWS")
        sys.exit()
    os.chdir("..")

    # Remove local files created
    files_in_directory = os.listdir(".")
    filtered_files = [file for file in files_in_directory if file.endswith(".gz")]
    for file in filtered_files:
        os.remove(file)

    print("Cleanup successful")
Remove resources created, s3 bucket, cloudtrail, stacks
Remove resources created, s3 bucket, cloudtrail, stacks
Remove resources created s3 bucket cloudtrail stacks
def cleanup(args):
    logger.info("Cleaning up all AWS resources used")
    s3_client = boto3.client("s3", region_name="us-east-1")
    s3 = boto3.resource("s3")
    if s3.Bucket("kelp-bucket-logging") in s3.buckets.all():
        logger.info("Deleting logging bucket...")
        bucket = s3.Bucket("kelp-bucket-logging")
        bucket.objects.all().delete()
        response = s3_client.delete_bucket(Bucket="kelp-bucket-logging")
    client = boto3.client("cloudformation", region_name="us-east-1")
    response = client.list_stacks(
        StackStatusFilter=[
            "ROLLBACK_COMPLETE",
            "CREATE_COMPLETE",
            "UPDATE_COMPLETE",
            "UPDATE_ROLLBACK_COMPLETE",
            "DELETE_FAILED",
        ]
    )
    if "kelp-bucket-stack" in [stack["StackName"] for stack in response["StackSummaries"]]:
        logger.info("Deleting logging bucket stack...")
        client.delete_stack(StackName="kelp-bucket-stack")
    if "kelp-trail-stack" in [stack["StackName"] for stack in response["StackSummaries"]]:
        logger.info("Deleting cloudtrail stack...")
        client.delete_stack(StackName="kelp-trail-stack")
    if s3.Bucket("kelp-bucket") in s3.buckets.all():
        logger.info("Deleting application bucket...")
        bucket = s3.Bucket("kelp-bucket")
        bucket.objects.all().delete()
    os.chdir(args.path)
    if args.debug:
        result = subprocess.run(["sls", "remove", "--stage=prod"])
    else:
        result = subprocess.run(["sls", "remove", "--stage=prod"], stdout=subprocess.DEVNULL)
    if result.returncode != 0:
        logger.exception("Failed to remove application from AWS")
        sys.exit()
    os.chdir("..")
    files_in_directory = os.listdir(".")
    filtered_files = [file for file in files_in_directory if file.endswith(".gz")]
    for file in filtered_files:
        os.remove(file)
    print("Cleanup successful")
def cleanup ( args ) : logger . info ( "Cleaning up all AWS resources used" ) s3_client = boto3 . client ( "s3" , region_name = "us-east-1" ) s3 = boto3 . resource ( "s3" ) if s3 . Bucket ( "kelp-bucket-logging" ) in s3 . buckets . all ( ) : logger . info ( "Deleting logging bucket..." ) bucket = s3 . Bucket ( "kelp-bucket-logging" ) bucket . objects . all ( ) . delete ( ) response = s3_client . delete_bucket ( Bucket = "kelp-bucket-logging" ) client = boto3 . client ( "cloudformation" , region_name = "us-east-1" ) response = client . list_stacks ( StackStatusFilter = [ "ROLLBACK_COMPLETE" , "CREATE_COMPLETE" , "UPDATE_COMPLETE" , "UPDATE_ROLLBACK_COMPLETE" , "DELETE_FAILED" , ] ) if "kelp-bucket-stack" in [ stack [ "StackName" ] for stack in response [ "StackSummaries" ] ] : logger . info ( "Deleting logging bucket stack..." ) client . delete_stack ( StackName = "kelp-bucket-stack" ) if "kelp-trail-stack" in [ stack [ "StackName" ] for stack in response [ "StackSummaries" ] ] : logger . info ( "Deleting cloudtrail stack..." ) client . delete_stack ( StackName = "kelp-trail-stack" ) if s3 . Bucket ( "kelp-bucket" ) in s3 . buckets . all ( ) : logger . info ( "Deleting application bucket..." ) bucket = s3 . Bucket ( "kelp-bucket" ) bucket . objects . all ( ) . delete ( ) os . chdir ( args . path ) if args . debug : result = subprocess . run ( [ "sls" , "remove" , "--stage=prod" ] ) else : result = subprocess . run ( [ "sls" , "remove" , "--stage=prod" ] , stdout = subprocess . DEVNULL ) if result . returncode != 0 : logger . exception ( "Failed to remove application from AWS" ) sys . exit ( ) os . chdir ( ".." ) files_in_directory = os . listdir ( "." ) filtered_files = [ file for file in files_in_directory if file . endswith ( ".gz" ) ] for file in filtered_files : os . remove ( file ) print ( "Cleanup successful" )
Remove resources created, s3 bucket, cloudtrail, stacks
Remove resources created s3 bucket cloudtrail stacks
"""Remove resources created, s3 bucket, cloudtrail, stacks""" # Cleanup logging bucket # Cleanup cfn stacks for trail and accompanying logging bucket # Destroy application # NOTE - when this is in localstack I won't need to worry about this # Remove local files created
args