Home > Software design >  Multi S3 bucket Definition with versioning limitations
Multi S3 bucket Definition with versioning limitations

Time:03-02

I was reading this post: Terraform - creating multiple buckets

and was wondering how could I have added a filter to enable bucket versioning on one of the buckets and disable versioning on the rest of the buckets using terraform conditionals or anything that would allow it to work? I was trying something like this but it is not working

variable "s3_bucket_name" {
  type    = "list"
  default = ["prod_bucket", "stage-bucket", "qa_bucket"]
}

resource "aws_s3_bucket" "henrys_bucket" {
  count         = "${length(var.s3_bucket_name)}"
  bucket        = "${var.s3_bucket_name[count.index]}"
  acl           = "private"
  force_destroy = "true"
  var.s3_bucket_name[count.index] != "target-bucket-name" versioning { enabled = true } : versioning { enabled = false }
}

CodePudding user response:

You can use list of objects instead of just list of bucket names. The object can contain bucket name, and versioning_enabled flag. Then use the bucket-name and versioning_enabled.

Something like:

bucket        = var.s3_buckets[count.index].bucket_name 

And for versioning, add dynamic block based on var.s3_buckets[count.index].versioning_enabled like below:

dynamic "versioning" {
  for_each = var.s3_buckets[count.index].versioning_enabled== true ? [1] : []
  content {
    enabled = true
  }
}
  • Related