๋กœ์ผ“๐Ÿพ
article thumbnail

 

 

ํ…Œ๋ผํผ์€ ๋Œ€ํ‘œ์ ์ธ IaC ๋„๊ตฌ๋กœ AWS ์™€ ๊ฐ™์€ ํด๋ผ์šฐ๋“œ ํ™˜๊ฒฝ์„ ์ฝ”๋“œ๋กœ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์žฅ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

 

๊ทธ๋ฆฌ๊ณ  ๋ณดํ†ต ํด๋ผ์šฐ๋“œ ํ™˜๊ฒฝ์€ Dev ๊ณผ Prod ์œผ๋กœ ๋‚˜๋‰˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. 

 

ํ•œํŽธ, ํ…Œ๋ผํผ์„ ์ด์šฉํ•ด์„œ ๋ฆฌ์†Œ์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ฒŒ ๋˜๋ฉด backend ๋ผ๋Š” ๊ณณ์— tfstate ๋ฅผ ์ €์žฅํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

tfstate ๋Š” ๋ฆฌ์†Œ์Šค์˜ ๊ตฌ์„ฑ ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ํŒŒ์ผ์ด๊ณ , backend ๋Š” ์ด๋Ÿฌํ•œ tfstate ๋ฅผ ์ €์žฅํ•˜๋Š” ๋ณด๊ด€์†Œ ์ž…๋‹ˆ๋‹ค.

 

backend ๋Š” ๋กœ์ปฌ์ด ๋  ์ˆ˜๋„ ์žˆ๊ณ , AWS S3 ์™€ ๊ฐ™์€ ์˜ค๋ธŒ์ ํŠธ ์Šคํ† ๋ฆฌ์ง€๋„ ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

ํ•œ๋ฒˆ, AWS S3 ์— Demo ๋ฒ„ํ‚ท์„ Dev ํ™˜๊ฒฝ๊ณผ Prod ํ™˜๊ฒฝ์— ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  ๋™์ผํ•˜๊ฒŒ ์ƒ์„ฑํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

์˜ˆ์ œ ์ฝ”๋“œ๋Š” ์—ฌ๊ธฐ์„œ ๊ฐ€์ ธ์™”์Šต๋‹ˆ๋‹ค!

 

์‚ฌ์šฉํ•  main.tf ๋Š” ์•„๋ž˜์™€ ๊ฐ™๊ณ ,

resource "random_pet" "this" {
  length = 2
}

module "simple_bucket" {
  source = "terraform-aws-modules/s3-bucket/aws"
  version = "4.1.2"

  bucket = "kingbj0429-${random_pet.this.id}"

  force_destroy = true
}

 

providers.tf ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

terraform {
  required_version = ">=1.5.0"

  backend "s3" {
    bucket         = "backend-dev"
    key            = "tfstates/s3/demo-bucket.tfstate"
    region         = "ap-northeast-2"
    profile        = "iam-dev"
  }

  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "5.33.0"
    }
  }
}
provider "aws" {
  shared_config_files      = ["~/.aws/config"]
  shared_credentials_files = ["~/.aws/credentials"]

  profile = terraform.workspace == "dev" ? "s3-manager-dev-role" : "s3-manager-prod-role"
  region  = "ap-northeast-2"

  default_tags {
    tags = {
      terraform   = "true"
      environment = terraform.workspace
    }
  }
}

 

๊ฐ„๋‹จํ•˜๊ฒŒ backend ๊ตฌ์„ฑ์„ ํ•œ๋ฒˆ ์‚ดํŽด๋ณด๋ฉด,

 

backend ๋Š” "AWS S3" ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , "backend-dev" ๋ฒ„ํ‚ท์— tfstate ํŒŒ์ผ(์˜ค๋ธŒ์ ํŠธ)์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ์ €์žฅ๋˜๋Š” ์˜ค๋ธŒ์ ํŠธ์˜ ํ‚ค๋Š” "tfstates/s3/demo-bucket.tfstate" ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  "backend-dev" ๋ฒ„ํ‚ท ์— ๋Œ€ํ•œ ์•ก์„ธ์Šค ๊ณ„์ •์œผ๋กœ๋Š” "iam-dev" ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

 

