不断收到 S3 复制的 Terraform 错误

Keep getting Terraform Error for S3 Replication

我正在尝试 运行 terraform 中的 s3 复制,这将是跨区域的。我的大部分代码都很好,但我只收到一些我似乎无法解决的错误。

我的部分主要s3.tf是

resource "aws_kms_key" "s3_replica-us-west-2-key" {
  description             = "S3 master key replica us-west-2"
  deletion_window_in_days = 30
  enable_key_rotation     = "true"
}

module "s3_replica" {
  source = "git@github.com:xxx"

  providers = {
    aws     = "aws.us-west-2"
  }

  name                  = "s3_replica"
  logging_bucket_prefix = "s3_replica"
  versioning            = var.versioning
  bucket_logging        = var.bucket_logging
  logging_bucket_name   = var.logging_bucket_name

  kms_key_id    = aws_kms_key.s3_replica-us-west-2-key.key_id
  sse_algorithm = var.sse_algorithm
}

module "s3" {
  source                = "git@github.com:xxxx"
  name                  = "s3"
  logging_bucket_prefix = "s3"
  versioning            = var.versioning
  bucket_logging        = var.bucket_logging
  logging_bucket_name   = var.logging_bucket_name

  kms_key_id    = aws_kms_key.s3.key_id
  sse_algorithm = var.sse_algorithm

  replication_configuration = {
    role = aws_iam_role.s3_replication.arn

      rules = {
         id = module.s3
         prefix = ""
         status = "Enabled"

        destination = {
          bucket = module.s3_replica.bucket_arn
          replica_kms_key_id = aws_kms_alias.s3_replica-us-west-2-key.arn
          storage_class = "STANDARD_IA"
          }
        }

      source_selection_criteria = {
          sse_kms_encrypted_objects = {
            enabled = true
          }
        }
  }
}  

我使用的模块中的复制配置块部分是:

