[英]When using terraform with heroku, is there a way to refresh app state after an addon is attached?
I'm using terraform to set up an app on heroku.我正在使用 terraform 在 heroku 上设置一个应用程序。 The app needs a mysql database and I'm using ClearDB addon for that.
该应用程序需要一个 mysql 数据库,我为此使用了 ClearDB 插件。 When ClearDB is attached to the app, it sets a
CLEARDB_DATABASE_URL
config variable.当 ClearDB 附加到应用程序时,它会设置一个
CLEARDB_DATABASE_URL
配置变量。 The app requires the database url to be in a DATABASE_URL
config variable.该应用程序要求数据库 url 位于
DATABASE_URL
配置变量中。 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
.所以我想要做的是使用资源
heroku_app_config_association
将值从CLEARDB_DATABASE_URL
复制到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.问题是,经过
heroku_app
创建资源,其状态尚不包含CLEARDB_DATABASE_URL
,经过heroku_addon
被创建,状态heroku_app
不会被更新。
This is the content of my main.tf
file:这是我的
main.tf
文件的内容:
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.第二次执行
terraform apply
时,配置变量复制成功。 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:为了获取
heroku_app
资源的最新状态,我使用了数据源:
data "heroku_app" "main" {
name = heroku_app.main.name
depends_on = [
heroku_addon.database
]
}
I could then access the value in heroku_app_config_association
resource:然后我可以访问
heroku_app_config_association
资源中的值:
resource "heroku_app_config_association" "main" {
app_id = heroku_app.main.id
sensitive_vars = {
DATABASE_URL = data.heroku_app.main.config_vars.CLEARDB_DATABASE_URL
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.