์ดํ›„ ์ด์ œ ํ…Œ๋ผํผ์˜ workspace ๊ธฐ๋Šฅ์„ ์ด์šฉํ•ด์„œ dev ์™€ prod ๋‚˜๋ˆˆ ํ›„ ๋™์ผํ•œ .tf ๋ฅผ ์ด์šฉํ•ด์„œ AWS Dev ์™€ Prod ํ™˜๊ฒฝ์— ๊ฐ๊ฐ ์ƒ์„ฑํ•ด์ค„ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

init ์™€ workspace ๋ฅผ ๋งŒ๋“ค์–ด์ฃผ๊ณ ,

terraform init
terraform workspace new dev
terraform workspace new prod

 

Dev ํ™˜๊ฒฝ์— S3 ๋ฒ„ํ‚ท์„ ์ƒ์„ฑํ•˜๊ณ ,

$ terraform workspace select dev

$ terraform apply -auto-approve

 

์ƒ์„ฑ๋œ tfstate

workspace ๋•๋ถ„์— env:/ ๋กœ tfstate ๊ฐ€ ๋”ฐ๋กœ ์ €์žฅ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

Prod ํ™˜๊ฒฝ์— S3 ๋ฒ„ํ‚ท์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

$ terraform workspace select prod

$ terraform apply -auto-approve

 

์ƒ์„ฑ๋œ tfstate

์ด๋ฒˆ์—” env:/ ์˜ ํ•˜์œ„ ํ‚ค๊ฐ€ dev ๊ฐ€ ์•„๋‹Œ prod ์ธ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

 

๊ทธ๋Ÿฐ๋ฐ, ์—ฌ๊ธฐ์„œ ๊ฐ„๊ณผํ•œ ๊ฒƒ์ด ํ•˜๋‚˜ ์žˆ์Šต๋‹ˆ๋‹ค. S3 ๊ฐ€ ๋ฐฐํฌ๋˜๋Š” ํ™˜๊ฒฝ์€ Dev ์™€ Prod ๊ฐ€ ๋‚˜๋‰˜์–ด์กŒ์ง€๋งŒ, tfstate ๊ฐ€ ์ €์žฅ๋˜๋Š” backend ๋Š” ํ˜„์žฌ Dev ํ™˜๊ฒฝ์— ์žˆ๋Š” "backend-dev" ๋ฒ„ํ‚ท์ž…๋‹ˆ๋‹ค. 

Dev ํ™˜๊ฒฝ์˜ backend-dev ๋ฒ„ํ‚ท์— ๋ชจ๋‘ ์ €์žฅ๋จ

 

Prod ํ™˜๊ฒฝ์— ์žˆ๋Š” S3 ๋ฒ„ํ‚ท์— ๋Œ€ํ•œ tfstate ๊ฐ€ Dev ํ™˜๊ฒฝ์— ์žˆ๋Š” ๊ฒƒ์ด ๋ญ”๊ฐ€ ์ฐœ์ฐœํ•ฉ๋‹ˆ๋‹ค... 

 

์‚ฌ์‹ค ์œ„์™€ ๊ฐ™์ด ํ•ด๋„ ๋ฌธ์ œ๋  ๊ฒƒ์€ ํ•˜๋‚˜๋„ ์—†์Šต๋‹ˆ๋‹ค. ์ฃผ์–ด์ง„ ์กฐ๊ฑด๊ณผ ํ™˜๊ฒฝ์€ ์ „๋ถ€ ๋‹ค๋ฅด๊ธฐ์— ์ •๋‹ต์€ ์—†์Šต๋‹ˆ๋‹ค.

 

