June 13, 2024

Azure Terraform 연동, tfstate Storage 저장

사전조건

Terraform, Azure CLI 설치 및 연동
참고 : Azure Terraform 연동, RBAC 생성 오류 해결 방법

tfstate

terraform 으로 설정한 인프라의 상태를 저장하는 파일.
terraform 과 tfstate에 대한 자세한 설명은 다른 글을 참고해 보자.

tfstate to Cloud Storage

tfstate를 local에 저장하는 것이 리스크가 있고, 인프라에 대한 정보를 다른 협업자들과 공유하기 위해 Cloud에 저장한다.
(AWS S3와 다르게 Key를 이용하여 저장한다. )

Terraform 을 이용하여 생성할 수도 있지만, 어짜피 Terraform 으로 관리할 리소스가 아니므로 직접 생성한다.
공부를 위해 Terraform으로 생성해 보는 것도 추천한다.

  1. Storage 생성
    • Resource Group, Storage Account, Container 필요
    • # Resource Group 생성
      az group create --name tfstate --location koreacentral

      # Storage Account 생성
      az storage account create --resource-group tfstate --name tfstate --sku Standard_LRS --encryption-services blob

      # Container 생성
      az storage container create --name tfstate --account-name tfstate

      # KEY 환경 설정
      ACCOUNT_KEY=$(az storage account keys list --resource-group tfstate --account-name tfstate --query '[0].value' -o tsv)
      export ARM_ACCESS_KEY=$ACCOUNT_KEY

  2. Terraform 설정
    • terraform {
      required_providers {
      azurerm = {
      source = "hashicorp/azurerm"
      version = "~> 3.0.2"
      }
      }
      backend "azurerm" {
      resource_group_name = "tfstate"
      storage_account_name = "tfstate"
      container_name = "tfstate"
      key ="terraform.tfstate"
      }
      }

Key : Container에 저장할 tfstate의 이름

Comments