使用 for_each 循环按预定义顺序创建 aws_ses_receipt_rule

Lui*_*ñiz 5 terraform terraform-provider-aws

我正在尝试使用变量中的规则集合定义的顺序来定义 SES 规则集。

我已经尝试过https://github.com/hashicorp/terraform-provider-aws/issues/24067中的解决方案来使用after资源的属性,它确实创建了第一个规则,但在创建第二个规则时失败了,并且所有后续规则,因为第一个规则尚不存在(after=null 的规则)。我想这需要一些时间来完成。depends_on据我所知,不适用于动态依赖项,所以这也不会成功。

如果我重新运行应用,则会创建第二条规则,但所有其他规则都会失败。

我的recipients_by_position地图由 0 填充位置索引(即“01”、“02”等):

这是我的代码

locals {

  recipients = {
    "mail1-recipient1" = {
      short_name    = "mail1"
      domain        = "mail1.domain.com"
      recipient     = "recipient1"
      position      = 1
      target_bucket = "bucket1"
    }
    "mail1-recipient2" = {
      short_name    = "mail1"
      domain        = "mail1.domain.com"
      recipient     = "recipient2"
      position      = 2
      target_bucket = "bucket1"
    }
    "mail2-recipient1" = {
      short_name    = "mail2"
      domain        = "mail2.domain.com"
      recipient     = "recipient1"
      position      = 3
      target_bucket = "bucket2"
    }
  }
  spec_by_domain = {
    "mail1.domain.com" = {
      irrelevant ={}
    }
    "mail2.domain.com" = {
      irrelevant ={}
    }
  }

  recipients_by_position = {for r in local.recipients: "${format("%02s",r.position)}" => r}
}

resource "aws_ses_domain_identity" "domains" {
  for_each = local.spec_by_domain
  domain = each.key
}

resource "aws_ses_receipt_rule_set" "main" {
  rule_set_name = "new-rules"
}


# store it in S3
resource "aws_ses_receipt_rule" "store" {
  for_each = local.recipients_by_position
  after   = each.value.position == 1 ? null : "${format("%02s",each.value.position - 1)}"

#  name          = "${each.value.short_name}-store_on_s3-${each.value.recipient}"
  name          = each.key
  rule_set_name = aws_ses_receipt_rule_set.main.rule_set_name
  recipients    =  ["${each.value.recipient}@${each.value.domain}"]
  enabled       = true
  scan_enabled  = true

  s3_action {
    bucket_name = aws_s3_bucket.mailboxes[each.value.domain].bucket
    object_key_prefix = each.value.recipient
    position    = 1
  }
}

Run Code Online (Sandbox Code Playgroud)

apply 失败并出现一堆

Error: Error creating SES rule: RuleDoesNotExist: Rule does not exist: xx
Run Code Online (Sandbox Code Playgroud)

xx 从 01 到定义的任意数量的规则