Terraform-provider-aws: s3 logging as variable

Created on 28 Dec 2017  ยท  6Comments  ยท  Source: hashicorp/terraform-provider-aws

Hello,

I'm trying to create generic S3 module so I can toggle S3 features on the referenced modules.
But I run out into this error which is not supposed to happen in the first place, because the logging type is map.

Terraform Version

Terraform v0.11.1
+ provider.aws v1.6.0

Affected Resource(s)

  • aws_s3_bucket

Terraform Configuration Files

variable "logging" {
  default = {}
}

resource "aws_s3_bucket" "s3_foo" {
  bucket = "foo"

  logging = "${var.logging}"
}

Debug Output

Error: aws_s3_bucket.s3_bucket: logging: should be a list

Expected Behavior

It should pass and get the logging arguments from logging variable.

Actual Behavior

Getting an error because of the variable type.

Steps to Reproduce

  1. terraform plan

Most helpful comment

Hi @AliGrab! @loivis has the correct idea here - the syntax of HCL makes this appear like a map, but in fact is not. If you don't want to expose this to your module consumers, you could put the list in the assignment rather than the variable, too:

logging = ["${var.logging}"]

...

variable "logging" {
  default = {
    target_bucket = "target_bucket1"
    target_prefix = "target_prefix1"
  }
}

All 6 comments

It should be a set of logging from code:
https://github.com/terraform-providers/terraform-provider-aws/blob/dddc588b53d0de7421040313d2ec7d54ca55bd4a/aws/resource_aws_s3_bucket.go#L185
Instead of using variable, try to set logging directly and plan will show this a bit more clear that it's really not a map.

      logging.#:                        "1"
      logging.3062530799.target_bucket: "target_bucket1"
      logging.3062530799.target_prefix: "target_prefix1"

So changing variable type to list should make it right.

variable "logging" {
  default = [{
    target_bucket = "target_bucket1"
    target_prefix = "target_prefix1"
  }]
}

Hi @AliGrab! @loivis has the correct idea here - the syntax of HCL makes this appear like a map, but in fact is not. If you don't want to expose this to your module consumers, you could put the list in the assignment rather than the variable, too:

logging = ["${var.logging}"]

...

variable "logging" {
  default = {
    target_bucket = "target_bucket1"
    target_prefix = "target_prefix1"
  }
}

Thank you @loivis and @jen20 for your help.
Really appreciate it.

Is this possible to enable/disable logging based on the value of variable say is_logging_enabled?

Really can't figure this out. Need help!

@goforgold I realize this is a very late answer, maybe it helps someone else.

dynamic "logging" {
  for_each = var.is_logging_enabled == true ? [1] : []
  content {
    target_bucket = "your-bucket-name"
    target_prefix = "s3/${var.name}/"
  }
}

I'm going to lock this issue because it has been closed for _30 days_ โณ. This helps our maintainers find and focus on the active issues.

If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks!

Was this page helpful?
0 / 5 - 0 ratings