AWS #3 terraform tfstate 트러블슈팅

AWS #3 terraform tfstate 트러블슈팅 #

#2026-02-08


#1 terraform 트러블슈팅

git add . 하려는데 아래 경고가 떴다

warning: 내장 깃 저장소 추가: terraform/.terraform/modules/vpc
hint: You've added another git repository inside your current repository.
hint: Clones of the outer repository will not contain the contents of
hint: the embedded repository and will not know how to obtain it.
hint: If you meant to add a submodule, use:
hint:
hint:       git submodule add <url> terraform/.terraform/modules/vpc
hint:
hint: If you added this path by mistake, you can remove it from the
hint: index with:
hint:
hint:       git rm --cached terraform/.terraform/modules/vpc
hint:
hint: See "git help submodule" for more information.
hint: Disable this message with "git config advice.addEmbeddedRepo false"

.terraform은 gitignore 해야한대서 아래와 같이 수정!!

git rm --cached -r terraform/.terraform
echo ".terraform/" >> .gitignore
git add .gitignore 
git commit -m "Add Docker and AWS ECS deployment pipeline" 
git push origin main 2>&1

#

#2 tfstate 트러블슈팅

디렉토리 삭제하고, 똑같이 StudyNote라는 디렉토리 만들어서 git init으로 세팅하면 완전 동일하게 세팅되지 않고 gitignore 등등 된거는 깃허브에 안올라가있으니까 누락된다.

누락되는 파일과 복구 방법

  • node_modules/: cd frontend && npm install
  • .terraform/: cd terraform && terraform init
  • *.tfstate: 복구 불가
  • dist/: npm run build
  • .env: 직접 다시 작성
  • pycache/: 자동 생성됨

대부분 명령어로 복구 가능한데, terraform.tfstate는 복구 불가. 이 파일이 없으면 Terraform이 AWS에 이미 만든 리소스를 모르게 돼서 관리가 안됨

해결 방법은 tfstate를 S3에 저장하는것.

# S3 버킷 생성
aws s3 mb s3://studynote-terraform-state --region ap-northeast-2
#그 후 terraform/main.tf에서 주석 해제:
backend "s3" {
  bucket         = "studynote-terraform-state"
  key            = "prod/terraform.tfstate"
  region         = "ap-northeast-2"
  encrypt        = true }

#