terraform-provider-heroku icon indicating copy to clipboard operation
terraform-provider-heroku copied to clipboard

heroku_build resource seems to not take Config Vars into account

Open ArtOfFugue66 opened this issue 3 years ago • 6 comments

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: image
  • Heroku app config vars, initialized by this same command: image

Steps to Reproduce

Using the Terraform config files provided:

  1. terraform init --upgrade
  2. terraform validate
  3. terraform plan -out="plan.tplan"
  4. terraform apply "plan.tplan"

Important Factoids

  1. Project is organized into multiple subdirectories, one of which is frontend/.
  2. Buildpacks used: https://github.com/timanovsky/subdir-heroku-buildpack (requires PROJECT_PATH to be set), heroku/nodejs.
  3. I have tried adding heroku_config & heroku_app_config_association resources 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
  ]
}
  1. If omitting the heroku_build resource from the main.tf file, the app is created with no issues. In this scenario, triggering the build via git push heroku <branch_name>:main works as expected and the app is functional. This suggests that this issue is caused by the heroku_build resource.

ArtOfFugue66 avatar Sep 28 '22 10:09 ArtOfFugue66

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?

mars avatar Sep 29 '22 17:09 mars

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
  }
}

mars avatar Sep 29 '22 18:09 mars

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.

mars avatar Sep 29 '22 23:09 mars

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
  }
}

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.

ArtOfFugue66 avatar Oct 03 '22 07:10 ArtOfFugue66

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.

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.

ArtOfFugue66 avatar Oct 03 '22 07:10 ArtOfFugue66

Any update @ArtOfFugue66 ?

mars avatar Oct 17 '22 22:10 mars