7
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Terraformでdestroyするときに「BucketNotEmpty」が出たときの対処法

Posted at

S3バケットに対してdestroyしたときに

BucketNotEmpty: The bucket you tried to delete is not empty

とか言われて怒られることが結構あるんですよね

対処(CLI)

メッセージの通りなんですが、
S3バケットの中身が空でない(NotEmpty)の状態でバケット自体削除できないわけです。

なので、CLIで消してしまうというのが一つの手

aws s3 rm s3://bucket-name --recursive

いちいち消すのが面倒くさい(tfでやりたいときは)ときは下記の通り

対処(Terraform)

resource "aws_s3_bucket" "default" {
  bucket        = "bucket-name"
}

resource "null_resource" "default" {
  triggers = {
    bucket   = aws_s3_bucket.default.bucket
  }
  depends_on = [
    aws_s3_bucket.default
  ]
  provisioner "local-exec" {
    when = destroy
    command = "aws s3 rm s3://${self.triggers.bucket} --recursive"
  }
}

解説
null_resourceブロックをつかって
depends_onで依存関係を作る

local-execwhen=destroyで実行するタイミングをdestroy時だけに限定

CLIでS3の中身を消す

いじょ!

7
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?