Terraform 动态循环创建多个 dynamodb

问题描述 投票:0回答:2

我正在尝试创建一个动态方法来创建多个具有自己属性的 dynamodb 表。我尝试使用动态块、列表对象等进行循环,但无法迭代每个表的属性。目标是一次性拥有多个具有不同属性的表以及每个表的全局索引。我有 terraform.tfvars 和 main.tf ,其结构如下:

变量声明:

variable "dynamodb_table" {
  type = list(object({
    table_name         = string
    billing_mode       = string
    read_capacity      = optional(number)
    write_capacity     = optional(string)
    hash_key           = string
    ttl_attribute_name = string
    ttl_enabled        = string
    range_key          = optional(string)
    attribute = object({
      name = string
      type = string
    })
  }))
}

variable "global_secondary_indexes" {
  description = "Describe a GSI for the table; subject to the normal limits on the number of GSIs, projected attributes, etc."
  type = list(object({
    index_name               = string
    index_projection_type    = string
    index_range_key          = string
    index_hash_key           = string
    index_write_capacity     = optional(string)
    index_read_capacity      = optional(string)
    index_non_key_attributes = list(string)
  }))

  default = []
}
dynamodb_table = [
      {
        table_name   = "devops-test-01",
        billing_mode = "PAY_PER_REQUEST",
        hash_key           = "UserId",
        range_key          = "GameTitle",
        ttl_attribute_name = "ttl_attribute_name",
        ttl_enabled        = "false"
        attribute = [
          {
            name = "UserId"
            type = "S"
          },
          {
            name = "GameTitle"
            type = "S"
          }
        ]
      },
      {
        table_name   = "devops-test-02",
        billing_mode = "PAY_PER_REQUEST",
        hash_key           = "GameTitle",
        ttl_attribute_name = "ttl_attribute_name",
        ttl_enabled        = "false"
      }
    ]

    global_secondary_indexes = [
      {
        index_name               = "TitleIndex"
        index_hash_key           = "UserId"
        index_range_key          = "GameTitle"
        index_projection_type    = "INCLUDE"
        index_non_key_attributes = ["Id"]
      }

    ]

    default_tags = {
      "Environment" = "Dev",
      "Owner"       = "xxx"
    }
resource "aws_dynamodb_table" "basic-dynamodb-table" {
      for_each       = { for key, value in var.dynamodb_table : key => value }
      name           = each.value.table_name
      billing_mode   = each.value.billing_mode
      read_capacity  = each.value.read_capacity
      write_capacity = each.value.write_capacity
      hash_key       = each.value.hash_key
      range_key      = each.value.range_key
      ttl {
        attribute_name = each.value.ttl_attribute_name
        enabled        = each.value.ttl_enabled
      }

      dynamic "attribute" {

        for_each = { for key, value in var.attributes : key => value }
        content {
          name = attribute.value.name
          type = attribute.value.type
        }
      }

      dynamic "global_secondary_index" {
        for_each = var.global_secondary_indexes
        content {
          name               = global_secondary_index.value.index_name
          hash_key           = global_secondary_index.value.index_hash_key
          projection_type    = global_secondary_index.value.index_projection_type
          range_key          = lookup(global_secondary_index.value, "index_range_key", null)
          read_capacity      = lookup(global_secondary_index.value, "index_read_capacity", null)
          write_capacity     = lookup(global_secondary_index.value, "index_write_capacity", null)
          non_key_attributes = lookup(global_secondary_index.value, "index_non_key_attributes", null)
        }
      }


      tags = merge(
        var.default_tags,
        {
          Name = each.value.table_name
      })

    }

此代码会产生以下错误:

给定值不适合在variable.tf:6,1-26处声明的var.dynamodb_table:元素0:属性│“属性”:需要对象

amazon-web-services terraform terraform-provider-aws
2个回答
2
投票

您没有共享您的

attributes
变量,但我在
attributes
变量中使用了
dynamodb_table

您的主要问题是需要

attribute
变量中的
dynamodb_table
属性,但您没有在
devops-test-02
表值中为其提供任何值。


variables.tf

