티스토리 뷰

혼자서 terraform사용할때는 사실 크게 필요없음(백업용도 외)

 

그냥 로컬코드 레포에 테라폼의 상태를 저장하는 tfstate를 두면 된다

 

그러나 여러사람이 동시에 작업할경우에는 일일히 tfstate를 동기화하여야 하고 동기화를 하지 않았을때 apply를 해버리면 인프라가 날라가는 경우가 생김

 

또한 tfstate를 동기화 했더라도 동시에 작업을 해버리면 일관성을 해칠수 있다

 

해서 terraform backend로 s3(tfstate저장) 와 dynamedb(tfstate Lock관리) 를 사용하는 방법 정리

 

현재 bc-labs 클러스터 내 mainnet,testnet 배포에 적용되어 있음

 

크게 어렵진 않다 아래와 같이 프로바이더tf에 설정해주면 됨

terraform {
  required_version = ">= 0.12"
    backend s3 {
    bucket         = "testnet" # S3 버킷 이름
    key            = "terraform/testnet/terraform.tfstate" # tfstate 저장 경로
    region         = "ap-northeast-2"
    dynamodb_table = "terraform-testnet-tfstate-lock" # dynamodb table 이름
    profile        = "testnet"
  }
}

 

 

profile의 경우 아래와 같이 provider에 설정이 되어있는데

provider "aws" {
        profile = "testnet"
  region = var.aws_region
}

위 설정을 참조하지 않아서 계속 에러가 발생됨

 

해서 backend 부분에 profile을 별도로 명시 해줌

 

lock용 dynamodb table은 아래와 같이 배포

resource "aws_dynamodb_table" "terraform-testnet-tfstate-lock" {
  name           = "terraform-testnet-tfstate-lock"
  hash_key       = "LockID"
  billing_mode   = "PAY_PER_REQUEST"

  attribute {
    name = "LockID"
    type = "S"
  }
}

dynamodb 및 s3는 프로비저닝 이전에 배포되야 하므로 console에서 생성하려면 아래와 같이 생성

새롭게 프로비저닝 하는것이라면 terraform init을 바로 하면 되고

 

기존에 이미 로컬로 관리하고 있었다면 .terraform 폴더를 삭제해야함(s3 backend의 경우 설치 되는게 다름)

 

삭제 후 terrafom init 재수행

terraform init

Initializing the backend...

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

Initializing provider plugins...
- Finding latest version of hashicorp/aws...
- Finding latest version of hashicorp/http...
- Installing hashicorp/http v3.1.0...
- Installed hashicorp/http v3.1.0 (signed by HashiCorp)
- Installing hashicorp/aws v4.36.1...
- Installed hashicorp/aws v4.36.1 (signed by HashiCorp)

Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

s3 backend 설정 완료

 

이후 테라폼 작업시에는 tfstate가 로컬이 아닌 s3로 저장되어지며 dynamodb로 인해 lock관리도 되므로 여러명이서 작업해도 문제 없음

최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/10   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
글 보관함