Home > Blockchain >  Terraform variable referencing locals not working
Terraform variable referencing locals not working

Time:06-08

I need to pass the database host name (that is dynamically generated) as an environmental variable into my task definition. I thought I could set locals and have the variable map refer to a local but it seems to not work, as I receive this error: “error="failed to check table existence: dial tcp: lookup local.grafana-db-address on 10.0.0.2:53: no such host". I am able to execute the terraform plan without issues and the code works when I hard code the database host name, but that is not optimal.

My Variables and Locals

//MySql Database Grafana Username (Stored as ENV Var in Terraform Cloud)
variable "username_grafana" {
  description = "The username for the DB grafana user"
  type        = string
  sensitive   = true
}

//MySql Database Grafana Password (Stored as ENV Var in Terraform Cloud)
variable "password_grafana" {
  description = "The password for the DB grafana password"
  type        = string
  sensitive   = true
}

variable "db-port" {
  description = "Port for the sql db"
  type = string
  default = "3306"
}

locals {
  gra-db-user = var.username_grafana
}

locals {
  gra-db-password = var.password_grafana
}


locals {
  db-address = aws_db_instance.grafana-db.address
}

locals {
  grafana-db-address = "${local.db-address}.${var.db-port}"
}

variable "app_environments_vars" {
  type        = list(map(string))
  description = "Database environment variables needed by Grafana"
  default = [
    {
      "name"  = "GF_DATABASE_TYPE",
      "value" = "mysql"
    },

    {
      "name"  = "GF_DATABASE_HOST",
      "value" = "local.grafana-db-address"
    },

    {
      "name"  = "GF_DATABASE_USER",
      "value" = "local.gra-db-user"
    },

    {
      "name"  = "GF_DATABASE_PASSWORD",
      "value" = "local.gra-db-password"
    }
  ]
}

Task Definition Variable reference

 "environment": ${jsonencode(var.app_environments_vars)},

Thank you to everyone who has helped me with this project. I am new to all of this and could not have done it without help from this community.

CodePudding user response:

You can't use dynamic references in your app_environments_vars. So your default values "value" = "local.grafana-db-address" will never get resolved by TF. If will be just a literal string "local.grafana-db-address".

You have to modify your code so that all these dynamic references in app_environments_vars get populated in locals.

UPDATE

Your app_environments_vars should be local variable for it to be resolved:

locals {
 app_environments_vars = [
    {
      "name"  = "GF_DATABASE_TYPE",
      "value" = "mysql"
    },

    {
      "name"  = "GF_DATABASE_HOST",
      "value" = local.grafana-db-address
    },

    {
      "name"  = "GF_DATABASE_USER",
      "value" = local.gra-db-user
    },

    {
      "name"  = "GF_DATABASE_PASSWORD",
      "value" = local.gra-db-password
    }
  ]
}

then you pass that local to your template for the task definition.

  • Related