Outsider's Dev Story

Stay Hungry. Stay Foolish. Don't Be Satisfied.
RetroTech 팟캐스트 44BITS 팟캐스트

Terraform의 원격 상태 위치 변경

Terraform을 사용하다 보면 하나의 Terrafom state 파일(.tfstate)로 모든 리소스를 관리하는 것을 권장하지 않으므로 자연히 여러 state 파일로 나누게 된다. 처음에도 많은 고민을 하고 구조를 나누어서 관리하게 되지만 리소스가 계획대로 늘어가는 것도 아니고 작업을 하다가 불편한 부분을 느끼고 개선을 하다 보면 이미 관리하는 상태 파일을 다른 위치로 이동하고 싶은 경우가 생기게 된다.

초기에 아주 잘 설계하면 당연히 좋겠지만 그런 일은 발생하지 않고 필요할 때 원하는 구조로 바꿀 수 있는 게 제일 좋은 구조가 된다. 로컬에서 .tfstate를 관리하고 있다면 A 폴더에서 관리하다가 B/A로 바꾸더라도 폴더 이동만 하면 되지만 원격으로 .tfstate를 관리하고 있다면 얘기가 달라진다.

terraform {
  backend "s3" {
    bucket     = "my-terraform-state"
    key        = "ec2/terraform.tfstate"
    region     = "ap-northeast-1"
    encrypt    = true
  }
}

위는 Terraform 원격 상태 파일을 AWS S3에 저장하도록 한 설정이고 여기서 키인 ec2/terraform.tfstate는 내 프로젝트의 폴더구조를 그대로 가져온 것이다. 상태마다 S3 버킷을 파는 것은 불편하므로 하나의 버킷에서 상태 파일을 관리하기 위해 폴더구조를 그대로 키값으로 사용한 것이다.

여기서 리전을 구분하기 위해서 ec2/terraform.tfstate로 관리하던 상태 파일을 ap-northeast-1/ec2/terraform.tfstate로 이동하고 싶다면 Terraform 설정 파일은 로컬에서 폴더구조를 바꿔주면 되지만 S3의 키값은 바뀌지 않으므로 위 설정을 바꾸어 주어야 한다.

처음에는 로컬에서 폴더구조를 바꾼 다음에 수동으로 S3에서 변경해야 한다고 생각했지만, 막상 이 작업을 해보니 Terraform에서 이 부분까지 지원하고 있었다.

먼저 원하는 경로에 맞춰서 키값을 아래처럼 변경한다.

terraform {
  backend "s3" {
    bucket     = "my-terraform-state"
    key        = "ap-northeast-1/ec2/terraform.tfstate"
    region     = "ap-northeast-1"
    encrypt    = true
  }
}

원격 상태 파일의 위치를 변경한 후에 다시 terraform init을 실행한다.

$ terraform init
Initializing modules...
- module.my_module

Initializing the backend...
Backend configuration changed!

Terraform has detected that the configuration specified for the backend
has changed. Terraform will now reconfigure for this backend. If you didn't
intend to reconfigure your backend please undo any changes to the "backend"
section in your Terraform configuration.


Do you want to copy the state from "s3"?
  Would you like to copy the state from your prior backend "s3" to the
  newly configured "s3" backend? If you're reconfiguring the same backend,
  answering "yes" or "no" shouldn't make a difference. Please answer exactly
  "yes" or "no".

  Enter a value: yes

Terraform has detected that the configuration specified for the backend has changed.라고 나온 것처럼 이전과 상태 파일의 위치가 달라진 것을 자동으로 감지하고 상태 파일을 S3에서 복사할 것인지를 붇는다. 의도가 아니라면 여기서 no를 입력하면 되지만 지금은 상태 파일을 바꾸는 것이 의도이므로 yes를 입력한다.

Do you want to copy state from "s3" to "s3"?
  Pre-existing state was found in "s3" while migrating to "s3". No existing
  state was found in "s3". Do you want to copy the state from "s3" to
  "s3"? Enter "yes" to copy and "no" to start with an empty state.

  Enter a value: yes

Releasing state lock. This may take a few moments...

Successfully configured the backend "s3"! Terraform will automatically
use this backend unless the backend configuration changes.

S3에서 이전에 있던 상태 파일을 새로운 위치로 복사할 것인지를 묻는다. 여기서는 단순히 이동하는 것이므로 yes를 입력하면 S3의 새 위치에 상태 파일을 복사해준다. 물론 복사이므로 이전 상태 파일을 알아서 지워주지는 않고 S3에 들어가서 직접 지워야 한다.

이 작업을 실제로 해보기 전에는 로컬에서 위치를 변경한 뒤에 S3에서 수동으로 위치를 변경하고 다시 plan, apply로 동기화해야 한다고 생각했는데 Terraform에서 이 부분까지 지원하고 있어서 쉽게 할 수 있었다.

2017/11/18 03:25 2017/11/18 03:25