hashicorp / terraform-provider-tfe

Official HCP Terraform and Terraform Enterprise provider, maintained by HashiCorp. Provision HCP Terraform or Terraform Enterprise - with Terraform!
https://registry.terraform.io/providers/hashicorp/tfe
Mozilla Public License 2.0
165 stars 156 forks source link

Terraform import fails with [ERROR] Error parsing provider name "" #630

Open dkirrane opened 2 years ago

dkirrane commented 2 years ago

Terraform Cloud/Enterprise version

Terraform Cloud

Terraform version

Terraform v1.2.9
on linux_amd64
+ provider registry.terraform.io/hashicorp/azuread v2.28.1
+ provider registry.terraform.io/hashicorp/azurerm v3.23.0
+ provider registry.terraform.io/hashicorp/random v3.4.3  
+ provider registry.terraform.io/hashicorp/tfe v0.36.1

Issue

My terrafrom plan uses tfe_outputs datasource to lookup remote state. When I run terraform import on this plan I hit below error Locally, I have also tried set TFE_TOKEN env variable before running terraform import but hit the same issue.

+ terraform import azurerm_key_vault.this /subscriptions/***/my-kv
Error: 47:53.797Z [ERROR] provider.terraform-provider-tfe_v0.36.1_x5: [ERROR] Error parsing provider name "": Invalid provider source string: The "source" attribute must be in the format "[hostname/][namespace/]name"
Error: 47:54.499Z [ERROR] provider.terraform-provider-tfe_v0.36.1_x5: [ERROR] Error parsing provider name "": Invalid provider source string: The "source" attribute must be in the format "[hostname/][namespace/]name"
Error: 47:54.691Z [ERROR] provider.terraform-provider-tfe_v0.36.1_x5: [ERROR] Error reading CLI config or credentials file /home/runner/.terraform.d/credentials.tfrc.json: open /home/runner/.terraform.d/credentials.tfrc.json: no such file or directory
Error: 47:54.976Z [ERROR] provider.terraform-provider-tfe_v0.36.1_x5: [ERROR] Error reading CLI config or credentials file /home/runner/.terraform.d/credentials.tfrc.json: open /home/runner/.terraform.d/credentials.tfrc.json: no such file or directory
brandonc commented 2 years ago

Hello, there. Despite the error regarding being unable to read the CLI config, you may not be experiencing an authentication failure. When a token is not authorized to read from a tfe datasource, you should see the following error:

data.tfe_outputs.foo: Reading...
╷
│ Error: Error reading remote state output
│
│   with data.tfe_outputs.foo,
│   on main.tf line 20, in data "tfe_outputs" "foo":
│   20: data "tfe_outputs" "foo" {
│
│ Error reading remote state output: Error reading workspace: unauthorized
╵

Instead, can you check to ensure that the outputs you expect are available? Create a minimal config that reads the tfe_outputs and writes them to an output, for example:

provider "tfe" {
  token = "foo"
}

# ---

data "tfe_outputs" "foo" {
  organization = "hashicorp"
  workspace = "terraform-minimal"
}

resource "null_resource" "null" {
  triggers = {
    "key" = data.tfe_outputs.foo.values.a-string
  }
}

output "sensitive-stuff" {
  value = data.tfe_outputs.foo.values
  sensitive = true
}
dkirrane commented 2 years ago

@brandonc The workspace I'm importing into has an empty state file. It hasn't been run yet. The data "tfe_outputs" does not exist in state yet.

Does this mean I'll have to run plan/apply first so Terraform state has the tfe_outputs datasources populated. Before importing other resources?

The remote workspace it's reading does have the outputs. The TFE_TOKEN is set as env variable on the workspace itself (and I also set it locally when running terraform import). It's a Team token and has admin privileges' on the Terrafrom Cloud workspaces (admin on both the one I'm running terrform import on and the remote workspace I'm readin from via tfe_outputs).

Terraform config has

provider "tfe" {
  # Gets Terraform Cloud token from env variable
  # TFE_TOKEN
}

I have also tried setting TF_TOKEN_app_terraform_io env variable to get rid of the [ERROR] Error reading CLI config or credentials file. But that didn't work

dkirrane commented 2 years ago

I got rid of the the [ERROR] Error reading CLI config or credentials file by manually creating the credentials.tfrc.json file. Setting the env variables TFE_TOKEN or TF_TOKEN_app_terraform_io was not enough.

mkdir ~/.terraform.d
cat >~/.terraform.d/credentials.tfrc.json<<EOL
{
  "credentials": {
    "app.terraform.io": {
      "token": "${TFE_TOKEN}"
    }
  }
}
EOL

However, the other error still exists on terraform import

Error: 12:11.812Z [ERROR] provider.terraform-provider-tfe_v0.37.0_x5: [ERROR] Error parsing provider name "": Invalid provider source string: The "source" attribute must be in the format "[hostname/][namespace/]name"
brandonc commented 1 year ago

@dkirrane I'm sorry I never followed up with you. The provider can be authenticated (in order of decreasing priority) using the token provider config, the TFE_TOKEN environment variable, and finally terraform login credentials.

tfe_outputs are only populated once the target workspace has state applied. If your data source points at a workspace that has outputs, that should be enough.

The import error appears to be caused by the source string within the terraform/provider_requirements block and not the outputs in particular. Hopefully you figured this out long ago, but if you are still having trouble let me know.

ashutoshrathore commented 1 year ago

I have this error in my runs, has anyone got the fix?

[ERROR] Error reading CLI config or credentials file /home/tfc-agent/.tfc-agent/component/terraform/runs/