Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: requests to create environment variables for custom environments are created with the wrong target #255

Closed
qlimenoque opened this issue Jan 15, 2025 · 8 comments · May be fixed by #256

Comments

@qlimenoque
Copy link

Hi, I'm running into an error when I'm trying to create an Environment Variable resource for Custom Environment

tldr

Expected behavior:
The API should create an environment variable only for the Custom Environment with the corresponding ID

How it works now:
The API tries to create an environment variable with a target that was not specified anywhere.

Details

This is an example of the resource I am trying to create

resource “vercel_project_environment_variable” “test_sensitive” {
  for_each = try(var.config.env.test.sensitive, {})
  project_id = vercel_project.this.id
  comment = “Sensitive test environment variable”
  key = each.key
  value = each.value
  sensitive = true
  custom_environment_ids = [
    vercel_custom_environment.test.id,
  ]

  lifecycle {
    create_before_destroy = true
  }
}

But I get the following error

Could not create project environment variable, unexpected error: ENV_ALREADY_EXISTS - Another Environment Variable with the same Name and Environment exists in your project. Remove it or choose a different Name or Environment: unable to list environment variables to detect conflict: %!s(<nil>)

First of all, I noticed that the latest version of the API for /projects/ is v10, but the provider makes requests to v9.

After updating the version of the API used locally, I continued debugging POST requests and found out that the Vercel API was trying to create an environment variable with an incorrect target that was not even specified in the request (below is an example of a cURL request that I caught)

Request

curl 'https://api.vercel.com/v10/projects/prj_XXXXXXXXXXXXX/env?teamId=team_YYYYYYYYYYY' \
-X POST \
-H 'Host: api.vercel.com' \
-H 'User-Agent: terraform-provider-vercel/2.8.0-SNAPSHOT-35219ba' \
-H 'Authorization: Bearer TOKEN' \
-H 'Content-Type: application/json' \
--data-raw '{“key”: “NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY”, “value”: “TEST”, “customEnvironmentIds”:[“env_TTTTTTTTTTTTTTTTTT”], 
“type”: “sensitive”, “comment”: “Sensitive test environment variable”}'

Response:

{
  { “error”: {
    “code": “ENV_CONFLICT”,
    “message": “A variable with the name `NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY` already exists for the target preview.”
    “key": “key”,
    “envVarKey": “NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY”
  },
  “failed": [
    {
      “error": {
        “code": “ENV_CONFLICT”,
        “message": “A variable with the name `NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY` already exists for the target preview.”
        “key": “key”,
        “envVarKey": “NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY”
      }
    }
  ]
}
@dglsparsons
Copy link
Collaborator

Hi @qlimenoque, thanks for raising this.

It looks like this isn't trying to create an env var with a target, but it's incorrectly detecting that it conflicts with an existing environment variable.

I've reached out to the team that maintain custom environments, and we are looking into the cause together. Ideally, we'll try to get this fixed without any need to update the TF provider, as it's most likely a bug in the API regardless of version.

I will keep you posted. 🙏

@qlimenoque
Copy link
Author

Hi @dglsparsons! Thanks for such a rapid answer
My colleague and I made some changes, which you can see in the #256, maybe it might be helpful somehow :)

Also, is it possible to make upsert request? As I can see, the documentation has optional parameters, that include upsert by default. I'd like to update the environment variable instead of having ENV_CONFLICT response

@dglsparsons
Copy link
Collaborator

Hi @qlimenoque, we've released a fix to our API for this. Can you give it another go and let me know if you still experience the same issue?

Thanks 🙏

@qlimenoque
Copy link
Author

qlimenoque commented Jan 17, 2025

Thanks for the update! It works well now
I tested adding a new sensitive value to custom environment, then changed it and reapplied.
Everything works fine 🎉

@wallwhite
Copy link

Thank you for the fast fixes and responses to our issues—we greatly appreciate it.

@qlimenoque
Copy link
Author

@dglsparsons
It does not affect the initial problem, but affects error handling.
Please, take a look at https://github.com/vercel/terraform-provider-vercel/pull/256/files#diff-de942bfad6da95da7d11b3ccf9d5dd6698d3390e531d36f5c8279822287a4f4cR53
It looks like the wrong error variable is checked

@dglsparsons
Copy link
Collaborator

That's a good catch. i'll have a look at the PR as soon as I am able. Thanks for raising it! ❤

@qlimenoque
Copy link
Author

Please take a look at https://github.com/vercel/terraform-provider-vercel/blob/main/client/environment_variable.go#L98-L109. I believe it should display the environment ID in case of failure to make it easier to import resources into the Terraform state when they are not present (e.g., added via the Web UI but later managed with Terraform).

Currently, it does not support Custom Environments. If an Environment Variable is added only to a Custom Environment, the response returns a 403 error code with "target": [] and no customEnvironmentIds.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants