403 Error when using Terraform with GitLab CI

I am using Terraform for creating Google resources and it’s deploying K8s cluster, custom roles using service_account key and it’s working locally fine using key credentials but when I created gitlabCI using below code it’s giving me this below error.


here is the GitlabCI code

image:
   name: hashicorp/terraform:light
   entrypoint:
     - '/usr/bin/env'
     - 'PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin'

before_script:
     -  rm -rf .terraform
     -  terraform --version
     -  mkdir -p ./creds
     -  echo $SERVICEACCOUNT | base64 -d > ./creds/serviceaccount.json
     -  terraform init
cache:
    key: terraform
    paths:
      - .terraform
stages:
      - validate
      - plan
      - apply

validate:
   stage: validate
   script:
     - terraform validate

plan:
   stage: plan
   script:
     - terraform plan -out "planfile"
   dependencies:
     - validate
   artifacts:
     paths:
       - planfile

apply:
   stage: apply
   script:
     - terraform apply --auto-approve "planfile"
   dependencies:
     - plan
   when: manual

Any suggestion or help?

1 Like