简体   繁体   中英

Using aws_s3_bucket_cors_configuration for multiple cors_rules with terraform aws provider version 4

In aws provider version 3, we defined the cors_rule in the aws_s3_bucket resource like this:

resource "aws_s3_bucket" "bucket" {
  ...

  dynamic "cors_rule" {
    for_each = var.cors_rule
    content {
      allowed_headers = lookup(cors_rule.value, "allowed_headers", [])
      allowed_methods = lookup(cors_rule.value, "allowed_methods", [])
      allowed_origins = lookup(cors_rule.value, "allowed_origins", [])
      expose_headers  = lookup(cors_rule.value, "expose_headers", [])
      max_age_seconds = lookup(cors_rule.value, "max_age_seconds", null)
    }
  }

  ...
}

That way, when we call the module, we can define several cors rules, and this resource would create them all.

We have tried many different ways to do this in version 4, and nothing is working.

How can we achieve this same outcome with the new aws_s3_bucket_cors_configuration in version 4?

It's a resource it will always be created by default but it requires those values to be created. The only way I could solve this is to conditionally create it and add the values.

resource "aws_s3_bucket_cors_configuration" "default" {
  count = local.enabled && var.cors_rule_inputs != null ? 1 : 0

  bucket = join("", aws_s3_bucket.default.*.id)

  dynamic "cors_rule" {
    for_each = var.cors_rule_inputs

    content {
      allowed_headers = cors_rule.value.allowed_headers
      allowed_methods = cors_rule.value.allowed_methods
      allowed_origins = cors_rule.value.allowed_origins
      expose_headers  = cors_rule.value.expose_headers
      max_age_seconds = cors_rule.value.max_age_seconds
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM