[IaC] Terraform import๋ฅผ ํ™œ์šฉํ•œ openstack ๋ฆฌ์†Œ์Šค tfstate ๋งŒ๋“ค๊ธฐ

์•ˆ๋…•ํ•˜์„ธ์š”
์˜ค๋Š˜์€ ํ…Œ๋ผํผ์„ ํ™œ์šฉํ•˜์—ฌ ํ…Œ๋ผํผ์ด ์•„๋‹Œ ๋ฐฉ์‹์œผ๋กœ ์ƒ์„ฑ๋œ ๋ฆฌ์†Œ์Šค๋„ tfstate ํŒŒ์ผ๋กœ ๋งŒ๋“ค์–ด์ฃผ๋Š” terraform import ๊ธฐ๋Šฅ์„ ์ •๋ฆฌํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.
 

์ฃผ์ ˆ์ฃผ์ ˆ

์ด ๊ธฐ๋Šฅ์„ ์ฐพ๊ธฐ๊นŒ์ง€ ํ—˜๋‚œํ•œ ์—ฌ์ •์„ ํŽผ์นœ ๋‚ด์šฉ์„ ์ฃผ์ ˆ๊ฑฐ๋ ค๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.
์ธํ”„๋ผ ๋ฆฌ์†Œ์Šค๋ฅผ ์šด์˜ ๋ฐ ๊ด€๋ฆฌํ•˜๋‹ค ๋ณด๋ฉด ๊ณผ๊ฑฐ์— ์ƒ์„ฑ๋œ ๋ ˆ๊ฑฐ์‹œ ๋ฆฌ์†Œ์Šค์™€ ํ˜„์žฌ ์ƒ์„ฑ๋œ ๋ฆฌ์†Œ์Šค๊ฐ€ ๋ชจ๋‘ ๊ณต์กดํ•˜๊ฒŒ ๋˜๋”๋ž๋‹ˆ๋‹ค.
๋ ˆ๊ฑฐ์‹œ๋ž€๊ฒƒ์€... ๋กœ์ปฌ์—์„œ cli๋กœ ์ƒ์„ฑํ•œ ๋ฆฌ์†Œ์Šค๋‚˜ ํ˜ธ๋ผ์ด์ฆŒ ์ฝ˜์†”์—์„œ ์ƒ์„ฑํ•œ ๋ฆฌ์†Œ์Šค ๊ฐ™์€... ๋ญ ์ด๊ฒƒ์ €๊ฒƒ ๋“ฑ๋“ฑ์ด์š”

์ด ๊ฒฝ์šฐ ํ˜„ํ™ฉ ๊ด€๋ฆฌ๊ฐ€ ์ฐธ ์–ด๋ ต๋”๋ผ๊ตฌ์š” ...

 
๊ทธ๋ž˜์„œ ํ…Œ๋ผํผ ์ด์ „์— ์ฐ์–ด๋‚ธ ๋ฆฌ์†Œ์Šค๋ฅผ ์–ด๋–ป๊ฒŒ ํ…Œ๋ผํผ์ด ๊ด€๋ฆฌํ•˜๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ์ง€๋ฅผ ์ฐพ์•„๋ณด๋‹ค๊ฐ€ GCP์—์„œ ๋งŒ๋“  terraformer๋ผ๋Š” ๋ฐฉ์‹๋„ ํ…Œ์ŠคํŠธํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค.
https://github.com/GoogleCloudPlatform/terraformer

 

GitHub - GoogleCloudPlatform/terraformer: CLI tool to generate terraform files from existing infrastructure (reverse Terraform).

CLI tool to generate terraform files from existing infrastructure (reverse Terraform). Infrastructure to Code - GoogleCloudPlatform/terraformer

github.com

 
ํ…Œ๋ผํฌ๋จธ์˜ ๊ฒฝ์šฐ ์˜คํ”ˆ์Šคํƒ ์ปดํ“จํŠธ, ๋ธ”๋ก์Šคํ† ๋ฆฌ์ง€, ๋„คํŠธ์›Œํ‚น 3๊ฐ€์ง€ ๋ฆฌ์†Œ์Šค๋ฅผ ๋ณ€ํ™˜ํ•˜๋Š” ๊ฒƒ์„ ์ง€์›ํ•œ๋‹ค๊ณ  ๋‚˜์™€์žˆ์ง€๋งŒ ์ปดํ“จํŠธ์˜ ๊ฒฝ์šฐ ๋ณ€ํ™˜์ด ์•ˆ๋ฉ๋‹ˆ๋‹ค! (panic error๋ฅผ ๋งˆ์ฃผ์นจ)
aws๋กœ ์“ฐ์‹œ๋Š” ๋ถ„๋“ค์€ ์ž˜ ๋  ๊ฒƒ ๊ฐ™๋„ค์š” ์•„๋งˆ.. ๋”ฐํํ‘
 
๊ณ ๋ฏผ์„ ๊ณ„์†ํ•˜๋‹ค terraform์—์„œ import๋ผ๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค๋Š” ๋ฌธ์„œ๋ฅผ ์ฝ์—ˆ์Šต๋‹ˆ๋‹ค.
https://developer.hashicorp.com/terraform/cli/import

 

Import | Terraform | HashiCorp Developer

Terraform can import and manage existing infrastructure. This can help you transition your infrastructure to Terraform.

developer.hashicorp.com

 

ํ•˜์ง€๋งŒ ๊ณง ๋‚œ๊ด€์— ๋„์ฐฉํ•ฉ๋‹ˆ๋‹ค.

์™œ ์˜คํ”ˆ์Šคํƒ์€ terraform import๋ฅผ ์•„๋ฌด๋„ ์•ˆ์ผ์„๊นŒ์š”...

 
๋„ค ์•„๋ฌดํŠผ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•์„ ์ฐพ์•„๋‚ด์–ด ์ €์™€ ๊ฐ™์€ ๊ณ ๋ฏผ์„ ํ•˜์‹œ๋Š” ๋ถ„๋“ค๊ป˜ ๋„์›€์ด ๋˜๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„ ์š”๋ฆฌ์กฐ๋ฆฌ ์ •๋ฆฌํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค.
 

Terraform import๊ฐ€ ๋ฌด์—‡์„ ํ•˜๋Š๋ƒ

 
terraform import๋Š” ๊ธฐ์กด์˜ ์ธํ”„๋ผ ๋ฆฌ์†Œ์Šค๋ฅผ Terraform ์ƒํƒœ ํŒŒ์ผ๋กœ ๊ฐ€์ ธ์˜ค๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค.
์ด ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๋ฉด ์ด๋ฏธ ํ”„๋กœ๋น„์ €๋‹๋œ ์ธํ”„๋ผ๋ฅผ Terraform์œผ๋กœ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜๋Š”๋ฐ์š” ๊ธฐ์กด ์ธํ”„๋ผ ๋ฆฌ์†Œ์Šค๋ฅผ ์ƒˆ๋กœ ๋งŒ๋“ค์ง€ ์•Š๊ณ , ํ˜„์žฌ ์ƒํƒœ๋ฅผ Terraform์˜ ์ƒํƒœ ํŒŒ์ผ(terraform.tfstate)์— ๊ธฐ๋กํ•˜์—ฌ Terraform์ด ๊ทธ ๋ฆฌ์†Œ์Šค๋ฅผ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.

Terraform Import์˜ ์ฃผ์š” ๊ธฐ๋Šฅ

  1. ๊ธฐ์กด ๋ฆฌ์†Œ์Šค๋ฅผ Terraform ์ƒํƒœ์— ์ถ”๊ฐ€: ์ด๋ฏธ ํ”„๋กœ๋น„์ €๋‹๋œ ์ธํ”„๋ผ๋ฅผ ๊ฑด๋“ค์ง€ ์•Š์Šต๋‹ˆ๋‹ค. Terraform ์ƒํƒœ ํŒŒ์ผ์— ํ•ด๋‹น ๋ฆฌ์†Œ์Šค๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ๊ธฐ์กด ๋ฆฌ์†Œ์Šค๊ฐ€ Terraform ์ฝ”๋“œ๋กœ ๊ด€๋ฆฌ๋  ์ˆ˜ ์žˆ๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.
  2. ๋ฆฌ์†Œ์Šค๋ฅผ ํŒŒ๊ดดํ•˜์ง€ ์•Š๊ณ  ๊ด€๋ฆฌ ๊ฐ€๋Šฅ: terraform import๋Š” ๋ฆฌ์†Œ์Šค๋ฅผ ํŒŒ๊ดดํ•˜๊ฑฐ๋‚˜ ๋‹ค์‹œ ํ”„๋กœ๋น„์ €๋‹ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.(ํœด) ์ด๋ฏธ ์กด์žฌํ•˜๋Š” ๋ฆฌ์†Œ์Šค๋ฅผ Terraform์˜ ๊ด€๋ฆฌ ํ•˜์— ๋‘๊ธฐ๋งŒ ํ•ฉ๋‹ˆ๋‹ค. ์ดํ›„ terraform plan๊ณผ terraform apply๋ฅผ ํ†ตํ•ด Terraform์ด ๋ฆฌ์†Œ์Šค ์ƒํƒœ์™€ ์ฝ”๋“œ ๊ฐ„์˜ ์ฐจ์ด๋ฅผ ๊ด€๋ฆฌํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
  3. ์—ฌ๋Ÿฌ ๋ฆฌ์†Œ์Šค ์ง€์›: ์ €๋Š” ์˜คํ”ˆ์Šคํƒ์— ์‚ฌ์šฉํ•˜์˜€์ง€๋งŒ AWS, Azure, GCP, ncloud ๋“ฑ ์—ฌ๋Ÿฌ ํด๋ผ์šฐ๋“œ์—๋„ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

