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

Credentials not found by Terraform - "Required token could not be found" #437

Open
colin-nolan opened this issue Oct 2, 2024 · 1 comment

Comments

@colin-nolan
Copy link

colin-nolan commented Oct 2, 2024

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

credsFile = process.env.TF_CLI_CONFIG_FILE ? process.env.TF_CLI_CONFIG_FILE : credsFile;
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.

@yocean-tseng
Copy link

I would love to work on this ! It seems to be an easy change. And yes ~/.terraform.d/credentials.tfrc.json is where the credentials should be set.

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

No branches or pull requests

2 participants