본문 바로가기

DevOps/Terraform

[T102 7주차] (19) 테라폼 Cloud 사용법(2)

cloudNet@ 팀의 가시다 님이 진행하는 테라폼 102 스터디 7주차 정리입니다.

 

2. Terraform Cloud 사용해보기 


✅ Fork 후 클론 

# Fork
https://github.com/terraform101/terraform-aws-tfc-workflow

✅ Remote Repository 클론 및 TFC 셋팅

 

MyGit=<>
MyGit=themapisto
git clone https://github.com/$MyGit/terraform-aws-tfc-workflow
cd terraform-aws-tfc-workflow

MyTfcOrg=<각자 자신의 TFC 조직명>
MyTfcOrg=themapisto-org
sed -i -e "s/<MY-ORG>/$MyTfcOrg/g" main.tf

#
git add main.tf
git commit -m "init"
git push

#
terraform init

 

✅  workspace 생성 후 Setting - Remote로 유지 

 

✅  실습 1 .  테라폼 클라우드 ( Remote )

 

 

  • plan
terraform plan #-var=prefix=dev

╷
│ Error: No value for required variable
│ 
│   on variables.tf line 1:
│    1: variable "prefix" {
│ 
│ The root module input variable "prefix" is not set, and has no default
│ value. Use a -var or -var-file command line argument to provide a value for
│ this variable.
╵
Operation failed: failed running terraform plan (exit 1)

  • variable 변수 등록 : TFC 리모트 실행 환경에서의 테라폼 입력 변수와 시스템 환경 변수 관리 : 워크스페이스 → Variables

✔️ 입력되는 변수 형태는 테라폼 코드에서 입력되는 입력 변수 Terraform variable

 

  • (입력 변수 : TFC에서는 실행 후 인라인으로 넣을 수 없기 때문이며, 기존 terraform.tfvars 를 대체하는 역할을 수행함.)
  • 따라서 기존 terraform.tfvars 파일은 동작하지 않는다

 

✔️ 테라폼이 실행되는 환경에서 환경 변수로 읽어 오는 시스템 환경 변수 Environment variable를 입력할 수 있다

 

  • 환경 변수 : 테라폼 실행 환경은 더 이상 로컬 작업 환경이 아닌 원격지의 TFC 환경이다. 프로비저닝하는 클라우드의 API 키 또는 테라폼 실행 설정 등이 구성되어야 하는 경우와 같은 상황에서 사용된다

  • 아까 난 에러는 variable.prefix에 입력값이 없어서 난 에러이다.
# variable.tf 

variable "prefix" {
  description = "This prefix will be included in the name of most resources."
  # default = "koo"
}
  • 이부분을 TFC의 variable에서 세팅해보자.

  • 추가로 AWS credential도 등록해야 한다.  AWS_ACCESS_KEY_ID / AWS_SECRET_ACCESS_KEY
  • plan 실행!

  • apply 실행
  • confirm & apply 

  • detail Overview

Remote 모드 , Auto apply Off

  • 완료
null_resource.configure-cat-app: Still creating... [1m0s elapsed]
null_resource.configure-cat-app (remote-exec):  _______________
null_resource.configure-cat-app (remote-exec): < Mooooooooooo! >
null_resource.configure-cat-app (remote-exec):  ---------------
null_resource.configure-cat-app (remote-exec):         \   ^__^
null_resource.configure-cat-app (remote-exec):          \  (oo)\_______
null_resource.configure-cat-app (remote-exec):             (__)\       )\/\
null_resource.configure-cat-app (remote-exec):                 ||----w |
null_resource.configure-cat-app (remote-exec):                 ||     ||
null_resource.configure-cat-app: Creation complete after 1m1s [id=45609032449001320]

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

Outputs:

catapp_ip = "http://13.124.174.113"
catapp_url = "http://ec2-13-124-174-113.ap-northeast-2.compute.amazonaws.com"
  • 삭제 
terraform destroy -auto-approve