how to save terraform state in gitlab


hello guys, si this is my first articel with english, so if i have wrong when i write this, yes i still learn

so in this article i will explain to you how to store terraform state to gitlab, first you must have gitlab account of course

if you already have gitlab account, you must generate personal token access, and you can generate from tutorial below

- In the top-right corner, select your avatar.
- Select Edit profile.
- On the left sidebar, select Access Tokens. 
- Enter a name and optional expiry date for the token.
- Select the desired scopes. (select API scope)
- Select Create personal access token.
- Save the personal access token, because after you leave the page, you no longer have access to the token.

you can implement store terraform state to gitlab with 2 way, for first way you can type all paramater on terraform init cli, and the second way you can put all parameter config except paramater username and password in file

first methode – type all paramater on terraform init cli

terraform init \
    -backend-config="address=<YOUR-PROJECT-ID>/terraform/state/<YOUR-STATE-NAME>" \
    -backend-config="lock_address=<YOUR-PROJECT-ID>/terraform/state/<YOUR-STATE-NAME>/lock" \
    -backend-config="unlock_address=<YOUR-PROJECT-ID>/terraform/state/<YOUR-STATE-NAME>/lock" \
    -backend-config="username=<YOUR-USERNAME>" \
    -backend-config="password=<YOUR-ACCESS-TOKEN>" \
    -backend-config="lock_method=POST" \
    -backend-config="unlock_method=DELETE" \

replace variable below

  • <YOUR-PROJECT-ID> = replace with project ID, you can find project ID in main repo page
  • <YOUR-STATE-NAME> = replace this one with values as you want direct on command line
  • <YOUR-USERNAME> = replace this one with your gitlab username
  • <YOUR-ACCESS-TOKEN> = replace this one with access token from step generate personal token access

if all variable already replace, the command will like below

terraform init \
    -backend-config="address=" \
    -backend-config="lock_address=" \
    -backend-config="unlock_address=" \
    -backend-config="username=sekolahlinux" \
    -backend-config="password=xxxxx-xxxxxxxxxxxxxxxxxxxxxx" \
    -backend-config="lock_method=POST" \
    -backend-config="unlock_method=DELETE" \

second methode – put all parameter config except paramater username and password in file

create in fill code like below

terraform {
  backend "http" {
    address = ""
    lock_address = ""
    unlock_address = ""
    lock_method = "POST"
    unlock_method = "DELETE"
    retry_wait_min = 5

after you create like above, run this cli below in same folder location

terraform init -backend-config="username=sekolahlinux" -backend-config="password=xxxxx-xxxxxxxxxxxxxxxxxxxxxx"


final step

after you chose one of two methode above please run command below

terraform plan
terraform apply

after you run command above, you can check on page gitlab > infrastructure > terraform, you will see the state name create on page terraform like below


reference link:



Please enter your comment!
Please enter your name here