Open1

Terraformの`dynamic`の挙動

yDog-1yDog-1
resource "aws_s3_bucket_cors_configuration" "web" {
  count = length(var.cors_rules) > 0 ? 1 : 0

  bucket = aws_s3_bucket.web.id

  dynamic "cors_rule" {
    for_each = var.cors_rules

    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"]
    }
  }
}

このときのdynamic "cors_rule"aws_s3_bucket_cors_configurationリソースのブロックのひとつ。
for_each = var.cors_rulesによってvar.cors_rulesの各要素がcors_rule変数に自動で格納される。

https://developer.hashicorp.com/terraform/tutorials/modules/module-object-attributes

https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_cors_configuration