dynamic "replication_configuration" {
    for_each = length(keys(var.replication_configuration)) == 0 ? [] : [var.replication_configuration]

    content {
      role = replication_configuration.value.role

      dynamic "rules" {
        for_each = replication_configuration.value.rules

        content {
          id       = lookup(rules.value, "id", null)
          priority = lookup(rules.value, "priority", null)
          prefix   = lookup(rules.value, "prefix", null)
          status   = lookup(rules.value, "status", null)

          dynamic "destination" {
            for_each = length(keys(lookup(rules.value, "destination", {}))) == 0 ? [] : [lookup(rules.value, "destination", {})]

            content {
              bucket             = lookup(destination.value, "bucket", null)
              storage_class      = lookup(destination.value, "storage_class", null)
              replica_kms_key_id = lookup(destination.value, "replica_kms_key_id", null)
              account_id         = lookup(destination.value, "account_id", null)
            }
          }

          dynamic "source_selection_criteria" {
            for_each = length(keys(lookup(rules.value, "source_selection_criteria", {}))) == 0 ? [] : [lookup(rules.value, "source_selection_criteria", {})]

            content {

              dynamic "sse_kms_encrypted_objects" {
                for_each = length(keys(lookup(source_selection_criteria.value, "sse_kms_encrypted_objects", {}))) == 0 ? [] : [lookup(source_selection_criteria.value, "sse_kms_encrypted_objects", {})]

                content {

                  enabled = sse_kms_encrypted_objects.value.enabled
                }
              }
            }
          }
}

现在,当我 运行 terraform init 时...它起作用了。 但是当我 运行 terraform plan 我得到错误:

Error: Invalid function argument

  on .terraform/modules/s3/main.tf line 321, in resource "aws_s3_bucket" "s3_bucket":"s3_bucket":
 321:           id       = lookup(rules.value, "id", null)
    |----------------
    | rules.value is "id.s3_replication"

Invalid value for "inputMap" parameter: lookup() requires a map as the first
argument.


Error: Invalid function argument

  on .terraform/modules/s3/main.tf line 321, in resource "aws_s3_bucket" "s3_bucket": "s3_bucket":
 321:           id       = lookup(rules.value, "id", null)
    |----------------
    | rules.value is ""

Invalid value for "inputMap" parameter: lookup() requires a map as the first
argument.


Error: Invalid function argument

  on .terraform/modules/s3/main.tf line 321, in resource "aws_s3_bucket" "s3_bucket": "s3_bucket":
 321:           id       = lookup(rules.value, "id", null)
    |----------------
    | rules.value is "Enabled"

Invalid value for "inputMap" parameter: lookup() requires a map as the first
argument.


Error: Invalid function argument

  on .terraform/modules/s3/main.tf line 322, in resource "aws_s3_bucket" "s3_bucket":
 322:           priority = lookup(rules.value, "priority", null)
    |----------------
    | rules.value is "id.s3_replication"

Invalid value for "inputMap" parameter: lookup() requires a map as the first
argument.


Error: Invalid function argument

  on .terraform/modules/s3/main.tf line 322, in resource "aws_s3_bucket" "s3_bucket":
 322:           priority = lookup(rules.value, "priority", null)
    |----------------
    | rules.value is ""

Invalid value for "inputMap" parameter: lookup() requires a map as the first
argument.


Error: Invalid function argument

  on .terraform/modules/s3/main.tf line 322, in resource "aws_s3_bucket" "s3_bucket":
 322:           priority = lookup(rules.value, "priority", null)
    |----------------
    | rules.value is "Enabled"

Invalid value for "inputMap" parameter: lookup() requires a map as the first
argument.


Error: Invalid function argument

  on .terraform/modules/s3/main.tf line 323, in resource "aws_s3_bucket" "s3_bucket":
 323:           prefix   = lookup(rules.value, "prefix", null)
    |----------------
    | rules.value is "id.s3_replication"

Invalid value for "inputMap" parameter: lookup() requires a map as the first
argument.


Error: Invalid function argument

  on .terraform/modules/s3/main.tf line 323, in resource "aws_s3_bucket" "s3_bucket":
 323:           prefix   = lookup(rules.value, "prefix", null)
    |----------------
    | rules.value is ""

Invalid value for "inputMap" parameter: lookup() requires a map as the first
argument.


Error: Invalid function argument

  on .terraform/modules/s3/main.tf line 323, in resource "aws_s3_bucket" "s3_bucket":
 323:           prefix   = lookup(rules.value, "prefix", null)
    |----------------
    | rules.value is "Enabled"

Invalid value for "inputMap" parameter: lookup() requires a map as the first
argument.


Error: Invalid function argument

  on .terraform/modules/s3/main.tf line 324, in resource "aws_s3_bucket" "s3_bucket":
 324:           status   = lookup(rules.value, "status", null)
    |----------------
    | rules.value is "id.s3_replication"

Invalid value for "inputMap" parameter: lookup() requires a map as the first
argument.


Error: Invalid function argument

  on .terraform/modules/s3/main.tf line 324, in resource "aws_s3_bucket" "s3_bucket":
 324:           status   = lookup(rules.value, "status", null)
    |----------------
    | rules.value is ""

Invalid value for "inputMap" parameter: lookup() requires a map as the first
argument.


Error: Invalid function argument

  on .terraform/modules/s3/main.tf line 324, in resource "aws_s3_bucket" "s3_bucket":
 324:           status   = lookup(rules.value, "status", null)
    |----------------
    | rules.value is "Enabled"

Invalid value for "inputMap" parameter: lookup() requires a map as the first
argument.


Error: Invalid function argument

  on .terraform/modules/s3/main.tf line 327, in resource "aws_s3_bucket" "s3_bucket":
 327:             for_each = length(keys(lookup(rules.value, "destination", {}))) == 0 ? [] : [lookup(rules.value, "destination", {})]
    |----------------
    | rules.value is "id.s3_replication"

Invalid value for "inputMap" parameter: lookup() requires a map as the first
argument.


Error: Invalid function argument

  on .terraform/modules/s3/main.tf line 327, in resource "aws_s3_bucket" "s3_bucket":
 327:             for_each = length(keys(lookup(rules.value, "destination", {}))) == 0 ? [] : [lookup(rules.value, "destination", {})]
    |----------------
    | rules.value is ""

Invalid value for "inputMap" parameter: lookup() requires a map as the first
argument.


Error: Invalid function argument

  on .terraform/modules/s3/main.tf line 327, in resource "aws_s3_bucket" "s3_bucket":
 327:             for_each = length(keys(lookup(rules.value, "destination", {}))) == 0 ? [] : [lookup(rules.value, "destination", {})]
    |----------------
    | rules.value is "Enabled"

Invalid value for "inputMap" parameter: lookup() requires a map as the first
argument.


Error: Invalid function argument

  on .terraform/modules/s3/main.tf line 338, in resource "aws_s3_bucket" "s3_bucket":
 338:             for_each = length(keys(lookup(rules.value, "source_selection_criteria", {}))) == 0 ? [] : [lookup(rules.value, "source_selection_criteria", {})]
    |----------------
    | rules.value is "id.s3_replication"

Invalid value for "inputMap" parameter: lookup() requires a map as the first
argument.


Error: Invalid function argument

  on .terraform/modules/s3/main.tf line 338, in resource "aws_s3_bucket" "s3_bucket":
 338:             for_each = length(keys(lookup(rules.value, "source_selection_criteria", {}))) == 0 ? [] : [lookup(rules.value, "source_selection_criteria", {})]
    |----------------
    | rules.value is ""

Invalid value for "inputMap" parameter: lookup() requires a map as the first
argument.


Error: Invalid function argument

  on .terraform/modules/s3/main.tf line 338, in resource "aws_s3_bucket" "s3_bucket":
 338:             for_each = length(keys(lookup(rules.value, "source_selection_criteria", {}))) == 0 ? [] : [lookup(rules.value, "source_selection_criteria", {})]
    |----------------
    | rules.value is "Enabled"

Invalid value for "inputMap" parameter: lookup() requires a map as the first
argument.

现在我不知道为什么会出现这些错误..

Error: Invalid function argument

  on .terraform/modules/s3/main.tf line 321, in resource "aws_s3_bucket" "s3_bucket":
 321:           id       = lookup(replication_configuration.value.rules, "id", null)
    |----------------
    | replication_configuration.value.rules is tuple with 1 element

Invalid value for "inputMap" parameter: lookup() requires a map as the first
argument.


Error: Invalid function argument

  on .terraform/modules/s3/main.tf line 322, in resource "aws_s3_bucket" "s3_bucket":
 322:           priority = lookup(replication_configuration.value.rules, "priority", null)
    |----------------
    | replication_configuration.value.rules is tuple with 1 element

Invalid value for "inputMap" parameter: lookup() requires a map as the first
argument.


Error: Invalid function argument

  on .terraform/modules/s3/main.tf line 323, in resource "aws_s3_bucket" "s3_bucket":
 323:           prefix   = lookup(replication_configuration.value.rules, "prefix", null)
    |----------------
    | replication_configuration.value.rules is tuple with 1 element

Invalid value for "inputMap" parameter: lookup() requires a map as the first
argument.


Error: Invalid function argument

  on .terraform/modules/s3/main.tf line 324, in resource "aws_s3_bucket" "s3_bucket":
 324:           status   = lookup(replication_configuration.value.rules, "status", null)
    |----------------
    | replication_configuration.value.rules is tuple with 1 element

Invalid value for "inputMap" parameter: lookup() requires a map as the first
argument.

您不需要 dynamic "rules" 中的每个规则,因为您在 replication_configuration.value.rules 中只有一个规则,并且没有什么可以迭代期望这个单一规则的实际值。

应该是:

          id       = lookup(replication_configuration.value.rules, "id", null)
          priority = lookup(replication_configuration.value.rules, "priority", null)
          prefix   = lookup(replication_configuration.value.rules, "prefix", null)
          status   = lookup(replication_configuration.value.rules, "status", null)

这仍然可能导致其他错误,因为您的动态块非常复杂且难以理解,需要重新编写才能使其工作。

或者,也许只需将输入值更改为规则列表即可,而无需完全更改动态块:

  replication_configuration = {
    role = aws_iam_role.s3_replication.arn

      rules = [
             {
         id = module.s3
         prefix = ""
         status = "Enabled"

        destination = {
          bucket = module.s3_replica.bucket_arn
          replica_kms_key_id = aws_kms_alias.s3_replica-us-west-2-key.arn
          storage_class = "STANDARD_IA"
          }
        }
      ]