I'm using terraform to set up an app on heroku. The app needs a mysql database and I'm using ClearDB addon for that. When ClearDB is attached to the app, it sets a CLEARDB_DATABASE_URL
config variable. The app requires the database url to be in a DATABASE_URL
config variable.
So what I'm trying to do is to use resource heroku_app_config_association
to copy the
value from CLEARDB_DATABASE_URL
to DATABASE_URL
. The problem is that after heroku_app
resource is created, its state does not yet contain CLEARDB_DATABASE_URL
, and after heroku_addon
is created, the state of heroku_app
is not updated.
This is the content of my main.tf
file:
provider "heroku" {}
resource "heroku_app" "main" {
name = var.app_name
region = "eu"
}
resource "heroku_addon" "database" {
app = heroku_app.main.name
plan = "cleardb:punch"
}
resource "heroku_app_config_association" "main" {
app_id = heroku_app.main.id
sensitive_vars = {
DATABASE_URL = heroku_app.main.all_config_vars.CLEARDB_DATABASE_URL
}
depends_on = [
heroku_addon.database
]
}
This is the error that I get:
Error: Missing map element
on main.tf line 22, in resource "heroku_app_config_association" "main":
22: DATABASE_URL = heroku_app.main.all_config_vars.CLEARDB_DATABASE_URL
|----------------
| heroku_app.main.all_config_vars is empty map of string
This map does not have an element with the key "CLEARDB_DATABASE_URL".
The configuration variable is copied successfully when terraform apply
is executed second time. Is there a way to make it work on the first run?
To get up-to-date state of the
heroku_app
resource I used the data source:I could then access the value in
heroku_app_config_association
resource: