npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@mobileposse/auto-delete-bucket

v1.56.0

Published

CDK Construct for automatically deleting bucket contents which allows the bucket to be deleted when the stack is destroyed.

Downloads

66

Readme

What it does

Creates an S3 bucket in Cloud Formation that will automatically be emptied before the bucket is destroyed by Cloud Formation.

:warning: Deprecation notice: AWS has included support for auto-deleting bucket contents upon bucket destroying in the AWS-CDK using the "autoDeleteObjects: true" BucketProps field. This is now the recommended way to handle this functionality.

How to use it

This is an AWS CDK Construct which makes it dead simple to use in your CDK code.

Just install with npm:

npm add @mobileposse/auto-delete-bucket

or yarn:

yarn add @mobileposse/auto-delete-bucket

And then require the construct and use it in your stack like any standard CDK resource!

import { AutoDeleteBucket } from '@mobileposse/auto-delete-bucket'

export class ExampleStack extends Stack {
  constructor(scope: App, id: string, props?: StackProps) {
    super(scope, id, props)

    /**
     * NOTE: S3 requires bucket names to be globally unique across accounts so
     * you will need to change the bucketName to something that nobody else is
     * using.
     */
    new AutoDeleteBucket(this, 'example-autobucket-1', {
      bucketName: 'autoexample-bucket1'
    })
  }
}

See the example directory for a complete CDK example. Be sure to change the bucketNames so they are unique.

The bucket can be configured with any of the standard CDK Bucket Properties.

Requirements

  • This is designed to work with AWS CDK but feel free to borrow the code if you want to create the custom CF resource some other way.
  • Does not yet work with versioned buckets but it can be easily adapted to do so (pull requests welcome.)

Versioning

Version numbers are consistent with the major and minor version numbers of the corresponding AWS CDK version that this module is compatible with. In other words, version 1.1.X would be compatiable with aws-cdk 1.1.X. Patch versions will inevitably vary between the two project but as long as you are using a version consistent with the major and minor version of the CDK version you are using you should be good.

Motivation

Cloud Formation will often fail to actually delete your S3 Bucket resources when you destroy your stack. This happens whenever the bucket is not empty as the Cloud Formation documentation clearly states:

You can only delete empty buckets. Deletion fails for buckets that have contents.

We find that in most of our use cases, we want to automatically delete the bucket and it's contents whenever the stack is deleted. Otherwise you will have a bunch of orphaned buckets to clean up manualy. The problem is even worse when you need to explicitly name the bucket (ex. for a website), because you won't be able to recreate the stack due to the fact that a bucket already exists with that name.

How it Works

Create a custom resource that will automatically delete your bucket contents before attempting bucket deletion.

Running Tests

yarn test

Building the Example Stack

The source code includes a reference CDK project inside the example directory which consists of a single auto-delete-bucket.

You can build the stack with:

yarn cdk:deploy

Go ahead and test this bucket out by adding some files to it. You can then test that everything will delete properly by destroying the stack (and bucket) with:

yarn cdk:destroy

Publish to NPM (Official maintainers only)

Add npm user to your local machine (one time setup)

npm adduser

Push the release (you will be asked the new version)

npm login

Then provide username and password. Once authenticaated use the following command:

npm publish --access public

Push the tagged source back up to Github

git push --tags

More Information

See the AWS documentation for more information on S3 and deleting bucket contents.