variable "dynamodb_table" {
  type = list(object({
    table_name   = string
    billing_mode = string
    // read_capacity      = optional(number)
    //write_capacity     = optional(string)
    hash_key           = string
    ttl_attribute_name = string
    ttl_enabled        = string
    //range_key          = optional(string)
    attribute = list(object({
      name = string
      type = string
    }))
  }))
  default = [
    {
      table_name         = "devops-test-01",
      billing_mode       = "PAY_PER_REQUEST",
      hash_key           = "UserId",
      range_key          = "GameTitle",
      ttl_attribute_name = "ttl_attribute_name",
      ttl_enabled        = "false"
      attribute = [
        {
          name = "UserId"
          type = "S"
        },
        {
          name = "GameTitle"
          type = "S"
        }
      ]
    },
    {
      table_name         = "devops-test-02",
      billing_mode       = "PAY_PER_REQUEST",
      hash_key           = "GameTitle",
      ttl_attribute_name = "ttl_attribute_name",
      ttl_enabled        = "false"
      attribute = [
        {
          name = "UserId"
          type = "S"
        },
        {
          name = "GameTitle"
          type = "S"
        }
      ]
    }
  ]
}

variable "global_secondary_indexes" {
  description = "Describe a GSI for the table; subject to the normal limits on the number of GSIs, projected attributes, etc."
  type = list(object({
    index_name            = string
    index_projection_type = string
    index_range_key       = string
    index_hash_key        = string
    //index_write_capacity     = optional(string)
    //index_read_capacity      = optional(string)
    index_non_key_attributes = list(string)
  }))

  default = [
    {
      index_name               = "TitleIndex"
      index_hash_key           = "UserId"
      index_range_key          = "GameTitle"
      index_projection_type    = "INCLUDE"
      index_non_key_attributes = ["Id"]
    }
  ]
}

variable "default_tags" {
  default = {
    "Environment" = "Dev",
    "Owner"       = "xxx"
  }
}

dynamodb.tf

resource "aws_dynamodb_table" "basic-dynamodb-table" {
  for_each       = { for key, value in var.dynamodb_table : value.table_name => value }
  name           = each.value.table_name
  billing_mode   = each.value.billing_mode
  read_capacity  = lookup(each.value, "read_capacity", null)
  write_capacity = lookup(each.value, "write_capacity", null)
  hash_key       = each.value.hash_key
  range_key      = lookup(each.value, "range_key", null)
  ttl {
    attribute_name = each.value.ttl_attribute_name
    enabled        = each.value.ttl_enabled
  }

  dynamic "attribute" {
    for_each = { for key, value in each.value.attribute : key => value }
    content {
      name = attribute.value.name
      type = attribute.value.type
    }
  }

  dynamic "global_secondary_index" {
    for_each = var.global_secondary_indexes
    content {
      name               = global_secondary_index.value.index_name
      hash_key           = global_secondary_index.value.index_hash_key
      projection_type    = global_secondary_index.value.index_projection_type
      range_key          = lookup(global_secondary_index.value, "index_range_key", null)
      read_capacity      = lookup(global_secondary_index.value, "index_read_capacity", null)
      write_capacity     = lookup(global_secondary_index.value, "index_write_capacity", null)
      non_key_attributes = lookup(global_secondary_index.value, "index_non_key_attributes", null)
    }
  }


  tags = merge(
    var.default_tags,
    {
      Name = each.value.table_name
  })
}

更新2023-01-17 将 Kinesis 流目标资源添加到 dynamodb 表。

resource "aws_kinesis_stream" "example" {
  for_each = aws_dynamodb_table.basic-dynamodb-table

  name        = "${each.key}_table_stream"
  shard_count = 1
}

resource "aws_dynamodb_kinesis_streaming_destination" "example" {
    for_each = aws_dynamodb_table.basic-dynamodb-table
    
    stream_arn = aws_kinesis_stream.example[each.key].arn   
    table_name = each.key
}

0
投票

你能给出上面dynamodb表的output.tf文件吗

© www.soinside.com 2019 - 2024. All rights reserved.