๊ทธ๋ž˜๋„ ์ €๋Š” Dev ํ™˜๊ฒฝ์— ๋Œ€ํ•œ tfstate ๋ฉด Dev ๋ฒ„ํ‚ท์—, Prod ํ™˜๊ฒฝ์— ๋Œ€ํ•œ tfstate ๋ฉด Prod ๋ฒ„ํ‚ท์— ์žˆ๋Š” ๊ฒƒ์ด ๋” ๊น”๋”?ํ•˜๋‹ค ์ƒ๊ฐ ํ–ˆ๊ณ , ๋ฌด์—‡๋ณด๋‹ค Prod ํ™˜๊ฒฝ๊ณผ Dev ํ™˜๊ฒฝ์ด ์™„์ „ํžˆ ๊ฒฉ๋ฆฌ๋˜์–ด์•ผ ํ•˜๋Š” ์กฐ๊ฑด์ด ์žˆ์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋”๋”์šฑ ๋ถ„๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ๋งž์•˜์ฃ .

Dev ์™€ Prod ๊ฐ€ ์™„์ „ํžˆ ๊ฒฉ๋ฆฌ๋จ

์™„์ „ํžˆ ๊ฒฉ๋ฆฌ๋˜์–ด์•ผ ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— AWS ๊ต์ฐจ ๊ณ„์ • ์•ก์„ธ์Šค ๋Š” ์‚ฌ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

 

๊ทธ๋ž˜์„œ ์ด๋ฒˆ ๊ธ€์—์„œ๋Š” Multi-Cross-Backend ์™€ Multi-Cross-Account ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์„œ๋กœ ๋‹ค๋ฅธ ๊ณ„์ •์œผ๋กœ ์„œ๋กœ ๋‹ค๋ฅธ backend ์— tfstate ๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค.

 

์—ฌ๊ธฐ์„œ ๋งํ•˜๋Š” Multi-Cross-Account ๋Š” ๋‹จ์ˆœํžˆ ์„œ๋กœ ๋‹ค๋ฅธ ๊ณ„์ •์„ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ์˜๋ฏธ์ด์ง€, AWS ๊ต์ฐจ ๊ณ„์ • ์•ก์„ธ์Šค ๋ฅผ ๋งํ•˜๋Š” ๊ฒƒ์€ ์•„๋‹™๋‹ˆ๋‹ค!

 

 

๋ ›์ธ ๋‘๋”์ฝ”๋“œ~

 

Multi-Cross-Backend X Multi-Cross-Account


 

providers.tf ์„ ๋‹ค์‹œ ํ•œ๋ฒˆ ๋ณด์ฃ .

terraform {
  required_version = ">=1.5.0"

  backend "s3" {
    bucket         = "backend-dev"
    key            = "tfstates/s3/demo-bucket.tfstate"
    region         = "ap-northeast-2"
    profile        = "iam-dev"
  }

  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "5.33.0"
    }
  }
}
provider "aws" {
  shared_config_files      = ["~/.aws/config"]
  shared_credentials_files = ["~/.aws/credentials"]

  profile = terraform.workspace == "dev" ? "s3-manager-dev-role" : "s3-manager-prod-role"
  region  = "ap-northeast-2"

  default_tags {
    tags = {
      terraform   = "true"
      environment = terraform.workspace
    }
  }
}

 

provider "aws" {} ์Šค์ฝ”ํ”„ ์•ˆ์—๋Š” terraform.workspace ์™€ ๊ฐ™์€ variable ์ด๋‚˜ ์‚ผํ•ญ์—ฐ์‚ฐ์ž ๋“ฑ ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, terraform {} ์Šค์ฝ”ํ”„ ์•ˆ์—๋Š” ๊ทธ ์–ด๋– ํ•œ ๊ฒƒ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. 

 

๊ทธ์ € ํ•˜๋“œ ์ฝ”๋”ฉ๋งŒ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ terraform.workspace ๋ฅผ ํ™œ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

 

