'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?
Solution 1:[1]
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
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | sysrex |
