terraform-provider-vercel: Error when trying to deploy prebuilt project

Hi,

I have a monorepo setup:

.vercel/
src/
  frontend/
    ...
    terraform/
      main.tf 

(Note how the .vercel folder is at the root of the monorepo - this is required for vercel build to work correctly).

I can build & deploy this manually just fine:

pnpm vercel build --prod --token=$VERCEL_TOKEN
pnpm vercel deploy --prebuilt --prod --token=$VERCEL_TOKEN

Now I want to do the same, deploying with terraform. Following the docs, I do:

data "vercel_prebuilt_project" "frontend_dir" {
  path = "../../../"
}

# TODO: How does this interact with the .vercel/project.json stuff?
resource "vercel_deployment" "prod_deployment" {
  project_id  = vercel_project.frontend_project.id
  files       = data.vercel_prebuilt_project.frontend_dir.output
  path_prefix = data.vercel_prebuilt_project.frontend_dir.path
  production = true
}

But this raises an error when running terraform -chdir=src/frontend/terraform apply (after having run pnpm vercel build --prod --token=$VERCEL_TOKEN already):

│ Error: Error reading prebuilt output
│ 
│   with data.vercel_prebuilt_project.frontend_dir,
│   on main.tf line 61, in data "vercel_prebuilt_project" "frontend_dir":
│   61: data "vercel_prebuilt_project" "frontend_dir" {
│ 
│ An unexpected error occurred reading files from the .vercel directory: could not read file
│ ../../../.vercel/output/functions/_not-found.rsc.func: read ../../../.vercel/output/functions/_not-found.rsc.func: is a directory

And that’s correct, .vercel/output/functions/_not-found.rsc.func is indeed a directory, not sure why terraform expects it to be a file. The vercel provider version is vercel/vercel v2.3.0, and I am using next 14.2.

I couldn’t find anything about this error online. Do you have an idea? Thanks!

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 27

Commits related to this issue

Most upvoted comments

Maybe just do terraform init -upgrade then. Probably because of your lock file.

Ahh damn, yeah, that makes sense. It’s interesting that i didn’t get these issues when i was doing a reproduction of your use case and testing out the fixes. It looks like the fix should be fairly simple though - just appending the path_prefix onto the files read from the filePathMap. I’ll look and see if i can get a fix out. 🙏

Hey @dglsparsons, thanks for your reactivity. I believe I need to give you more context on my monorepo structure. Once I ran vercel pull and vercel build from the root of my repository, here’s how it looks like when I try to apply my terraform stack located on infrastructure/vercel/project_A :

.
├── .vercel
│   ├── .env.preview.local
│   ├── output
│   └── project.json
├── .yarn
├── .yarnrc.yml
├── next_project_A
│   ├── .next
│   ├── next.config.js
│   ├── node_modules
│   ├── package.json
│   ├── public
│   ├── src
│   └── types
├── next_project_B
├── next_project_C
├── infrastructure
│   └── vercel
│       └── project_A
│               ├── .terraform
│               ├── .terraform.lock.hcl
│               ├── backend.tf
│               ├── data.tf
│               ├── main.tf
│               ├── outputs.tf
│               ├── provider.tf
│               └── variables.tf
├── node_modules
├── package.json
├── ui
│   └── dist
└── yarn.lock

I gave v2.13.0 a go and seems to encounter other issues :

First of all the path_prefix is well applied on the files inside the .vercel folder When I output data.vercel_prebuilt_project.preview.output, I have some files well prefixed like that : "../../../../../MyRepo/.vercel/output/builds.json": "793~279f82e969d7edd9f18b5ce87461bdc9dc0690ba", But also not so well prefixed like dependency files :

              "node_modules/@fortawesome/fontawesome-svg-core/index.js": "108829~762f236f1314733ec1ec731cb86de4244ff10892",
              <more node_modules/ files>...,
              "ui/dist/index.js": "59244~03b97c9422b8c24b489ea141dc8cfe32746e57b0",
              "ui/package.json": "2618~c4ca1b5da7823a114d10588c4c9d3707fa45edbc"

And applying terraform gives me error like : Could not get info for file ui/dist/index.js, unexpected error: lstat ui/dist/index.js: no such file or directory I believe the data.vercel_prebuilt_project.preview.output output should looks like :

              "../../../../../MyRepo/.vercel/output/builds.json": "793~279f82e969d7edd9f18b5ce87461bdc9dc0690ba",
              "../../../../../MyRepo/node_modules/@fortawesome/fontawesome-svg-core/index.js": "108829~762f236f1314733ec1ec731cb86de4244ff10892",
              <more node_modules/ files>...,
              "../../../../../MyRepo/ui/dist/index.js": "59244~03b97c9422b8c24b489ea141dc8cfe32746e57b0",
              "../../../../../MyRepo/ui/package.json": "2618~c4ca1b5da7823a114d10588c4c9d3707fa45edbc"

Please let me know if you need further information

@dglsparsons Congratulations on your baby! 😃

Thank you, looking forward to see this fixed. It’s hard to create a simple repo to reproduce, since this is a monorepo with a bunch of other code. This is my entire main.tf configuration:

terraform {
  required_providers {
    vercel = {
      source  = "vercel/vercel"
      version = "~> 2.0"
    }
  }
}

provider "vercel" {
  api_token = var.api_token
}

resource "vercel_project" "this" {
  name      = var.project_name
  framework = "nextjs"
}

data "vercel_prebuilt_project" "prebuild_dir" {
  # Points to the base directory where the .vercel dir is
  path = "/home/myrepo/"
}

resource "vercel_deployment" "prod_deployment" {
  project_id  = vercel_project.this.id
  files       = data.vercel_prebuilt_project.prebuild_dir.output
  path_prefix = data.vercel_prebuilt_project.prebuild_dir.path
  production = true
}

The structure of my project is:

tsconfig.json
.vercel
src/frontend
├── app
│   ├── api
│   ├── layout.tsx
│   ├── page.tsx
├── next.config.mjs
└── terraform
    ├── prod
    │   └── vercel
            └── main.tf

And the error is still the same:

An unexpected error occurred reading files from the .vercel directory:
  │ could not read file
  │ /home/myrepo/.vercel/output/functions/_not-found.rsc.func: read
  │ /home/myrepo/.vercel/output/functions/_not-found.rsc.func: is a
  │ directory