<p>Setting </p>

<pre><code>export GOOGLE_APPLICATION_CREDENTIALS=${TF_CREDS}
</code></pre>

<p>solved the issue.</p>

CC BY-SA 4.0
2021-11-26T15:31:31.690Z
I can confirm that having a `gcloud auth` properly set does not matter, and you still need to set `GOOGLE_APPLICATION_CREDENTIALS` to your json credential file
Rafael Sierra
CC BY-SA 4.0
2023-07-23T19:53:51.607Z
Your question and answer inspired me to use 'GOOGLE_APPLICATION_CREDENTIALS' env_var in my use-case -- I was incorporating GCE-provider in a specific way. But what was also in my case, as in logs I saw errors, like: "GCE metadata ... not defined".

And after I set that env_var to the credentials file, the pod got up (though needed to solve other issues, of course).
acd
CC BY-SA 4.0
2019-06-04T20:04:07.147Z
2019-06-04T20:04:07.147Z
SamwelOpiyo
<p>I have tried initializing terraform code in Travis CI but an error keeps coming up;</p>

<pre><code>Failed to load state: 
Failed to open state file at 
gs://{{project_id}}/terraform/state/default.tfstate: 
Get 
https://storage.googleapis.com/{{project_id}}/terraform/state/default.tfstate: 
metadata: GCE metadata "instance/service-accounts/default/token" not defined
</code></pre>

<p>I have initialized gcloud(Google Cloud SDK), with a service account ie</p>

<pre><code>gcloud auth activate-service-account --key-file {{service-account-path}}
</code></pre>

<p>and set:</p>

<pre><code>export TF_CREDS={{service-account-path}}
</code></pre>

<p>but the error still comes up.</p>

<p>My terraform provider and backend are in the following format respectively:</p>

<pre><code>provider "google" {
  region  = "${var.region}"
  zone    = "${var.zone}"
  project = "${var.project_id}"
}
</code></pre>

<pre><code>terraform {
  backend "gcs" {
    bucket = "{{project_id}}"
    prefix = "terraform/state"
  }
}
</code></pre>

<p>I removed the default Google Cloud SDK thinking that it was causing a conflict when another which is specific to the usage in the CI/CD process is installed. This, however, did not help.</p>

<p>The .travis.yml looks like:</p>

<pre><code>language: node_js
sudo: true
node_js:
  - "12.2.0"
  - "12.0"
  - "11.0"
  - "10.0"
  - "9.10.0"

services:
  - docker

stages:
  - name: test
  - name: test-build
  - name: build
    if: |
      tag =~ /^v\d+\.\d+(\.\d+)?(-\S*)?$/ || \
      tag =~ /^dev\/v\d+\.\d+(\.\d+)?(-\S*)?$/ || \
      tag =~ /^release\/v\d+\.\d+(\.\d+)?(-\S*)?$/ || \
      tag =~ /^prod\/v\d+\.\d+(\.\d+)?(-\S*)?$/
  - name: deploy
    if: |
      tag =~ /^v\d+\.\d+(\.\d+)?(-\S*)?$/ || \
      tag =~ /^dev\/v\d+\.\d+(\.\d+)?(-\S*)?$/ || \
      tag =~ /^release\/v\d+\.\d+(\.\d+)?(-\S*)?$/ || \
      tag =~ /^prod\/v\d+\.\d+(\.\d+)?(-\S*)?$/

install:
  - yarn install

script:
  - yarn list
  # - yarn check
  # - yarn test

