gitlabterraformworkspace

GitLab repository for terraform state can't permit create terraform workspace


I'[m trying to save the terraform state in my GitLab env.

I created this script to set it (and works fine):

PROJECT=my-project-name
PROJECT_ID=1000
STATE_ADDR="https://my.gitlab.local/api/v4/projects/$PROJECT_ID/terraform/state/${WORKSPACE}_${PROJECT}"
terraform init \
    -backend-config="username=$GIT_USERNAME" \
    -backend-config="password=$GIT_ACCESS_TOKEN" \
    -backend-config="address=${STATE_ADDR}" \
    -backend-config="lock_address=${STATE_ADDR}/lock" \
    -backend-config="unlock_address=${STATE_ADDR}/lock" 
unset WORKSPACE

my-project-name/terraform$ ./terraform_init.sh 

WORKSPACE SELECTED: my-env

Initializing the backend...
Initializing modules...

Initializing provider plugins...
- Reusing previous version of hashicorp/aws from the dependency lock file
- Using previously-installed hashicorp/aws v4.60.0

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

This is my backend.tf:

terraform {
  backend "http" {
    lock_method = "POST"
    unlock_method = "DELETE"
    retry_wait_min = 5
  }
}

but when I try to create a workspace I receive the error below:

my-project-name/terraform$ terraform workspace new my-env
Failed to get configured named states: workspaces not supported

This is the folder structure:

gitlab-repo$ tree
.
├── docs
│   └── DOCUMENTATIONS.md
└── terraform
    ├── main.tf
    ├── backend.tf
    ├── modules
    │   ├── module1
    │   │   ├── main.tf
    │   │   ├── outputs.tf
    │   │   ├── variables.tf
    │   │   └── versions.tf
    │   ├── module2
    │   │   ├── main.tf
    │   │   ├── outputs.tf
    │   │   ├── variables.tf
    │   │   └── versions.tf
    │   ├── module3
    │   │   ├── main.tf
    │   │   ├── outputs.tf
    │   │   ├── variables.tf
    │   │   └── versions.tf
    ├── output.tf
    ├── terraform_init.sh
    ├── variables.tf

My gitLab version is: GitLab Enterprise Edition 15.7.5-ee And I'm running Terraform v1.4.2


Solution

  • I solved creating this script terraform_init.sh: set -a

    if [ -z $GIT_USERNAME ]
    then
      echo "GIT_USERNAME not defined in environment"
      exit 1
    fi    
    if [ -z $GIT_ACCESS_TOKEN ]
    then
      echo "GIT_ACCESS_TOKEN not defined in environment"
      exit 1
    fi
    
    echo -n "terraform workspace (dev|test|prod)? "
    read  WORKSPACE
    echo ""
    echo "WORKSPACE SELECTED: $WORKSPACE"
    
    if [[ ! "$WORKSPACE" =~ ^(dev|test|prod)$ ]]; then
      echo "invalid workspace selected"
      exit 1
    fi
      
    PROJECT=project-name
    PROJECT_ID=0000
    STATE_ADDR="https://mygitlab/api/v4/projects/$PROJECT_ID/terraform/state/${WORKSPACE}_${PROJECT}"
    terraform init \
        -backend-config="username=$GIT_USERNAME" \
        -backend-config="password=$GIT_ACCESS_TOKEN" \
        -backend-config="address=${STATE_ADDR}" \
        -backend-config="lock_address=${STATE_ADDR}/lock" \
        -backend-config="unlock_address=${STATE_ADDR}/lock" \
        
    unset WORKSPACE
    

    I run ./terraform_init.sh and create the workspace.
    To change workspace I delete .terraform/ and .terraform.lock.hcl and re run ./terraform_init.sh