이 프로젝트는 Terraform을 사용하여 Google Cloud Platform(GCP)의 인프라를 코드로 관리합니다.
개발(dev), 운영(prod), 공유(shared) 환경을 워크스페이스 기반으로 분리하며, 모듈화를 통해 코드 중복을 제거했습니다.
Terraform이 GCP에 접근하려면 인증이 필요합니다.
gcloud auth application-default login- GCP 콘솔에서 서비스 계정 키(JSON) 생성
terraform.tfvars에 경로를 지정
# terraform.tfvars 예시
credential_file_path = "your/path/to/service-account.json"이 프로젝트는 terraform workspace를 활용하여 dev, prod, shared 환경을 분리합니다.
terraform workspace new dev
terraform workspace new prod
terraform workspace new shared
terraform workspace select dev # 사용하고 싶은 환경 선택terraform initterraform planterraform applyterraform destroy이미 수동으로 생성된 GCP 리소스를 Terraform 상태에 등록하려면 아래 명령어를 사용합니다.
terraform import "module.compute_dev[0].google_compute_instance.this" "projects/PROJECT/zones/ZONE/instances/INSTANCE_NAME"zsh 사용자라면
[]가 포함된 주소는 반드시 큰따옴표로 감싸야 합니다!
terraform.tfstate,.terraform/,terraform.tfvars는 Git에 절대 커밋하지 마세요!.terraform.lock.hcl은 커밋해서 provider 버전을 고정하세요.terraform plan→ 팀원과 공유 →terraform apply순서를 반드시 지켜주세요.
terraform refresh상태 파일과 실제 인프라를 동기화합니다.
이미 존재하는 리소스를 Terraform 상태에 등록하세요:
terraform import "..." "..."lifecycle {
prevent_destroy = true
}lifecycle {
ignore_changes = ["description"]
}