hashicorp / hashicorp/setup-terraform
Credentials not found by Terraform - "Required token could not be found"
- Dominant language
- JavaScript
- Stars
- 1.6k
- Forks
- 288
- Avg merge
- 23h 21m
- Merged PRs (30d)
- 3
Description
If we supply the action with `cli_config_credentials_token`, followed by a Terraform init step:
```
- uses: hashicorp/setup-terraform@v3
with:
cli_config_credentials_token: ${{ inputs.hcp_terraform_api_key }}
- name: Run Terraform init
shell: bash
run: |
cd ${{ inputs.checkout_directory }}/terraform/environments/${{ inputs.deployment }}
terraform init -input=false
```
the result is an error on the latter step:
```
│ Error: Required token could not be found
│
│ Run the following command to generate a token for app.terraform.io:
│ terraform login
```
The reason appears to be that Terraform is looking for `~/.terraform.d/credentials.tfrc.json` (referenced in `terraform login --help`), whereas the action is writing the credentials to `~/.terraformrc`. Adding this intermediate step subsequently works around the issue:
```
- name: Symlink Terraform credentials to alternative location
shell: bash
run: |
mkdir -p ~/.terraform.d
ln -s ~/.terraformrc ~/.terraform.d/credentials.tfrc.json
```
The action has the (undocumented) ability to override the location with `TF_CLI_CONFIG_FILE` https://github.com/hashicorp/setup-terraform/blob/76fd07bad386ea4e790eca43250a461a5fa7f716/lib/setup-terraform.js#L112 but this didn't appear to work (this may well have been user error though!).
It could be helpful if the config file location could be set via an input parameter. I don't know enough about Terraform, and the issue to say whether `~/.terraform.d/credentials.tfrc.json` should be getting used instead of `.terraformrc`.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.