这是indexloc提供的服务,不要输入任何密码
Skip to content

[BUG] Can't add environment variable to custom environment, sometimes #362

@TowardsDeath

Description

@TowardsDeath

Describe the bug

I'm having trouble with custom environments and environment variables, and the behaviour seems random. These are the scenario's where it doesn't do what I would expect.

Scenario 1

When adding an environment variable to a project that has a custom environment, the apply will fail when creating the new environment variable:

Error: Error updating project environment variables
(...)
Could not update project environment variable, unexpected error: failed to
create environment variables, A variable with the name VAR_2
already exists for the target preview on branch undefined

When re-running apply, the error changes:

Could not update project environment variable, unexpected error:
ENV_CONFLICT - A variable with the name VAR_2 already exists
for the target preview on branch undefined the conflicting environment
variable ID is prj_3ZzKj1r6Zg2VBnePtHBP8dXA0rz2/JzG6wAcSFyPQjgbg

The new environment variable is correctly created on development, preview and production, but not for the custom environment.

Scenario 2

When adding a new project with a custom environment with environment variables, the apply will succeed. However, when running another apply after that (with changes unrelated to Vercel), the apply will fail, even though nothing has changed in the project, not in Terraform nor in the UI:

Could not create project environment variables, unexpected error: failed to
create environment variables, A variable with the name VAR_1 for
the target undefined already exists on branch undefined.

Scenario 3

I tried to reproduce the issue by creating a project similar to the ones that fail, but that seems to work:

module.vercel.vercel_project_environment_variables.test: Modifying...
module.vercel.vercel_project_environment_variables.test: Modifications complete after 1s

The only difference between the projects and the reproduction might be the number of variables. Could that be a problem?

To Reproduce

This was my reproduction case, but as mentioned above, this seems to work, and a project with "real world data" doesn't.

resource "vercel_project" "test" {
  name                       = "test"
  team_id                    = var.team_id
  serverless_function_region = var.serverless_function_region
  output_directory           = "dist"
  skew_protection            = "12 hours"

  vercel_authentication = {
    deployment_type = "standard_protection"
  }

  git_repository = {
    type              = "gitlab"
    repo              = "some/project"
    production_branch = "main"
  }
}

resource "vercel_custom_environment" "test_staging" {
  project_id  = vercel_project.test.id
  name        = "staging"
  description = "Staging environment"
  branch_tracking = {
    pattern = "develop"
    type    = "equals"
  }
}

resource "vercel_project_environment_variables" "test" {
  project_id = vercel_project.test.id
  variables = [
    {
      key    = "VAR_1"
      value  = "https://production"
      target = ["production"]
    },
    {
      key                    = "VAR_1"
      value                  = "https://staging"
      target                 = ["preview"]
      custom_environment_ids = [vercel_custom_environment.test_staging.id]
    },
    {
      key    = "VAR_1"
      value  = "http://development"
      target = ["development"]
    },
  ]
}

resource "vercel_project_deployment_retention" "test" {
  project_id            = vercel_project.test.id
  team_id               = vercel_project.test.team_id
  expiration_preview    = "3m"
  expiration_production = "unlimited"
  expiration_canceled   = "1m"
  expiration_errored    = "1m"
}

resource "vercel_firewall_config" "test_firewall" {
  project_id = vercel_project.test.id

  managed_rulesets {
    owasp {
      gen  = { action = "deny" }
      java = { action = "deny" }
      lfi  = { action = "deny" }
      ma   = { action = "deny" }
      php  = { action = "deny" }
      rce  = { action = "deny" }
      rfi  = { action = "deny" }
      sd   = { action = "deny" }
      sqli = { action = "deny" }
      xss  = { action = "deny" }
    }
  }
}

Add an environment variable for a custom environment:

resource "vercel_project" "test" {
  name                       = "test"
  team_id                    = var.team_id
  serverless_function_region = var.serverless_function_region
  output_directory           = "dist"
  skew_protection            = "12 hours"

  vercel_authentication = {
    deployment_type = "standard_protection"
  }

  git_repository = {
    type              = "gitlab"
    repo              = "some/project"
    production_branch = "master"
  }
}

resource "vercel_custom_environment" "test_staging" {
  project_id  = vercel_project.test.id
  name        = "staging"
  description = "Staging environment"
  branch_tracking = {
    pattern = "develop"
    type    = "equals"
  }
}

resource "vercel_project_environment_variables" "test" {
  project_id = vercel_project.test.id
  variables = [
    {
      key    = "VAR_1"
      value  = "https://production"
      target = ["production"]
    },
+   {
+     key    = "VAR_2"
+     value  = "https://some-other-url"
+     target = ["production"]
+   },
    {
      key                    = "VAR_1"
      value                  = "https://staging"
      target                 = ["preview"]
      custom_environment_ids = [vercel_custom_environment.test_staging.id]
    },
+   {
+     key                    = "VAR_2"
+     value                  = "https://some-other-url"
+     target                 = ["preview"]
+     custom_environment_ids = [vercel_custom_environment.test_staging.id]
+   },
    {
      key    = "VAR_1"
      value  = "http://development"
      target = ["development"]
    },
+   {
+     key    = "VAR_2"
+     value  = "http://some-other-url"
+     target = ["development"]
+   }
  ]
}

resource "vercel_project_deployment_retention" "test" {
  project_id            = vercel_project.test.id
  team_id               = vercel_project.test.team_id
  expiration_preview    = "3m"
  expiration_production = "unlimited"
  expiration_canceled   = "1m"
  expiration_errored    = "1m"
}

resource "vercel_firewall_config" "test_firewall" {
  project_id = vercel_project.test.id

  managed_rulesets {
    owasp {
      gen  = { action = "deny" }
      java = { action = "deny" }
      lfi  = { action = "deny" }
      ma   = { action = "deny" }
      php  = { action = "deny" }
      rce  = { action = "deny" }
      rfi  = { action = "deny" }
      sd   = { action = "deny" }
      sqli = { action = "deny" }
      xss  = { action = "deny" }
    }
  }
}

Terraform and Vercel Provider Version

$ terraform --version
Terraform v1.11.3
on linux_amd64



provider "registry.terraform.io/vercel/vercel" {
  version     = "3.3.0"
  constraints = "~> 3.3.0"

Additional Details

No response

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions