[IaC] OpenTofu with GitLab #2 μ˜€ν”ˆμŠ€νƒ μΈμŠ€ν„΄μŠ€ νŒŒμ΄ν”„λΌμΈ 섀계

μ•ˆλ…•ν•˜μ„Έμš”
μ˜€λŠ˜μ€ κΉƒλž© λŸ¬λ„ˆμ™€ OpenTofu νŒŒμ΄ν”„λΌμΈμ„ μ μš©ν•œ μ–˜κΈ°λ₯Ό 써보고자 ν•©λ‹ˆλ‹€.
νŒŒμ΄ν”„λΌμΈμ„ μž‘μ„±ν•  일이 μ—†μ–΄ μ‚¬μš©ν•΄λ³΄μ§€ μ•Šμ•˜λŠ”λ°, μ œλ²• μž¬λ°Œμ—ˆμŠ΅λ‹ˆλ‹€. 자꾸만 더 효율적으둜 μ‚¬μš©ν•˜κ³  싢은 생각이 λ“œλ„€μš”!
 
μ΄λ ‡κ²Œ κ°œλ°œν•˜κΈ° 쒋은 ν™˜κ²½μ„ ꡬ성해쀀 μ˜† νŒ€μ— 항상 κ°μ‚¬ν•©λ‹ˆλ‹€.
 

νŒ¨ν‚€μ§€ μ„€μΉ˜

opentofu

wget --secure-protocol=TLSv1_2 --https-only https://get.opentofu.org/install-opentofu.sh -O install-opentofu.sh

chmod +x install-opentofu.sh

./install-opentofu.sh --install-method deb

rm -f install-opentofu.sh

apt-get update

apt-get install -y apt-transport-https ca-certificates curl gnupg

sudo install -m 0755 -d /etc/apt/keyrings

curl -fsSL https://get.opentofu.org/opentofu.gpg | sudo tee /etc/apt/keyrings/opentofu.gpg >/dev/null

curl -fsSL https://packages.opentofu.org/opentofu/tofu/gpgkey | sudo gpg --no-tty --batch --dearmor -o /etc/apt
/keyrings/opentofu-repo.gpg >/dev/null

sudo chmod a+r /etc/apt/keyrings/opentofu.gpg /etc/apt/keyrings/opentofu-repo.gpg

echo "deb-src [signed-by=/etc/apt/keyrings/opentofu.gpg] https://packages.opentofu.org/opentofu/tofu/any/ any main" | sudo tee /etc/apt/sources.list.d/opentofu.list > /dev/null

sudo chmod a+r /etc/apt/sources.list.d/opentofu.list

apt-get install -y tofu

 

openstack-provider

wget https://github.com/terraform-provider-openstack/terraform-provider-openstack/releases/download/v3.0.0
/terraform-provider-openstack_3.0.0_linux_amd64.zip

unzip terraform-provider-openstack_3.0.0_linux_amd64.zip

 
ν”„λ‘œλ°”μ΄λ”λŠ” μƒλ‹Ήνžˆ μ΅œμ‹  버전을 μ‚¬μš©ν•˜μ˜€λŠ”λ°μš”(무렀 24λ…„ 9μ›” 25일)
μš°μˆ˜λ¦¬μ—λ„ ν˜Έν™˜μ΄ μ•„μ£Ό 잘 λ©λ‹ˆλ‹€
 

κΉƒλž© λŸ¬λ„ˆ 연동

Settings CI/CD
개인 λŸ¬λ„ˆ 생성 ν›„ λŸ¬λ„ˆ μ‹€ν–‰ν•  μ„œλ²„μ—μ„œ gitrab-runner register
 

ν…ŒλΌνΌ(μ˜€ν”ˆν† ν‘Έ) 폴더 ꡬ쑰

https://spacelift.io/blog/terraform-state

Managing Terraform State - Best Practices & Examples

Learn how to manage Terraform State. See best practices for storing, organizing, and isolating your state files.

spacelift.io

 
μœ„ 글을 보고 tfstate νŒŒμΌμ„ κ΄€λ¦¬ν•˜λŠ” ꡬ쑰의 μ˜ˆμ‹œ 아이디어λ₯Ό μ–»μ—ˆμŠ΅λ‹ˆλ‹€.

