DevOps

[DevOps/03]The Core Terraform Workflow 테라폼 코어 워크플로우

박한결 2021. 4. 6. 10:34

 

Write(init)

- 원하는 에디터에서 코드를 작성하는 것처럼 테라폼 구성을 작성

- 다른 명령어(plan, apply 등)를 위한 설정 작성

- 내부적으로 provider, state, module 설정 등이 존재

- 개인/팀에 관계없이 버전 관리 저장소에 작업을 저장하는 것이 일반적

# Create repository
$ git init my-infra && cd my-infra

Initialized empty Git repository in /.../my-infra/.git/

# Write initial config
$ vim main.tf

# Initialize Terraform
$ terraform init

Initializing provider plugins...
# ...
Terraform has been successfully initialized!

- Configuration을 작성하면서 plan을 반복적으로 실행하면 구문(syntax) 오류를 제거하고 configuration이 예상대로 함께 실행되는지 확인할 수 있음

# Make edits to config
$ vim main.tf

# Review plan
$ terraform plan

# Make additional edits, and repeat
$ vim main.tf

- 이는 코드 편집과 테스트 명령 실행 사이의 긴밀한 피드백 루프가 유용한 개별 애플리케이션 코드 작업과 유사함

Plan

- Write 단계의 피드백 루프가 괜찮아 보이는 변화를 가져 오면 작업을 커밋하고 최종 계획을 검토

$ git add main.tf
$ git commit -m 'Managing infrastructure as code!'

[master (root-commit) f735520] Managing infrastructure as code!
 1 file changed, 1 insertion(+)

- terraform apply will display a plan for confirmation before proceeding to change any infrastructure

- that's the command you run for final review

- 실제로 작성한 테라폼 코드가 어떻게 만들어지는지 미리보기를 제공

- apply는 실제 인프라에 영향을 주는 명령어

- plan에 문제가 없어야 apply에 문제가 없을 확률이 높음

$ terraform apply

An execution plan has been generated and is shown below.
# ...

- 따라서 변경사항이 있을 시 반복적으로 plan을 사용해 확인한 후에 apply할 것 

Apply

- tell Terraform to provision real infrastructure

Do you want to perform these actions?

  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.
  Enter a value: yes

# ...

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

- 이 시점에서 안전을 위해 버전 관리 저장소를 원격 위치로 푸시하는 것이 일반적

$ git remote add origin https://github.com/*user*/*repo*.git
$ git push origin master

 

 

이외에도 자주 사용되는 명령어로는 import, state, destroy가 있다.