๊ทธ๋ ‡๋‹ค๊ณ  ๋ฐฉ๋ฒ•์ด ์—†๋Š” ๊ฑด ์•„๋‹™๋‹ˆ๋‹ค. backend "s3" {} ๋‚ด์šฉ์„ ๋”ฐ๋กœ ํŒŒ์ผ๋กœ ๋บ„ ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

 

dev.hcl ์™€ prod.hcl

 

๊ทธ๋ฆฌ๊ณ  ์•ˆ์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋‚ด์šฉ์ด ์žˆ์ฃ .

#-- dev.hcl
bucket         = "backend-dev"
profile        = "iam-dev"
region         = "ap-northeast-2"
#-- prod.hcl
bucket         = "backend-prod"
profile        = "iam-prod"
region         = "ap-northeast-2"

 

์ฐธ๊ณ ๋กœ ํŒŒ์ผ์˜ ํ™•์žฅ์ž๋Š” .hcl ๋“ , .txt ๋“ , ์—†๋“  ์ƒ๊ด€ ์—†์Šต๋‹ˆ๋‹ค. ๊ฒฝ๋กœ ๋˜ํ•œ ์ƒ๊ด€ ์—†์Šต๋‹ˆ๋‹ค.

 

์ดํ›„ providers.tf ๋Š” ์•„๋ž˜์™€ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

terraform {
  required_version = ">=1.5.0"

  backend "s3" {
    key = "tfstates/s3/demo-bucket.tfstate"
  }

  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "5.33.0"
    }
  }
}
provider "aws" {
  shared_config_files      = ["~/.aws/config"]
  shared_credentials_files = ["~/.aws/credentials"]

  profile = terraform.workspace == "dev" ? "s3-manager-dev-role" : "s3-manager-prod-role"
  region  = "ap-northeast-2"

  default_tags {
    tags = {
      terraform   = "true"
      environment = terraform.workspace
    }
  }
}

 

dev.hcl ์™€ prod.hcl ์— ์žˆ๋Š” ๊ฐ’๋“ค์€ ๋ชจ๋‘ ์ œ๊ฑฐ ํ•ด์ค€๊ฑฐ์ฃ .

 

๊ทธ๋Ÿผ ์•„๋ž˜ ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด์„œ backend ์˜ configuration ์„ ๋™์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

-backend-config=backend-configs/dev.hcl ์˜ต์…˜์„ ํ†ตํ•ด backend ๋ฅผ backend-dev ๋ฒ„ํ‚ท์œผ๋กœ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์ด์ง€์š”.

$ terraform init -backend-config=backend-configs/dev.hcl -reconfigure

 

-reconfigure ์˜ต์…˜์— ๋Œ€ํ•ด ์•Œ๊ณ  ์‹ถ๋‹ค๋ฉด ์—ฌ๊ธฐ!

 

terraform init -backend-config=backend-configs/dev.hcl -reconfigure

 

๊ผญ terraform workspace show ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ํ˜„์žฌ workspace ๋ฅผ ํ™•์ธํ•˜์‹œ๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค!

 

terraform apply ๋„ ๋งˆ์ € ์‹คํ–‰ ํ•œ ํ›„ ํ•œ๋ฒˆ backend-dev ๋ฒ„ํ‚ท์„ ํ™•์ธํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

์›ํ•˜๋Š” ๋ฐ”๋Œ€๋กœ ์ž˜ ์ƒ์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค!

backend-dev

 

๊ทธ๋Ÿผ ์ด๋ฒˆ์—” prod.hcl ๋ฅผ ์‚ฌ์šฉํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

workspace ๋ฅผ ์˜ฎ๊ธด ํ›„์— init ๋ช…๋ น์–ด๋ฅผ ํ•ด์ค๋‹ˆ๋‹ค.

$ terraform init -backend-config=backend-configs/prod.hcl -reconfigure

$ terraform workspace new prod
-reconfigure ์˜ต์…˜์€ ๋กœ์ปฌ backend ๊ตฌ์„ฑ์„ ์ดˆ๊ธฐํ™” ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ƒˆ๋กญ๊ฒŒ workspace ๋ฅผ ๋งŒ๋“ค์–ด์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค!

 

