Ssoon

[T101-3주차] 03 테라폼 상태 관리하기 - (4) 상태파일격리 - workspaces 을 통한 격리 본문

Terraform 101 Study

[T101-3주차] 03 테라폼 상태 관리하기 - (4) 상태파일격리 - workspaces 을 통한 격리

구구달스 2022. 10. 31. 23:11
CloudNet@ 팀의 가시다님이 진행하는 Terraform 101 Study 03주차 정리입니다.

📌 모든 환경을 단 하나의 테라폼 세트로 정의하는 대신 하나의 환경에서 문제가 생기더라도 다른 환경에 영향을 주지 않도록 각 환경을 별도의 구성 세트로 정의

상태 파일 격리 2가지 방법

💠 workspaces 을 통한 격리

  • terraform 은 "default" 기본 workspace 에서 시작 

백엔드 리소스 생성 / backend.tf

provider "aws" {
  region = "ap-northeast-2"
}

resource "aws_s3_bucket" "ssoon_s3bucket" {
  bucket = "ssoon-t101study-tfstate-week3"
}

resource "aws_s3_bucket_versioning" "ssoon_s3bucket_versioning" {
  bucket = aws_s3_bucket.ssoon_s3bucket.id
  versioning_configuration {
    status = "Enabled"
  }
}

resource "aws_dynamodb_table" "ssoon_dynamodbtable" {
  name         = "terraform-locks-week3"
  billing_mode = "PAY_PER_REQUEST"
  hash_key     = "LockID"

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

output "s3_bucket_arn" {
  value       = aws_s3_bucket.ssoon_s3bucket.arn
  description = "The ARN of the S3 bucket"
}

output "dynamodb_table_name" {
  value       = aws_dynamodb_table.ssoon_dynamodbtable.name
  description = "The name of the DynamoDB table"
}

🚩 Console 확인

📢 CLI 확인

"Default" workspace 에 EC2 생성/ main.tf

provider "aws" {
  region = "ap-northeast-2"
}

resource "aws_instance" "example" {
  ami           = "ami-0c76973fbe0ee100c"
  instance_type = "t2.micro"
  tags = {
    Name = "t101-week3"
  }
}

terraform {
  backend "s3" {
    bucket         = "ssoon-t101study-tfstate-week3"
    key            = "workspaces-default/terraform.tfstate"
    region         = "ap-northeast-2"
    dynamodb_table = "terraform-locks-week3"
  }
}

🚩 Console 확인

📢 CLI 확인

새로운 workspace 생성 / ssoon_work1

새로운 workspace 를 생성 한 후 terraform apply 하면 EC2를 새롭게 생성합니다.

📌 "default" workspace 와 "ssoon_work1" workspace 는 완전히 분리된 공간이기 때문이며 "defaut" workspace 의 상태 파일을 사용하지 않습니다.

🚩 Console 확인 / terraform apply

 새로운 workspace 생성 / ssoon_work2

workspace 변경

🚩 Console 확인 / S3 Bucket

S3 의 env: 폴더 안에 각 workspace 마다 하나의 폴더가 생성됩니다.

각각의 workspace 폴더안에는 각자의 상태 파일을 가지고 있습니다.

🔑 이미 배포되어 있는 인프라에 영향을 주지 않고 테스트 시 유용합니다.

Comments