0

I want to use a GitLab Runner to deploy to AWS with Terraform. I have setup AWS credentials in GitLab "Variables" (AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY). I must be misunderstanding how .gitlab-cy.yml performs string interpolation because I cannot get the credentials to populate.

The stage in question looks like this:

validate:
  stage: validate
  dependencies:
    - lint
    - unit
  image:
    name: hashicorp/terraform:light
    entrypoint:
      - "/usr/bin/env"
      - "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
      - "AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}"
      - "AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}"
  before_script:
    - rm -rf terraform/test/.terraform
    - terraform --version
    - terraform init -input=false -backend-config="access_key=${AWS_ACCESS_KEY_ID}" -backend-config="secret_key=${AWS_SECRET_ACCESS_KEY}" terraform/test
  script:
    - terraform validate

The pipeline fails without fail on the terraform init command. However, just to confirm I'm not crazy, I did try a pipeline run with the credentials hardcoded and it worked (I also immediately learned about how to permanently delete commits and pipelines).

From the relevant GitLab documentation on variable usage, I don't see anything obviously wrong.

Error message:

Initializing the backend...
 Error: error using credentials to get account ID: error calling sts:GetCallerIdentity: InvalidClientTokenId: The security token included in the request is invalid.
Kwhitejr
  • 2,206
  • 5
  • 29
  • 49
  • The problem turned out to be Protected Branches. If you "protect" a Variable, then the variable is not available to branches that are not designated Protected. – Kwhitejr Feb 03 '20 at 04:59
  • I am pretty sure you should not be setting up environment variables in a Docker entrypoint. – Matthew Schuchard Feb 03 '20 at 15:55
  • @MattSchuchard Fair enough, but do you have a recommended alternative? I was working from these examples: https://medium.com/@me.daniel.meier/handling-aws-with-terraform-and-gitlab-ci-ed83d17e018e https://medium.com/@dbourgeois23/manage-terraform-with-gitlab-ci-5c24005eb62a – Kwhitejr Feb 04 '20 at 16:17
  • Have you tried the `environment` Gitlab key instead? – Matthew Schuchard Feb 04 '20 at 16:39
  • I'm not seeing any difference from what I've done here https://docs.gitlab.com/ee/ci/variables/ – Kwhitejr Feb 05 '20 at 04:32

0 Answers0