backend-prod ๋ฒ„์ปท์— ์ƒ๊ธด ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

backend-prod

 

์ด๋กœ์จ backend ๋ฅผ ๋ถ„๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

dev.tfstate ๋Š” dev ๋ฒ„ํ‚ท์—, prod.tfstate ๋Š” prod ๋ฒ„ํ‚ท์—

 

workspace ๋Š” ์ด์šฉํ•ด๋„ ์ถฉ๋ถ„ํžˆ Dev ์™€ Prod ํ™˜๊ฒฝ์„ ๋ถ„๋ฆฌํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ์œ„์—์„œ ๋ณด์ธ ์˜ˆ์ œ์ฒ˜๋Ÿผ Dev ํ™˜๊ฒฝ์— ์žˆ๋Š” backend-dev ๋ฒ„ํ‚ท์— tfstate ๊ฐ€ ์ €์žฅ๋  ๊ฒฝ์šฐ, backend-dev ๋ฒ„ํ‚ท์— ๋Œ€ํ•œ ์•ก์„ธ์Šค๋งŒ ์žˆ์œผ๋ฉด ์ถฉ๋ถ„ํžˆ Prod ํ™˜๊ฒฝ์—๋„ ์˜ํ–ฅ์„ ๋ผ์น  ์ˆ˜ ์žˆ๋‹ค๋Š” ์œ„ํ—˜์„ฑ์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.

 

์˜ˆ๋ฅผ ๋“ค์–ด, A ๋Š” Prod ๋ฆฌ์†Œ์Šค์— ๋Œ€ํ•œ tfstate ๋ฅผ backend-dev ๋ฒ„ํ‚ท์— ์ €์žฅํ•˜๊ฒŒ ๋˜๋Š”๋ฐ, backend-dev ๋ฒ„ํ‚ท์— ๋Œ€ํ•ด์„œ๋Š” B ๋„ ์•ก์„ธ์Šค ๊ถŒํ•œ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋•Œ B ๋Š” Prod ๋ฆฌ์†Œ์Šค์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ๊ถŒํ•œ์ด ์—†์ง€๋งŒ, backend-dev ๋ฒ„ํ‚ท์— ์žˆ๋Š” Prod ๋ฆฌ์†Œ์Šค์— ๋Œ€ํ•œ tfstate ์€ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ๋‹น์—ฐํžˆ ๋ณด์•ˆ์ ์œผ๋กœ ๋งค์šฐ ์ข‹์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

 

๊ทธ๋ ‡๋‹ค๊ณ  ์™„์ „ํžˆ ๋ถ„๋ฆฌํ•˜๋Š” ๊ฒƒ๋„ ์ข‹์€ ์ ๋งŒ ์žˆ๋Š” ๊ฒƒ์€ ์•„๋‹™๋‹ˆ๋‹ค. ๊ด€๋ฆฌํ•  ๊ฒƒ์ด ๋งŽ์•„์ง€๊ธฐ ๋•Œ๋ฌธ์— ๋ณต์žก์„ฑ์ด ์˜ฌ๋ผ ๊ฐˆ ์ˆ˜ ๋ฐ–์— ์—†์ฃ .

๋‹ค์‹œ ํ•œ๋ฒˆ ๊ฐ•์š”ํ•˜์ง€๋งŒ backend ๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์—๋Š” ์ •๋‹ต์ด ์—†์œผ๋ฉด ์ฃผ์–ด์ง„ ํ™˜๊ฒฝ์— ๋งž๊ฒŒ ์„ค์ •ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. 

 

๊ทธ๋Ÿผ ์˜ค๋Š˜์€ ์—ฌ๊ธฐ๊นŒ์ง€!

'DevOps > Terraform' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Terraform] -migrate-state vs. -reconfigure  (29) 2024.02.19
profile on loading

Loading...