Skip to main content

AWS CDK package that empties an S3 bucket upon resource deletion.

Project description

AWS Empty Bucket

A custom S3 bucket with an ability to completely delete itself (even if it contains files within).

Remarks

The project is written by Laimonas Sutkus and is owned by iDenfy. This is an open source library intended to be used by anyone. iDenfy aims to share its knowledge and educate market for better and more secure IT infrastructure.

Related technology

This project utilizes the following technology:

  • AWS (Amazon Web Services).
  • AWS CDK (Amazon Web Services Cloud Development Kit).
  • AWS CloudFormation.
  • AWS S3 (Amazon Web Services Simple Storage Service).

Assumptions

This library project assumes the following:

  • You have knowledge in AWS (Amazon Web Services).
  • You have knowledge in AWS CloudFormation and AWS S3.
  • You are managing your infrastructure with AWS CDK.
  • You are writing AWS CDK templates with a python language.

Install

The project is built and uploaded to PyPi. Install it by using pip.

pip install aws-empty-bucket

Or directly install it through source.

./build.sh -ic

Description

Natively S3 buckets can not be deleted if they contain files. If you were to delete a bucket through CloudFormation, you would get a similar error message:

The bucket you tried to delete is not empty (Service: Amazon S3; Status Code: 409; Error Code: BucketNotEmpty; Request ID: ; S3 Extended Request ID: )

This gets especially annoying if a developer is spinning up and tearing down the infrastructure many times a day. Wouldn't it be awesome if S3 buckets could just be simply deleted in any case?

With this project you can create S3 buckets that can be deleted even if they contain filed inside. A project exposes a class EmptyS3Bucket which can be used exactly the same as a class Bucket provided by AWS CDK. Next time you delete your stack, you will not see that error message again.

Examples

To create an S3 Bucket that can be easily deleted create an EmptyS3Bucket instance in your stack. An example is given below:

from aws_cdk import core, aws_s3
from aws_empty_bucket.empty_s3_bucket import EmptyS3Bucket

class MainStack(core.Stack):
    def __init__(self, scope: core.App) -> None:
        super().__init__(
            scope=scope,
            id='MyCoolStack'
        )

        self.empty_bucket = EmptyS3Bucket(
            self,
            'MyCoolBucketThatCanBeDeleted',
            access_control=aws_s3.BucketAccessControl.PRIVATE,
            bucket_name='mybucket',
        )

To delete inner S3 Bucket files, a custom resource with a lambda function as as a backend is created too. EmptyS3Bucket exposes two properties: backend and custom_resource. If you need to access them use the following:

from aws_empty_bucket.empty_s3_bucket import EmptyS3Bucket

empty_bucket = EmptyS3Bucket(...)

function = empty_bucket.backend
resource = empty_bucket.custom_resource

Release history

2.0.1

Do not use singleton lambdas since we are using roles for specific buckets.

2.0.0

Make a custom S3 bucket with inner-file deletion capabilities.

1.0.0

Initial project. Not tested. No extensive readme.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aws_empty_bucket-2.0.1.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aws_empty_bucket-2.0.1-py3-none-any.whl (19.5 kB view details)

Uploaded Python 3

File details

Details for the file aws_empty_bucket-2.0.1.tar.gz.

File metadata

  • Download URL: aws_empty_bucket-2.0.1.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.6.8

File hashes

Hashes for aws_empty_bucket-2.0.1.tar.gz
Algorithm Hash digest
SHA256 c967470a4a2e247a117ea44c9e3a8c0fd5ee0fc793e55b10db7710168890e45e
MD5 8e824828dd69c6a97fa5e174ec36628f
BLAKE2b-256 9c7cc901aba39f37e023de24389c199bcca7b90b9fa9a22be0ab4dc53f6debdb

See more details on using hashes here.

File details

Details for the file aws_empty_bucket-2.0.1-py3-none-any.whl.

File metadata

  • Download URL: aws_empty_bucket-2.0.1-py3-none-any.whl
  • Upload date:
  • Size: 19.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.6.8

File hashes

Hashes for aws_empty_bucket-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 71c9967f241170e090324999639a69f7b31ef43f08e85e774a01d13a32d675c4
MD5 87ee52b16d4b0bb75b564f7424eabc97
BLAKE2b-256 50bdc948c6434028a68ccc416887ef4b3fd3d04ee5bdab5a6946cc538e2d5961

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page