.
β”œβ”€β”€ .gitlab-ci.yml      # μƒμœ„ νŒŒμ΄ν”„λΌμΈ
β”œβ”€β”€ env
β”‚   β”œβ”€β”€ dev
β”‚   β”‚   β”œβ”€β”€ .gitlab-ci.yml
β”‚   β”‚   β”œβ”€β”€ main.tf
β”‚   β”‚   β”œβ”€β”€ variables.tfvars
β”‚   β”‚   └── ...
β”‚   β”œβ”€β”€ live
β”‚   β”‚   β”œβ”€β”€ .gitlab-ci.yml
β”‚   β”‚   β”œβ”€β”€ main.tf
β”‚   β”‚   β”œβ”€β”€ variables.tfvars
β”‚   β”‚   └── ...
β”‚   β”œβ”€β”€ perf
β”‚   └── sandbox

 
μ œκ°€ μš΄μ˜ν•˜λŠ” μ˜€ν”ˆμŠ€νƒμ˜ ν™˜κ²½μœΌλ‘œ λ‚˜λˆ„μ–΄ λ³΄μ•˜κ΅¬μš”,
μ΄λ ‡κ²Œ λ‚˜λˆˆ ν™˜κ²½ μ•ˆμ— λ³„λ„μ˜ ci νŒŒμΌμ„ λ„£μ–΄ νŠΈλ¦¬κ±°κ°€ 각 ν™˜κ²½λ³„λ‘œ λ™μž‘ν•˜κ²Œ κ΅¬ν˜„ν•˜μ˜€μŠ΅λ‹ˆλ‹€.
μš΄μ˜μžκ°€ μ–΄λ–»κ²Œ 관리할 지에 λŒ€ν•œ λ‚΄μš©μ΄μ—¬μ„œ 각자 ν™˜κ²½μ— 맞게 κ΅¬μ„±ν•˜λ©΄ λ˜κ² μŠ΅λ‹ˆλ‹€.
 
ci 파일
μ €λŠ” 각 envλ³„λ‘œ 두고 ν•˜μœ„μ— νŒŒμ΄ν”„λΌμΈ νŒŒμΌμ„ λ„£μ–΄λ³΄μ•˜μŠ΅λ‹ˆλ‹€.
https://docs.gitlab.com/ee/ci/pipelines/downstream_pipelines.html

Downstream pipelines | GitLab

GitLab product documentation.

docs.gitlab.com

 
μœ„ ꡬ쑰λ₯Ό μ°Έκ³ ν•˜μ—¬ νŒŒμ΄ν”„λΌμΈμ„ μž‘μ„±ν•˜μ˜€μŠ΅λ‹ˆλ‹€.
μ €λŠ” 브랜치λ₯Ό λ”°λ‘œ μƒμ„±ν•˜κ³  싢지 μ•Šμ•„μ„œ νŒŒμ΄ν”„λΌμΈ 내에 MR 이벀트λ₯Ό λ°œμƒν•˜κ²Œ μ•ˆν–ˆλŠ”λ°μš”, ν™˜κ²½λ³„λ‘œ λ³€κ²½ 지점 생겼을 λ•Œ MR μ¨μ„œ λ¨Έμ§€ν•˜λŠ” λ°©ν–₯도 μ’‹μ•„λ³΄μž…λ‹ˆλ‹€. μ €λŠ” 혼자 μš΄μ˜ν•΄μ„œ κ·Έλ ‡κ²Œ ν•˜μ§€λŠ” μ•Šμ•˜μŠ΅λ‹ˆλ‹€ μœΌν•˜ν•˜
 

νŒŒμ΄ν”„λΌμΈ κ²°κ³Ό

 
μš°μ„  devμ—λ§Œ 트리거λ₯Ό μ€˜μ„œ λŒμ•„κ°„ κ²°κ³Ό ν™”λ©΄μž…λ‹ˆλ‹€.
μΈμŠ€ν„΄μŠ€ 생성, λ¦¬μ‚¬μ΄μ¦ˆ μž‘μ—… ν•΄λ΄€λŠ”λ° active μƒνƒœκΉŒμ§€ 32초 κ±Έλ ΈμŠ΅λ‹ˆλ‹€.
 
무쑰건 IaC 도ꡬλ₯Ό ν•˜λ‚˜λ§Œ μ“°λŠ” 것은 정닡이 μ•„λ‹ˆλΌκ³  λ³΄κ΅¬μš”, opentofu(terraform), ansible을 적절히 μ¨μ„œ μž₯점만 μ§¬λ½•ν•΄μ„œ 효율적으둜 μš΄μ˜ν•˜λ©΄ 정말 쒋을 도ꡬ라 μƒκ°ν•©λ‹ˆλ‹€.
 
 λ‹€μš΄μŠ€νŠΈλ¦Όμͺ½ νŒŒμ΄ν”„λΌμΈ μ½”λ“œλ₯Ό μž¬μ‚¬μš©ν•  수 있게 효율적으둜 짜면 ν•΄λ‹Ή μ½”λ“œμ— λŒ€ν•΄μ„œλ„ μž‘μ„±ν•΄λ³΄κ² μŠ΅λ‹ˆλ‹€.