Friday, November 19, 2021

[SOLVED] Terraform init problem: too many command line arguments

Issue

What I'm trying to do: I have set up a self hosted gitlab instance, and I'm working on automating the terraform process with GitLab CI, for which I have to set up a terraform backend. I followed the steps in the official documentation, but unfortunately I run into an error when running the pipeline. This code is found in my .gitlab-ci.yml file, so it runs on my GitLab runner, which uses a docker image based on python:3.9-buster (Debian)

Here is the code in question, of course I edited out the IP address:

- terraform init \ 
        -backend-config="address=http://ipaddress//api/v4/projects/3/terraform/state/terraform-state/" \ 
        -backend-config="lock_address=http://ipaddress//api/v4/projects/3/terraform/state/terraform-state/lock" \ 
        -backend-config="unlock_address=http://ipaddress//api/v4/projects/3/terraform/state/terraform-state/lock" \ 
        -backend-config="username=root" \ 
        -backend-config="password=$ACCESSTOKEN" \ 
        -backend-config="lock_method=POST" \ 
        -backend-config="unlock_method=DELETE" \ 
        -backend-config="retry_wait_min=5"

Unfortunately I'm getting this error when the above command gets executed:

Too many command line arguments. Did you mean to use -chdir?

Solution

Just in case anyone would get stuck on a similar issue and happens to find this post, changing my script to this format solved the issue:

  - export TF_ADDRESS=http://ipaddress//api/v4/projects/3/terraform/state/terraform-state/
  - export TF_HTTP_ADDRESS=${TF_ADDRESS}
  - export TF_HTTP_LOCK_ADDRESS=${TF_ADDRESS}/lock
  - export TF_HTTP_LOCK_METHOD=POST
  - export TF_HTTP_UNLOCK_ADDRESS=${TF_ADDRESS}/lock
  - export TF_HTTP_UNLOCK_METHOD=DELETE
  - export TF_HTTP_USERNAME=root
  - export TF_HTTP_PASSWORD=$ACCESSTOKEN
  - export TF_HTTP_RETRY_WAIT_MIN=5
  - terraform init -reconfigure


Answered By - Bonewind