terraform-provider-heroku
terraform-provider-heroku copied to clipboard
heroku_build resource seems to not take Config Vars into account
Hi there,
I ran into this issue while trying to build a React app in Heroku via Terraform, using the heroku_build resource. The Heroku app is created without issue using heroku_app resource, however when heroku_build kicks in to start the build, the building process fails with the message 'PROJECT_PATH is undefined', even though the PROJECT_PATH config var is set in the app.
Terraform Version & Heroku Provider Version
Terraform v1.3.0
on linux_amd64
\+ provider registry.terraform.io/heroku/heroku v5.1.4
Affected Resource(s)
Please list the resources as a list, for example:
- heroku_build
Terraform Configuration Files
# main.tf
resource "heroku_app" "middleware-frontend-app" {
name = var.heroku_fe_app_name
region = var.region
config_vars = {
PROJECT_PATH = "frontend"
}
buildpacks = [
"https://github.com/timanovsky/subdir-heroku-buildpack",
"heroku/nodejs"
]
}
resource "heroku_build" "middleware-frontend-build" {
app_id = heroku_app.middleware-frontend-app.id
source {
path = "frontend" # The path to the FE app source code
}
depends_on = [
heroku_app.middleware-frontend-app,
]
}
resource "heroku_formation" "fe-formation" {
app_id = heroku_app.middleware-frontend-app.id
type = "web"
quantity = 1
size = "Standard-1x"
depends_on = [
heroku_build.middleware-frontend-build
]
}
# providers.tf
terraform {
required_version = ">=1.0.7"
required_providers {
heroku = {
source = "heroku/heroku"
version = "5.1.4"
}
}
}
provider "heroku" {
}
# variables.tf
variable "region" {
default = "us"
}
variable "heroku_fe_app_name" {
default = "middleware-frontend"
description = "Name of the frontend Heroku app as it is displayed in the Heroku dashboard"
}
Debug Output
Gist containing the debug output
Expected Behavior
React app builds successfully inside Heroku app.
Actual Behavior
Build fails with error message PROJECT_PATH is undefined, even though the PROJECT_PATH config var has been set in the app.
terraform apply -out="plan.tplan"command output:
- Heroku app config vars, initialized by this same command:

Steps to Reproduce
Using the Terraform config files provided:
terraform init --upgradeterraform validateterraform plan -out="plan.tplan"terraform apply "plan.tplan"
Important Factoids
- Project is organized into multiple subdirectories, one of which is
frontend/. - Buildpacks used:
https://github.com/timanovsky/subdir-heroku-buildpack(requiresPROJECT_PATHto be set),heroku/nodejs. - I have tried adding
heroku_config&heroku_app_config_associationresources as follows, with no success (same error):
# main.tf
resource "heroku_config" "common" {
vars = {
PROJECT_PATH = "frontend"
}
}
resource "heroku_app_config_association" "middleware-frontend-config" {
app_id = heroku_app.middleware-frontend-app.id
vars = heroku_config.common.vars
depends_on = [
heroku_config.common
]
}
- If omitting the
heroku_buildresource from themain.tffile, the app is created with no issues. In this scenario, triggering the build viagit push heroku <branch_name>:mainworks as expected and the app is functional. This suggests that this issue is caused by theheroku_buildresource.
heroku_build resource has nothing to do with how config vars are populated into the build process' ENV_DIR.
Anecdotally, we have numerous uses of config vars in source-based builds with Terraform, which work correctly.
I suspect there is something subtle that has happened to this deployment, maybe from ordering of operations during development. If you use the TF config to create a completely new deployment, without any pre-existing state, does this error still occur?
Also, the resource "heroku_build" "middleware-frontend-build" should not have that depends_on, because the app_id already contains a dynamic reference to that parent resource.:
resource "heroku_build" "middleware-frontend-build" {
app_id = heroku_app.middleware-frontend-app.id
source {
path = "frontend" # The path to the FE app source code
}
}
Also worth noting, your linked gist of the Terraform log does not contain any app creation, nor setting of config vars. So there's nothing in that log to indicate that the PROJECT_PATH config var should be available in the build process.
Curious to see what a fresh terraform apply would log.
Also, the
resource "heroku_build" "middleware-frontend-build"should not have thatdepends_on, because theapp_idalready contains a dynamic reference to that parent resource.:resource "heroku_build" "middleware-frontend-build" { app_id = heroku_app.middleware-frontend-app.id source { path = "frontend" # The path to the FE app source code } }
I can confirm from previous tests that depends_on does not affect this issue. I got this same error when running the apply command both with and without the depends_on instructions.
Also worth noting, your linked gist of the Terraform log does not contain any app creation, nor setting of config vars. So there's nothing in that log to indicate that the
PROJECT_PATHconfig var should be available in the build process.Curious to see what a fresh
terraform applywould log.
I will run a fresh terraform apply when I get a chance, today or tomorrow, and will provide you with the full Terraform log. Thank you.
Any update @ArtOfFugue66 ?