Terraform count attribute issue

 Error: Invalid reference
│ 
│   on modules/database/database.tf line 92, in resource "azurerm_postgresql_server" "events_server":
│   92:   count                        = "${var.region_specific[region] == "primary" ? 1 : 0}"
│ 
│ A reference to a resource type must be followed by at least one attribute
│ access, specifying the resource name.
#Events_postgres_server

resource "azurerm_postgresql_server" "events_server" {

  name                = "${var.azure_events_postgresql_name}-${var.environment}-${var.suffix}"

  location            = var.rg_obuoc_location

  resource_group_name = var.rg_obuoc_name

  sku_name     = var.postgresql_sku_name

  storage_mb            = var.postgresql_storage_mb

  backup_retention_days = var.postgresql_backup_retention_days

  geo_redundant_backup_enabled = var.postgresql_geo_redundant_backup

  administrator_login          = var.pdb_admin_login_akv_key

  administrator_login_password = var.pdb_admin_pwd_akv_key

  version                      = var.postgresql_server_version

  ssl_enforcement_enabled      = var.postgresql_ssl_enforcement

  count                        = "${var.region_specific[region] == "primary" ? 1 : 0}"

  tags = var.tags

  //depends_on = [null_resource.sleep]

}

resource "azurerm_postgresql_database" "events_dbs" {

  #count               = length(var.postgresql_db_names)

  name                = var.postgresql_events_db_names

  resource_group_name = var.rg_obuoc_name

  server_name         = azurerm_postgresql_server.events_server.name

  charset             = var.postgresql_db_charset

  collation           = var.postgresql_db_collation

  depends_on = [azurerm_postgresql_server.events_server]

 //count                        = (var.region_specific[var.region] ? 1 : 0)

}
variable "region_specific" {

  type = map(bool)

    default = {

  "primary" = true

  "secondary" = false

}

}

variable "region" { }

hello i have been trying to use count with variable_region specific my requirement is if i select region as a Primary terraform has to deploy the Database ifnot it has to abort but while i try to use count i get above error can anyone help me on this

Hi @eliyaz7134,

I’m not sure I fully understand everything you shared but the main thing I notice in the error message is that you referred to just region, and so Terraform thinks you are trying to refer to a block like resource "region" "...", and is returning an error related to that misunderstanding.

If you instead intended to refer to variable "region" then you can write that as var.region:

  count = var.region_specific[var.region] == "primary" ? 1 : 0

With that said, although the above should deal with the error you already saw, I think it will lead to a new problem: the keys in yuor var.region_specific are "primary" and "secondary", and the values are true and false. That means that var.region_specific[var.region] will fail unless var.region is one of those strings, and even if it were one of those strings this conditional expression would always produce 0 because the string "primary" isn’t equal to either true or false.

If you’re not sure how to proceed beyond what I’ve suggested above, it would help if you could say a little more about your goal, so we can think about different ways to meet it.



hi @apparentlymart I am trying to deploy events_server if the region is primary
if it is secondary region it should not get deployed for that i have referenced with region_specific where i have primary and secondary and then added a variable region this var.region value will be passed as a parameter at run time of pipeline - var.region = primary like that iam passing but somehow the count is failing with the following error

│ Error: Missing resource instance key

│ on modules/database/database.tf line 101, in resource “azurerm_postgresql_database” “events_dbs”:
│ 101: server_name = azurerm_postgresql_server.events_server.name

│ Because azurerm_postgresql_server.events_server has “count” set, its
│ attributes must be accessed on specific instances.

│ For example, to correlate with indices of a referring resource, use:
│ azurerm_postgresql_server.events_server[count.index]

tried with multiple ways of using count like this
count = var.region_specific[var.region] == “primary” ? 1 : 0
even though the error still getting can you help me where iam doing wrong

Hello @apparentlymart I have one more concern I have a event database already existing in terraform infrastructure, now I need to add read-replica for the event database I have added that. now my question is do I need to do terraform tf.state file update manually or else terraform will identify the new configuration and update the state file accordingly ?