Introduction
Problem: While using tfe_provider to create HCP Terraform related resources from a workspace present in HCP Terraform/ terraform and you are getting unauthorised error when triggering run.
Cause: While running a plan, you will get multiple errors like:
Error reading configuration of workspace ws-xxxx: unauthorized
OR
Error retrieving workspace: unauthorized
This provider requires a HCP Terraform and Terraform Enterprise API token in order to manage resources. See Authentication above for more information.This error message is usually caused because of incorrect passed token.
-
the token used doesn't have sufficient permissions.
- either the token is expired or value is not being received by variable token/TFE_TOKEN.
-
the user who triggered run doesn't have correct permissions.
Overview of possible solutions (if applicable)
Solutions:
-
The user should have `Manage Access` to workspace from where he is creating resource at organisation level and write access at workspace level. Here is the article related to permission model of HCP Terraform.
- Validate by hardcoding the value in variable like below. If that fixes the issue, it means the value is not being received by variable if passed through nested input.
-
provider "tfe" {
hostname = var.hostname # Optional, defaults to HCP Terraform `app.terraform.io`
token = "xxxxxxxxxxxxxxxxx"
version = "~> 0.62.0"
}
# Create an organization
resource "tfe_organization" "org" {
# ...
} - To check if token is valid or not by below commands. If invalid, this command will fail. Then, regenerate and pass the active token.
export TOKEN=<TFE_Token>
curl \
--header "Authorization: Bearer $TOKEN" \
--header "Content-Type: application/vnd.api+json" \
https://<TFE_URL>/api/v2/organizations/<Organization_Name>/workspaces
Please replace <TFE_URL> and <Organization_Name>
Outcome: You will be able to create HCP Terraform resources using tfe provider.
Additional Information:
If you're still experiencing issues, please contact HCP Terraform Support by submitting a ticket through our support portal