Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terraform AWS optional logging for S3 bucket

I am trying to create S3 bucket using terraform from examples in the link https://www.terraform.io/docs/providers/aws/r/s3_bucket.html I have created a S3 module.

The issue i am facing is, for certain bucket i do not want logging enabled. How can this be accomplished in terraform.

 logging {
        target_bucket = "${aws_s3_bucket.log_bucket.id}"
        target_prefix = "log/"   
}

Using empty string for target_bucket and target_prefix causes terraform to make an attempt to create target_bucket.

Also, i am trying to use a module.

like image 338
Arpan Solanki Avatar asked Oct 20 '25 14:10

Arpan Solanki


1 Answers

Using the newer dynamic block support in terraform 0.12+ we pass a single-item array containing the logging settings if we want logging like so:

variable "logging" {
  type        = list
  default     = []
  description = "to enable logging set this to [{target_bucket = 'xxx' target_prefix = 'logs/'}]"
}

resource "aws_s3_bucket" "s3bucket" {
  dynamic "logging" {
    for_each = [for l in var.logging : {
      target_bucket = l.target_bucket
      target_prefix = l.target_prefix
    }]
    content {
      target_bucket = logging.value.target_bucket
      target_prefix = logging.value.target_prefix
    }
  }
}
like image 128
nachonachoman Avatar answered Oct 22 '25 05:10

nachonachoman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!