새소식

IaC/Terraform

3주차(1)_Workspace를 이용한 tfstate 파일 격리

  • -
CloudNet@ 팀의 가시다님께서 Leading 하시는 Terraform T101 Study 내용 요약

 

해당 Terraform Study 는 Terraform Up and Running 책을 기반으로 진행 중입니다.

 

 

이번에는 테라폼 Workspace 기능을 이용한 tfstate 파일 격리 방안에 대해서 알아보자
혹시 tfstate 파일에 관한 이론이 궁금하다면 이전 게시글을 참고해보자

 

 

 

2주차(3)_terraform.tfstate 란 무엇인가??

CloudNet@ 팀의 가시다님께서 Leading 하시는 Terraform T101 Study 내용 요약 해당 Terraform Study 는 Terraform Up and Running 책을 기반으로 진행 중입니다. 이번에는 terraform 에서 가장 중요한 부분 중 하나인 tfstat

kimalarm.tistory.com



1. Preview


  • 이번 실습을 통해 만드는 workspace 는 다음과 같은 구조를 지니고 있다.

같은 S3 Bucket을 backend 로 사용하고 있지만,

workspace는 tfstate 파일을 논리적으로 격리시킬 수 있기 때문에 동일한 코드로 다른 환경에 각각 배포할 수 있다.

 

혹시 지금 이해하지 못하더라도 게시글을 끝까지 읽은 후 다시 보면 이해가 될 것이다.

그럼 workspace 를 테스트 해보자 ! 

 

 

2. S3 backend 생성


  • 이전 게시글에서 생성했던 backend 를 다시 배포하자

 

2.1. 테스트를 위한 backend 생성


mkdir tfstate-test && cd tfstate-test

 

  • backend.tf 파일

// backend.tf

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

resource "aws_s3_bucket" "scott_s3_bucket" {
  bucket = "scott-terraform-study-tfstate"

  // S3 Version 관리
  versioning {
    enabled = true
  }
  // Bucket 삭제 방지
  lifecycle {
    prevent_destroy = false
  }
  // Bucket 암호화
  server_side_encryption_configuration {
    rule {
        apply_server_side_encryption_by_default {
            sse_algorithm = "AES256"
        }
    }
  }
}

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

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

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

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

 

  • 리소스 배포

terraform init && terraform plan && terraform apply -auto-approve

 

2.2. 테스트를 위한 main.tf 생성


mkdir workspace-test && cd workspace-test

 

  • main.tf 파일

// main.tf

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

terraform {
  backend "s3" {
    bucket = "scott-terraform-study-tfstate"
    key    = "stg/terraform.tfstate"
    region = "ap-northeast-2"
    dynamodb_table = "terraform-locks"
    # encrypt        = true
  }
}

resource "aws_instance" "ubuntu" {
  ami                    = "ami-0e9bfdb247cc8de84"
  instance_type          = "t2.micro"
}

 

  • 리소스 배포

terraform init && terraform plan && terraform apply -auto-approve

 

 

 

3. Workspace 로 tfstate 파일 격리


  • 이전 게시글에서 테라폼의 기본 workspace 는 default 로 설정되어 있다고 하였다.
  • 실제로 그러한 지 확인해보자

terraform workspace show

현재 사용 중인 workspace 는 default 임을 확인할 수 있다.

 

 

Workspace 를 생성하지 않고 그대로 terraform plan 또는 apply 명령어를 입력한다면??

당연하게도 아무런 변화가 없다.
지금은 오직 default workspace 내의 tfstate 파일만을 참조하고 있기 때문이다.

 

그렇다면 workspace를 새로 생성해서 명령어를 다시 치면 결과가 달라질까??

바로 확인해보자 !

 

 

3.1. workspace 생성

  • 터미널 화면에서 아래 명령어를 입력하자

terraform workspace new work-01

 

work-01 이라는 이름을 가진 workspace 가 새로 생성되었고 작업 환경도 work-01 로 스위칭된 것을 확인할 수 있다.

 

이 때 다시 terraform 명령어를 쳐보자


terraform plan && terraform apply -auto-approve

리소스가 배포되었다 !!

 

 

  • 아래 명령어를 통해 현재 Terraform 에서 생성된 Workspace 리스트를 확인할 수 있다.

terraform workspace list

Workspace 는 default, work-01 두 개로 이루어져 있고, 지금 사용하고 있는 것은 work-01 이라는 의미이다.

 

 

  • 만약, Workspace 를 변경하고 싶다면 아래의 명령어를 입력하자

terraform workspace select default

 

 

실제 AWS 콘솔에는 어떻게 적용되어 있는지 확인해보자

 

 

3.2. S3 & DynamoDB 확인

  • S3 확인

이때까지 보지 못했던 env:/ 라는 디렉토리가 생긴 것을 확인할 수 있다.

한 번 들어가보자

 

env:/ 라는 디렉토리 밑에 우리가 생성했던 work-01 이라는 workspace 이름의 경로로 tfstate 파일이 들어가있는 것을 확인할 수 있다.

 

 

  • DynamoDB 확인

DynamoDB 에도 동일하게 /env: 경로로 LockID 가 생성된 것을 확인할 수 있다.

 

 

실습 진행 후에는 반드시 리소스를 삭제하자!
## work-01 리소스 삭제
terraform workspace select work-01
terraform destroy -auto-approve

## default 리소스 삭제
terraform workspace select default
terraform destroy -auto-approve

 

 


 

이번에는 Workspace 기능을 이용하여 tfstate 파일을 격리하는 방법을 알아보았다.

다음에는 File Layout 을 통한 tfstate 파일 격리 방법에 대해서 알아보자 !

Contents

포스팅 주소를 복사했습니다