how to save terraform state in gitlab

0
683

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 version.tf

first methode – type all paramater on terraform init cli

terraform init \
    -backend-config="address=https://gitlab.com/api/v4/projects/<YOUR-PROJECT-ID>/terraform/state/<YOUR-STATE-NAME>" \
    -backend-config="lock_address=https://gitlab.com/api/v4/projects/<YOUR-PROJECT-ID>/terraform/state/<YOUR-STATE-NAME>/lock" \
    -backend-config="unlock_address=https://gitlab.com/api/v4/projects/<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" \
    -backend-config="retry_wait_min=5"

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=https://gitlab.com/api/v4/projects/35392811/terraform/state/tfstate-sekolahlinux" \
    -backend-config="lock_address=https://gitlab.com/api/v4/projects/35392811/terraform/state/tfstate-sekolahlinux/lock" \
    -backend-config="unlock_address=https://gitlab.com/api/v4/projects/35392811/terraform/state/tfstate-sekolahlinux/lock" \
    -backend-config="username=sekolahlinux" \
    -backend-config="password=xxxxx-xxxxxxxxxxxxxxxxxxxxxx" \
    -backend-config="lock_method=POST" \
    -backend-config="unlock_method=DELETE" \
    -backend-config="retry_wait_min=5"

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

create version.tf in fill code like below

terraform {
  backend "http" {
    address = "https://gitlab.com/api/v4/projects/35392811/terraform/state/tfstate-sekolahlinux"
    lock_address = "https://gitlab.com/api/v4/projects/35392811/terraform/state/tfstate-sekolahlinux/lock"
    unlock_address = "https://gitlab.com/api/v4/projects/35392811/terraform/state/tfstate-sekolahlinux/lock"
    lock_method = "POST"
    unlock_method = "DELETE"
    retry_wait_min = 5
  }
}

after you create version.tf like above, run this cli below in same folder version.tf 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:

  • https://docs.gitlab.com/ee/user/infrastructure/iac/terraform_state.html
  • https://docs.gitlab.com/ee/user/profile/personal_access_tokens.html
  • https://docs.gitlab.com/ee/user/profile/personal_access_tokens.html#personal-access-token-scopes