오늘의 인기 글
최근 글
최근 댓글
Today
Total
05-20 11:35
관리 메뉴

우노

[Terraform] terraform import 본문

DevOps/Terraform

[Terraform] terraform import

운호(Noah) 2023. 4. 18. 16:04

들어가기 앞서,

  • Terraform으로 특정 자원을 생성할 경우,
  • 해당 자원이 이미 생성되어 있는 상태라면, already exist 에러가 발생하게 됩니다.
  • 따라서, 해당 포스팅에선 terraform import를 사용해,
  • 이미 생성되어 있는 자원을 유지하면서 Terraform State 파일을 업데이트할 수 있는 방법을 다뤄보겠습니다.

생성하고자 하는 리소스 블록 선언

resource "google_cloud_scheduler_job" "test" {
}
  • 리소스 블록을 선언하지 않을 경우, 아래와 같은 에러가 발생하게 됩니다.
    • Before importing this resource, please create its configuration in the root module.

Terraform State 파일 업데이트 (terraform import)

terraform import <module.모듈명.리소스공식명.리소스정의명> <실제리소스명>

Terraform State 파일 업데이트 확인

# 모든 자원들의 세부 정보 출력
terraform show

# 모든 자원들을 리스트형식으로 출력
terraform state list

Terraform State와 실제 리소스 간 Diff 확인 및 수정

resource "google_cloud_scheduler_job" "test" {
  # (실제 리소스와 동일한 설정을 하시거나 수정하시면 됩니다.)
}
terraform plan
terraform apply
Comments