fullmoon's bright IT blog

[T103] 1주차 - 기본 사용 1/3 (3) 본문

Cloud/AWS

[T103] 1주차 - 기본 사용 1/3 (3)

휘영청 2023. 9. 3. 13:25
728x90

3.주요 커맨드

  • 실습 디렉터리 생성 (workspaces) → VS Code에서 폴더 열기 ⇒ VS Code에서 터미널 열기
    • VS Code 폴더 생성 (03.start) → 새파일 생성 (main.tf)
# 실습 디렉터리 생성 후 이동
mkdir workspaces
cd workspaces

# 테라폼 실행
terraform
Usage: terraform [-version] [-help] <command> [args]
...

# 테라폼 실행을 위해 코드 파일이 있는 디렉터리로 이동
# (참고) 테라폼이 실행되는 디렉터리 = 모듈(테라폼 코드 파일과 변수 파일), 기본 작업디렉터리는 '루트 모듈', 호출 모듈은 '자식 모듈'
cd 03.start/


# 초기화 : 코드 사용 구문 기반으로 필요한 프로바이더 플러그인을 찾고 설치, 추가로 '프로바이더/모듈/백엔드' 구성 설정/변경 시 수행 필요
terraform init
ls -al
tree .terraform  # VS Code에서 탐색기 확인

terraform init 후 plan으로 파일이 만들어 지는 것을 확인

와 컴퓨터도 이렇게 테라폼으로 할 수 있다니 

하나의 리소스가 추가되고, 변경되거나 삭제되는 것은 없을 예정

 

  • -detailed-exitcode : plan 추가 옵션으로, 파이프라인 설계에서 활용 가능, exitcode가 환경 변수로 구성됨
  • -auto-approve 자동 승인 기능 부여 옵션

plan 결과를 시스템 코드로 출력 terraform plan -detailed-exitcode

C:\terraform\03.start>terraform plan -detailed-exitcode

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # local_file.abc will be created
  + resource "local_file" "abc" {
      + content              = "abc!"
      + content_base64sha256 = (known after apply)
      + content_base64sha512 = (known after apply)
      + content_md5          = (known after apply)
      + content_sha1         = (known after apply)
      + content_sha256       = (known after apply)
      + content_sha512       = (known after apply)
      + directory_permission = "0777"
      + file_permission      = "0777"
      + filename             = "./abc.txt"
      + id                   = (known after apply)
    }

Plan: 1 to add, 0 to change, 0 to destroy.

──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── 

Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform apply" now.

그럼 파일을 수정해보고 다시 해보자

terraform fmt 을 보면 format 또는 reformat 줄임 표시로 terraform tmt 명령어로 수행, 테라폼 구성 파일을 표준 형식과 표준 스타일로 적용. 코드 가독성 높임

 

내용 추가 후 다시 만들어보자

resource "local_file" "abc" {
content = "abc!"
filename = "${path.module}/abc.txt"
}

resource "local_file" "dev" {
  content  = "def!"
  filename = "${path.module}/def.txt"
}

추가된 내용에서만 달라짐

 

terraform state list 를 사용하면 현재 상태에 있는 파일을 알 수있음

-replace : 프로비저닝이 완료 후 사용자에 필요에 의해 특정 리소스를 삭제 후 다시 생성. plan, apply 모두 적용 가능

 

리소스도 삭제해보자

  • destroy 제거 & fmt
    • 테라폼 구성에서 관리하는 모든 개체제거하는 명령어

그렇다면 특정 리소스만 어떻게 지울 수 있을까?

terraform state 로 힌트 > 그 리소스만 만들고 다시 지워보자

다시 만든 후에

 

특정한 것만 지우면

짜잔!

 

하나만 남고 지워졌다

이제 이것도 지워버리쟈쟈쟈

테라폼 이번에는 너 제대로 배운다 ㅋㅋ

728x90