jobs:
  include:
    - stage: test-build
      install:
        - yarn install

      script:
        - yarn run build

    - stage: build
      install:
        - docker -v
      before_script:
        - git clone https://$GITHUB_TOKEN@github.com/{{github-user}}/devops_configurations.git ../devops_configurations/
        - cp ../devops_configurations/React/* .
      script:
        - docker login -u $DOCKER_HUB_USER -p $DOCKER_HUB_PASSWORD
        - docker build -t $DOCKER_HUB_USER/test_react:$(echo $TRAVIS_TAG | tr "/" .) -f Dockerfile-multibuild .
        - docker push $DOCKER_HUB_USER/test_react:$(echo $TRAVIS_TAG | tr "/" .)
        - docker rmi $DOCKER_HUB_USER/test_react:$(echo $TRAVIS_TAG | tr "/" .)
        - docker logout

    - stage: deploy
      install:
        # Install Google Cloud SDK
        - if [ ! -d "$HOME/google-cloud-sdk/bin" ]; then
            rm -rf $HOME/google-cloud-sdk;
            sudo rm -rf /usr/lib/google-cloud-sdk/;
            export CLOUDSDK_CORE_DISABLE_PROMPTS=1;
            curl https://sdk.cloud.google.com | bash &gt; /dev/null;
          fi
        # Add gcloud to $PATH
        - source /home/travis/google-cloud-sdk/path.bash.inc
        # Install Kubectl
        - if ! [ -x "$(command -v kubectl)" ]; then
            gcloud components install kubectl --quiet;
          fi
        # Install Terraform
        - if ! [ -x "$(command -v terraform)" ]; then
            TERRAFORM_VERSION=0.12.0;
            URL="https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_amd64.zip";
            wget $URL;
            unzip terraform_${TERRAFORM_VERSION}_linux_amd64.zip -d executables/;
            sudo mv executables/terraform /usr/local/bin/terraform;
            sudo chmod +x /usr/local/bin/terraform;
            rm terraform_${TERRAFORM_VERSION}_linux_amd64.zip;
            rm -rf executables/;
          fi
      before_script:
        - git clone https://$GITHUB_TOKEN@github.com/{{github-user}}/devops_configurations.git ../devops_configurations/
        - cd ../devops_configurations/terraform/
        - mkdir {{service-account-folder}}
        - echo $GCP_MAIN_SERVICE_ACCOUNT | base64 --decode &gt; {{service-account-path}}
        - gcloud auth activate-service-account --key-file {{service-account-path}}
        - export TF_CREDS={{service-account-path}}
      script:
        - terraform init
        - terraform plan -var-file variables.tfvars
          -var="react_docker_image=$DOCKER_HUB_USER/test_react:$(echo $TRAVIS_TAG | tr "/" .)"
          -var="vue_docker_image=$(terraform output terraform_gcp_gke_project_id || echo '$DOCKER_HUB_USER/test_vue:dev.v0.0.1-test')"
          -var="angular_docker_image=$(terraform output terraform_gcp_gke_project_id || echo '$DOCKER_HUB_USER/test_angular:dev.v0.0.1-test')"
          -target kubenetes_deployment.react-deployment
        - terraform apply -var-file variables.tfvars
          -var="react_docker_image=$DOCKER_HUB_USER/test_react:$(echo $TRAVIS_TAG | tr "/" .)"
          -var="vue_docker_image=$(terraform output terraform_gcp_gke_project_id || echo '$DOCKER_HUB_USER/test_vue:dev.v0.0.1-test')"
          -var="angular_docker_image=$(terraform output terraform_gcp_gke_project_id || echo '$DOCKER_HUB_USER/test_angular:dev.v0.0.1-test')"
          -target kubenetes_deployment.react-deployment
      after_script:
        - rm -rf {{service-account-folder}}
        - unset TF_CREDS
</code></pre>

<p>Any help would be appreciated.</p>

<terraform><google-cloud-platform><travis-ci>
GCE metadata not defined on Travis CI
SamwelOpiyo
# GCE metadata not defined on Travis CI

(Asked by: username_0 on 2019-06-04)

I have tried initializing terraform code in Travis CI but an error keeps coming up;
Failed to load state: 
Failed to open state file at 
gs://{{project_id}}/terraform/state/default.tfstate: 
Get 
https://storage.googleapis.com/{{project_id}}/terraform/state/default.tfstate: 
metadata: GCE metadata "instance/service-accounts/default/token" not defined

I have initialized gcloud(Google Cloud SDK), with a service account ie
gcloud auth activate-service-account --key-file {{service-account-path}}

and set:
export TF_CREDS={{service-account-path}}

but the error still comes up.
My terraform provider and backend are in the following format respectively:
provider "google" {
  region  = "${var.region}"
  zone    = "${var.zone}"
  project = "${var.project_id}"
}

terraform {
  backend "gcs" {
    bucket = "{{project_id}}"
    prefix = "terraform/state"
  }
}

I removed the default Google Cloud SDK thinking that it was causing a conflict when another which is specific to the usage in the CI/CD process is installed. This, however, did not help.
The .travis.yml looks like:
language: node_js
sudo: true
node_js:
  - "12.2.0"
  - "12.0"
  - "11.0"
  - "10.0"
  - "9.10.0"

services:
  - docker

stages:
  - name: test
  - name: test-build
  - name: build
    if: |
      tag =~ /^v\d+\.\d+(\.\d+)?(-\S*)?$/ || \
      tag =~ /^dev\/v\d+\.\d+(\.\d+)?(-\S*)?$/ || \
      tag =~ /^release\/v\d+\.\d+(\.\d+)?(-\S*)?$/ || \
      tag =~ /^prod\/v\d+\.\d+(\.\d+)?(-\S*)?$/
  - name: deploy
    if: |
      tag =~ /^v\d+\.\d+(\.\d+)?(-\S*)?$/ || \
      tag =~ /^dev\/v\d+\.\d+(\.\d+)?(-\S*)?$/ || \
      tag =~ /^release\/v\d+\.\d+(\.\d+)?(-\S*)?$/ || \
      tag =~ /^prod\/v\d+\.\d+(\.\d+)?(-\S*)?$/

install:
  - yarn install

script:
  - yarn list
  # - yarn check
  # - yarn test

jobs:
  include:
    - stage: test-build
      install:
        - yarn install

      script:
        - yarn run build

    - stage: build
      install:
        - docker -v
      before_script:
        - git clone https://$GITHUB_TOKEN@github.com/{{github-user}}/devops_configurations.git ../devops_configurations/
        - cp ../devops_configurations/React/* .
      script:
        - docker login -u $DOCKER_HUB_USER -p $DOCKER_HUB_PASSWORD
        - docker build -t $DOCKER_HUB_USER/test_react:$(echo $TRAVIS_TAG | tr "/" .) -f Dockerfile-multibuild .
        - docker push $DOCKER_HUB_USER/test_react:$(echo $TRAVIS_TAG | tr "/" .)
        - docker rmi $DOCKER_HUB_USER/test_react:$(echo $TRAVIS_TAG | tr "/" .)
        - docker logout

    - stage: deploy
      install:
        # Install Google Cloud SDK
        - if [ ! -d "$HOME/google-cloud-sdk/bin" ]; then
            rm -rf $HOME/google-cloud-sdk;
            sudo rm -rf /usr/lib/google-cloud-sdk/;
            export CLOUDSDK_CORE_DISABLE_PROMPTS=1;
            curl https://sdk.cloud.google.com | bash > /dev/null;
          fi
        # Add gcloud to $PATH
        - source /home/travis/google-cloud-sdk/path.bash.inc
        # Install Kubectl
        - if ! [ -x "$(command -v kubectl)" ]; then
            gcloud components install kubectl --quiet;
          fi
        # Install Terraform
        - if ! [ -x "$(command -v terraform)" ]; then
            TERRAFORM_VERSION=0.12.0;
            URL="https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_amd64.zip";
            wget $URL;
            unzip terraform_${TERRAFORM_VERSION}_linux_amd64.zip -d executables/;
            sudo mv executables/terraform /usr/local/bin/terraform;
            sudo chmod +x /usr/local/bin/terraform;
            rm terraform_${TERRAFORM_VERSION}_linux_amd64.zip;
            rm -rf executables/;
          fi
      before_script:
        - git clone https://$GITHUB_TOKEN@github.com/{{github-user}}/devops_configurations.git ../devops_configurations/
        - cd ../devops_configurations/terraform/
        - mkdir {{service-account-folder}}
        - echo $GCP_MAIN_SERVICE_ACCOUNT | base64 --decode > {{service-account-path}}
        - gcloud auth activate-service-account --key-file {{service-account-path}}
        - export TF_CREDS={{service-account-path}}
      script:
        - terraform init
        - terraform plan -var-file variables.tfvars
          -var="react_docker_image=$DOCKER_HUB_USER/test_react:$(echo $TRAVIS_TAG | tr "/" .)"
          -var="vue_docker_image=$(terraform output terraform_gcp_gke_project_id || echo '$DOCKER_HUB_USER/test_vue:dev.v0.0.1-test')"
          -var="angular_docker_image=$(terraform output terraform_gcp_gke_project_id || echo '$DOCKER_HUB_USER/test_angular:dev.v0.0.1-test')"
          -target kubenetes_deployment.react-deployment
        - terraform apply -var-file variables.tfvars
          -var="react_docker_image=$DOCKER_HUB_USER/test_react:$(echo $TRAVIS_TAG | tr "/" .)"
          -var="vue_docker_image=$(terraform output terraform_gcp_gke_project_id || echo '$DOCKER_HUB_USER/test_vue:dev.v0.0.1-test')"
          -var="angular_docker_image=$(terraform output terraform_gcp_gke_project_id || echo '$DOCKER_HUB_USER/test_angular:dev.v0.0.1-test')"
          -target kubenetes_deployment.react-deployment
      after_script:
        - rm -rf {{service-account-folder}}
        - unset TF_CREDS

Any help would be appreciated.

**Tags:** terraform, google-cloud-platform, travis-ci


---

## 1 answers

### Answer by username_0 on 2019-06-04. Score: 5

Setting 
export GOOGLE_APPLICATION_CREDENTIALS=${TF_CREDS}

solved the issue.

> **Comments:**
> **username_1**: I can confirm that having a `gcloud auth` properly set does not matter, and you still need to set `GOOGLE_APPLICATION_CREDENTIALS` to your json credential file  
> **username_2**: Your question and answer inspired me to use 'GOOGLE_APPLICATION_CREDENTIALS' env_var in my use-case -- I was incorporating GCE-provider in a specific way. But what was also in my case, as in logs I saw errors, like: "GCE metadata ... not defined".  And after I set that env_var to the credentials file, the pod got up (though needed to solve other issues, of course).  

---

