IBM Cloud์—์„œ tfstate ํŒŒ์ผ ๊ด€๋ฆฌํ•˜๊ธฐ

 

์ด ๊ธ€์€ ๋‹ค์Œ ๊ธ€์„ ๋ฒˆ์—ญํ•˜์—ฌ ํฌ์ŠคํŒ…ํ•˜์˜€์Šต๋‹ˆ๋‹ค.
Link : https://github.com/l2fprod/serverless-terraform-backend

Terraform์€ ๋‹ค์–‘ํ•œ backend types๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ์ค‘ http ๋ฐฑ์•ค๋“œ ๋ฐฉ์‹์€ REST Client ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Terraform states ํŒŒ์ผ์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

์ด ํ”„๋กœ์ ํŠธ๋Š” IBM์˜ ์„œ๋ฒ„๋ฆฌ์Šค IBM Cloud Functions์™€ IBM Cloud Object Storage๋ฅผ Terraform states์˜ REST beckend๋กœ ์‚ฌ์šฉํ•˜์—ฌ optional state locking์™€ versioning ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

Cloud Object Storage ์ƒ์„ฑ

1. IBM Cloud console์— ์ ‘์†ํ•˜์—ฌ Cloud Object Storage ์„œ๋น„์Šค๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

2. Terraform states๊ฐ€ ์ €์žฅ๋  bucket์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
Create Bucket

3. ์„œ๋น„์Šค Credential์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
Create Credential

4. ์›ํ•˜์‹œ๋Š” ์ง€์—ญ์˜ API endpoint๋ฅผ ๊ธฐ์–ตํ•ด๋‘ก๋‹ˆ๋‹ค.
API Endpoint

์„œ๋ฒ„๋ฆฌ์Šค ๋ฐฑ์—”๋“œ ์ƒ์„ฑ

5. git clone ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ํŒŒ์ผ์„ ๋‚ด๋ ค๋ฐ›์Šต๋‹ˆ๋‹ค.
git clone https://github.com/l2fprod/serverless-terraform-backend.git

6. actions/template.local.env ํŒŒ์ผ์„ actions/local.env ๋กœ ๋ณต์‚ฌํ•ฉ๋‹ˆ๋‹ค.

cp actions/template.local.env actions/local.env

7. actions/local.env๋ฅผ ํ…์ŠคํŠธ ์—๋””ํ„ฐ๋กœ ์—ด์–ด Cloud Object Storage์˜ Credential์„ ์ž‘์„ฑํ•œ ๋’ค ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

8. ๊ธฐ ์ •์˜๋œ ์„œ๋ฒ„๋ฆฌ์Šค ๋ฐฑ์•ค๋“œ์˜ action์„ ๋ฐฐํฌํ•ฉ๋‹ˆ๋‹ค.

cd actions
./deploy --install

9. API Gateway๋ฅผ ํ†ตํ•ด ํ•ด๋‹น Action์„ Expose ํ•ฉ๋‹ˆ๋‹ค.

cd actions
./deploy --installApi

10. API Gateway endpoint๋ฅผ ๊ธฐ์–ตํ•ด๋‘ก๋‹ˆ๋‹ค. ์•„๋ž˜ ๋งํฌ์™€ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค. https://service.us.apiconnect.ibmcloud.com/gws/apigateway/api/abcdefabecdef0192837465/terraform/1/backend

Terraform ์„ค์ •

11. terraform/backend.tf์„ ํŽธ์ง‘ํ•ฉ๋‹ˆ๋‹ค.

12. address๋ฅผ API Gateway endpoint๋กœ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.

์ฃผ์†Œ์˜ query ๋งค๊ฐœ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๋‹น state ํŒŒ์ผ์—์„œ env ์ด๋ฆ„์„ ์„ค์ •ํ•˜๊ฑฐ๋‚˜ versioning์„ ์‚ฌ์šฉํ•˜๋„๋ก ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ด ์ฃผ์†Œ๋Š” us/south/staging์„ ํ‚ค๋กœ ์‚ฌ์šฉํ•˜์—ฌ ์ƒํƒœ๋ฅผ ์œ ์ง€ํ•˜๊ณ  versioning์„ ํ•ฉ๋‹ˆ๋‹ค.: https://service.us.apiconnect.ibmcloud.com/gws/apigateway/api/abcdefabecdef0192837465/terraform/1/backend?env=us/south/staging&versioning=true

13. ๋งŒ์•ฝ locking์„ ์›ํ•˜์‹ ๋‹ค๋ฉด lock_address์™€ unlock_address์˜ ์ฃผ์„์„ ์‚ญ์ œํ•˜์‹  ๋’ค, address์— ์ž…๋ ฅํ•œ ๊ฐ’์„ ๋™์ผํ•˜๊ฒŒ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.

14. password๋Š” Cloud Object Storage์˜ apikey๋กœ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค. Cloud Object Storage์˜ Credential์—์„œ ํ™•์ธ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

15. ์ €์žฅ

Terraform์„ ํ†ตํ•ด ์ €์žฅํ•˜๊ธฐ

16. Terraform init ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค.

cd terraform
terraform init

17. terraform plan ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.

terraform plan

18. terraform Apply ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ์ ์šฉํ•ฉ๋‹ˆ๋‹ค.

terraform apply

19. ์œ„์—์„œ ์„ค์ •ํ•œ Bucket์„ Cloud Object Storage์˜ ๋Œ€์‰ฌ๋ณด๋“œ์—์„œ ํ™•์ธํ•ด ๋ณด๋ฉด, Terraform state ํŒŒ์ผ์ด ์ƒ์„ฑ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

20. main.tf ๋‚ด์šฉ์„ ๋ณ€๊ฒฝํ•˜์—ฌ ์ ์šฉํ•ฉ๋‹ˆ๋‹ค.

21. Terraform Plan

terraform plan

22. Terraform Apply

terraform apply

 

License

This project is licensed under the Apache License Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0).

ํ† ๋ก  ์ฐธ๊ฐ€

์ด๋ฉ”์ผ์€ ๊ณต๊ฐœ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํ•„์ˆ˜ ์ž…๋ ฅ์ฐฝ์€ * ๋กœ ํ‘œ์‹œ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.