Last active
April 9, 2021 15:39
-
-
Save bassmanitram/53a57988d9f4e4a2ffc94aed789bbf9e to your computer and use it in GitHub Desktop.
Workaround for Terraform taking a loooooong time to delete a bucket with many objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# A TERRAFORM local_exec provisioner to remove all the content from a bucket because | |
# Terraform is so bloody slow doing it | |
# | |
# Arrange for this script to be executed before the bucket is deleted. | |
# | |
# ALSO you must arrange for AWS CLI credentials to available to this script | |
# | |
# ONE way to do that is to run the terraform command in the context of AWS CLI | |
# credentials too, setting the standard AWS CLI environment variables, plus the | |
# AWS_SDK_LOAD_CONFIG set to true. | |
# | |
# Note also that this is unlikely to work on a bucket with object versioning enabled | |
# | |
BUCKET_ID=${1:-$BUCKET_ID} | |
aws s3 rm "s3://${BUCKET_ID}/" --recursive --only-show-errors || echo "WARNING: S3 rm ${BUCKET_ID} reported errors" >&2 | |
# | |
# We exit with success whether or not the CLI command ended with success.This then | |
# allows the normal TF clearance algorithm to to clean up as a fallback. | |
# | |
exit 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Handle the bucket itself | |
# Use force_destroy even with the workaround so we delete anything | |
# for which the workaround script experiences an error deleting | |
# | |
resource "aws_s3_bucket" "my_s3_bucket" { | |
bucket = local.bucket_name | |
force_destroy = true | |
... | |
# | |
# The content remover destroy time provisioner script | |
# | |
# On destroy, runs the script to clear the bucket content before the bucket itself is destroyed | |
# | |
provisioner "local-exec" { | |
when = destroy | |
command = "./remove-content.sh" | |
interpreter = [ "sh" ] | |
working_dir = path.module | |
environment = { | |
BUCKET_ID = self.id | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment