【问题标题】:Keep getting Terraform Error for S3 Replication不断收到 S3 复制的 Terraform 错误
【发布时间】:2021-06-06 22:15:50
【问题描述】:

我正在尝试在跨区域的 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.

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

  • 我尝试将我的 rules.value (即“id”)更改为许多不同的值,但它只是不断给出相同的错误..

  • 由于@Marcin .. 谢谢你。

  • 但现在我遇到了新错误:

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.
  • 请帮我修复我正在使用的动态块。我一直卡在这个问题上,似乎无法弄清楚。

【问题讨论】:

    标签: amazon-web-services amazon-s3 syntax terraform terraform-provider-aws


    【解决方案1】:

    您不需要 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"
              }
            }
          ]
    
    

    【讨论】:

    • 谢谢 .. 这有助于消除当前的错误,但我得到了更多。我需要帮助来解决这个问题。自 2 周以来,我一直被困在这种复制上,似乎无法弄清楚。你能帮我修改我的动态块吗?我已经更新了我的问题。
    • @mgb 你好。没问题。我认为对新错误提出新问题会更好。
    • 嘿@Marcin .. 谢谢你!!我提出了一个新问题,请检查 ty
    猜你喜欢
    • 2021-06-14
    • 1970-01-01
    • 1970-01-01
    • 2013-08-10
    • 2019-05-05
    • 2021-06-12
    • 1970-01-01
    • 1970-01-01
    • 2020-04-26
    相关资源
    最近更新 更多