brunch

5. GCP-테라폼 상태 관리하기

by Master Seo


클라우드 상태를 지정하는 백엔드를 알아보자



목표

로컬 백엔드

클라우드 스토리지 백엔드

상태



<1> 테라폼 상태 목적

<2> 백엔드 작업 목적

<3> local backend 추가하기

<4> backend로 Cloud Storage 추가하기

<5> 상태 갱신하기



<1> 테라폼 상태 목적


1

실제 상태와 매핑 ?


2

싱크?

https://developer.hashicorp.com/terraform/language/state/remote



3

상태 잠금 ?

백엔드가 지원

https://developer.hashicorp.com/terraform/language/settings/backends/configuration


벡엔드 타입

https://developer.hashicorp.com/terraform/language/settings/backends/configuration#backend-types



4

작업 공간?

백엔드에 저장된 영구 데이터는 작업 공간에 속합니다





<2> 백엔드 작업 목적


1

팀으로 작업하기

민감한 정보를 디스크에 저장하기 않음

원격 작업


2

백엔드는 선택사항이다.

개인으로 작업하는 경우는 백엔드를 사용하지 않고 사용해도 된다.




<3> local backend 추가하기



1

touch main.tf


2

gcloud config list --format 'value(core.project)'


3

provider "google" {

project = "# REPLACE WITH YOUR PROJECT ID"

region = "us-central-1"

}

resource "google_storage_bucket" "test-bucket-for-state" {

name = "# REPLACE WITH YOUR PROJECT ID"

location = "US"

uniform_bucket_level_access = true

}



cloud storage

https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/storage_bucket



4

추가하기


terraform {

backend "local" {

path = "terraform/state/terraform.tfstate"

}

}



5

terraform init



6

terraform apply



7

terraform show





<4> backend로 Cloud Storage 추가하기


1

local 백엔드를 gcs로 변경하기


main.tf


terraform {

backend "gcs" {

bucket = "# REPLACE WITH YOUR BUCKET NAME"

prefix = "terraform/state"

}

}



2

automatically migrate the state하기

terraform init -migrate-state




<5> 상태 갱신하기


1

terraform refresh 명령어



2

storage 버킷가서

Labels 탭 > Add Label Key 1= key , Value 1 = value

저장



3

terraform refresh



4

terraform show



<6> Woekspace 정리하기


1

gcs를 local로 다시 변경하기


terraform {

backend "local" {

path = "terraform/state/terraform.tfstate"

}

}



2

terraform init -migrate-state



3

main.tf

force_destroy = true 추가하기


resource "google_storage_bucket" "test-bucket-for-state" {

name = "xxxxxx-gcp-03-c136e27648"

location = "US"

uniform_bucket_level_access = true

force_destroy = true

}



4

terraform apply



5

terraform destroy




다음 보기

https://brunch.co.kr/@topasvga/3389




https://brunch.co.kr/@topasvga/2419

테리폼.png


감사합니다.



keyword
매거진의 이전글4. GCP-테라폼 문법 개념잡기