search by tags

for the user

adventures into the land of the command line

terraform variable passing

i have a module and i want to pass some variables defined in prod/main.tf to the module my_app/main.tf

★ tree
.
├── README.md
├── modules
│   ├── my_app
│   │   ├── provider.tf
│   │   ├── main.tf <----------- aaaand this guy
│   │   └── variables.tf <--------- and this guy also
├── prod
│   ├── backend.tf
│   └── main.tf <--------- inside this guy

the variables look like this. they are just some strings:

> prod/main.tf

module "my_app" {
    source = "../modules/my_app"

    some_var = "abc"
    some_other_var = "123"
}

in the module’s variables.tf file, you need to do this, which is like initialising them. good practice is to include a description with the variable.

> modules/my_app/variables.tf

variable "some_var" {
    type        = "string"
    description = "A groovy description about this variable."
}

variable "some_other_var" {
    type        = "string"
    description = "Descriptions are awesome."
}

and finally, you will be able to use them in the actual module template by putting them into this ${var.some_var} format:

> modules/my_app/main.tf

resource "azurerm_mysql_database" "my_groovy_db" {
  name                = "my_groovy_db"
  resource_group_name = "${var.some_var}"
  server_name         = "${var.some_other_var}"
  charset             = "utf8"
  collation           = "utf8_unicode_ci"
}

terraform i ❤︎ u