์„ค์น˜

Terraform 1.9.7
terraform import ๊ธฐ๋Šฅ์€ ํ…Œ๋ผํผ 1.5.0 ์ด์ƒ๋ถ€ํ„ฐ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ์ €๋Š” ๊ฐ€์žฅ ์ตœ์‹  ๋ฒ„์ „์œผ๋กœ ๊ฒŸํ–ˆ์Šต๋‹ˆ๋‹ค.

wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
  
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
  
sudo apt update && sudo apt install terraform

 
openstack terraform provider v3.0.0
ํ”„๋กœ๋ฐ”์ด๋”๋Š” ์ผ์ฃผ์ผ ์ „์— ์ตœ์‹  ๋ฒ„์ „์ด ๋‚˜์™€์„œ ๋œจ๋ˆ๋œจ๋ˆํ•ด์„œ ๊ฒŸํ–ˆ์Šต๋‹ˆ๋‹ค.

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

 

terraform import ํŒŒ์ผ ๊ตฌ์กฐ

ํŒŒ์ผ ๊ตฌ์กฐ์˜ ๊ฒฝ์šฐ ์ €๋Š” ์„ค์ • ํŒŒ์ผ, ๋ฆฌ์†Œ์Šค ํŒŒ์ผ๋กœ ๊ตฌ๋ถ„ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋ฆฌ์†Œ์Šค์˜ ๊ฒฝ์šฐ ์ถ”ํ›„ ์ƒํ™ฉ์— ๋งž๊ฒŒ ํ™˜๊ฒฝ๋ณ„๋กœ ๋…ผ๋ฆฌ์ ์œผ๋กœ ๋‚˜๋ˆ„์–ด ์“ฐ์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค.
 
๐Ÿ“ ํ…Œ๋ผํผ ํด๋”
  ใ„ด provider.tf     # ํด๋ผ์šฐ๋“œ ์„ค์ • ์ œ๊ณต ํŒŒ์ผ
  ใ„ด resource.tf    # ์ธ์Šคํ„ด์Šค ๋ฆฌ์†Œ์Šค ์ •์˜ ํŒŒ์ผ
 

# provider.tf
terraform {
  required_providers {
    openstack = {
      source = "terraform-provider-openstack/openstack"
      version = ">=3.0.0"
    }
  }
}
 
provider "openstack" {
  auth_url = "http://[openstack controller ip]:5000/v3"
  region = "RegionOne"
  domain_name = "Default"
  user_name = "[user account]"
  password = "[user password]"
  tenant_name = "[project name]"
}

 

# resource.tf

resource "openstack_compute_instance_v2" "test" {}
 
resource "openstack_compute_instance_v2" "test2" {}

 
ํŒŒ์ผ์ด ๋‹ค ๊ตฌ์„ฑ๋˜์—ˆ๋‹ค๋ฉด ์ดํ›„ ์•„๋ž˜ ๋‹จ๊ณ„๋ฅผ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

terraform init

terraform import openstack_compute_instance_v2.test <instance id>

terraform import openstack_compute_instance_v2.test2 <instance id>

 
์ดํ›„ tfstate ํŒŒ์ผ๋กœ ์˜ˆ์˜๊ฒŒ ์ž˜ ๋งŒ๋“ค์–ด์ง„ ํŒŒ์ผ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.