mdn / stumptown-deployer

🚢 those static Stumptown sites
Mozilla Public License 2.0
4 stars 6 forks source link

Create S3 buckets with short lifetime #2

Closed peterbe closed 5 years ago

peterbe commented 5 years ago

Most of the S3 websites we're going to create with this repo are going to be short-lived. This stuff is cheap so we can perhaps have, at least, 30 days or something. However, it should be possible to override this. For example, we might have a post-successful-build in TravisCI that runs if it's a build on the master branch.

peterbe commented 5 years ago

I don't think S3 buckets can be short-lived. But the objects put into it can. So, given enough time, there'll exist buckets that have no content in them and then they can be deleted. Something like this:

for bucket in s3_client.get_buckets():
    for key in s3_client.list_objects_v2(Bucket=bucket.name, MaxKeys=1):
        break
    else:
        assert bucket.name.startswith('stumptown-')       
        print(f"{bucket.name} has nothing in it!")
       s3_client.delete_bucket(Bucket=bucket.name)