Home > OS >  How to instantiate an object in locals?
How to instantiate an object in locals?

Time:11-01

I have a module which uses a variable of object type

variable "table" {
  type = object({
    name    = string
    read_capacity = number
    write_capacity = number
    arn = string
    policy_name = string
  })
}

I am not able to understand how to initialize this and pass to the module

locals {
  table_dev_configs = {
  arn = "arn:aws:dynamodb:us-east-2:123456789:table/my-table-dev"
  name = "some_name"
  policy_name = "some_policy"
  read_capacity = 20
  write_capacity = 20
  }
}


module "my_table_dev" {
  source = "../../modules/my_table"
  table = local.table_dev_configs
}

I mean locals.table_dev_configs looks I am initializing a Map instead of an object.

../modules/my_table.tf

resource "aws_dynamodb_table" "my_table" {
  name           = var.table.name
  read_capacity  = var.table.read_capactity
  write_capacity = var.table.write_capactity
  hash_key       = "id"
}

Doing it the Map way gives me this error:

│ Error: Unsupported attribute
│ 
 read_capacity  = var.table.read_capactity
│     ├────────────────
│     │ var.table is a object, known only after apply
│ 
│ This object does not have an attribute named "read_capactity".
╵

What is the right way to initialize an object?

CodePudding user response:

Instead of

  read_capacity  = var.table.read_capactity
  write_capacity = var.table.write_capactity

it should be

  read_capacity  = var.table.read_capacity
  write_capacity = var.table.write_capacity
  • Related