使用 GitLab 管理 Terraform 狀態

做法

在專案根目錄新增 .gitlab-ci.yml 檔。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
include:
- template: Terraform/Base.gitlab-ci.yml

variables:
TF_ROOT: ${CI_PROJECT_DIR}/staging
TF_STATE_NAME: staging
# TF_AUTO_DEPLOY: "true"

stages:
- validate
- test
- build
- deploy
- cleanup

fmt:
extends: .terraform:fmt
needs: []

validate:
extends: .terraform:validate
needs: []

build:
extends: .terraform:build
environment:
name: $TF_STATE_NAME
action: prepare

deploy:
extends: .terraform:deploy
dependencies:
- build
environment:
name: $TF_STATE_NAME
action: start

在指定資料夾新增 backend.tf 檔。

1
2
3
4
terraform {
backend "http" {
}
}

在 GitLab 專案設定環境變數。

1
2
3
AWS_DEFAULT_REGION=
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=

推送專案。

轉移狀態

在 GitLab 生成一個存取令牌,並新增 migrate.sh 檔,然後執行腳本。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
PROJECT_ID="<gitlab-project-id>"
TF_USERNAME="<gitlab-username>"
TF_PASSWORD="<gitlab-personal-access-token>"
TF_ADDRESS="https://gitlab.com/api/v4/projects/${PROJECT_ID}/terraform/state/old-state-name"

terraform init \
-backend-config=address=${TF_ADDRESS} \
-backend-config=lock_address=${TF_ADDRESS}/lock \
-backend-config=unlock_address=${TF_ADDRESS}/lock \
-backend-config=username=${TF_USERNAME} \
-backend-config=password=${TF_PASSWORD} \
-backend-config=lock_method=POST \
-backend-config=unlock_method=DELETE \
-backend-config=retry_wait_min=5

修改腳本,並執行。

1
2
3
4
5
6
7
8
9
10
11
12
TF_ADDRESS="https://gitlab.com/api/v4/projects/${PROJECT_ID}/terraform/state/new-state-name"

terraform init \
-migrate-state \
-backend-config=address=${TF_ADDRESS} \
-backend-config=lock_address=${TF_ADDRESS}/lock \
-backend-config=unlock_address=${TF_ADDRESS}/lock \
-backend-config=username=${TF_USERNAME} \
-backend-config=password=${TF_PASSWORD} \
-backend-config=lock_method=POST \
-backend-config=unlock_method=DELETE \
-backend-config=retry_wait_min=5

參考資料