본문 바로가기

퍼블릭 클라우드

CentOS 7에서 Terraform을 설치하는 방법

반응형

CentOS 7에서 Terraform을 설치하는 방법

Terraform은 HashiCorp에서 제공하는 IaC(Infrastructure as Code) 도구로 클라우드 및 온프레미스 인프라를 코드로 관리할 수 있습니다.

Terraform 설치

yum-utils 설치

yum install -y yum-utils

HashiCorp Repository 추가

yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo

Terraform 설치

yum install -y terraform

설치 확인

terraform -version
Terraform v0.14.5

기본 사용법

도움말 확인

$ terraform -help
Usage: terraform [global options] <subcommand> [args]

The available commands for execution are listed below.
The primary workflow commands are given first, followed by
less common or more advanced commands.

Main commands:
  init          Prepare your working directory for other commands
  validate      Check whether the configuration is valid
  plan          Show changes required by the current configuration
  apply         Create or update infrastructure
  destroy       Destroy previously-created infrastructure

All other commands:
  console       Try Terraform expressions at an interactive command prompt
  fmt           Reformat your configuration in the standard style
  force-unlock  Release a stuck lock on the current workspace
  get           Install or upgrade remote Terraform modules
  graph         Generate a Graphviz graph of the steps in an operation
  import        Associate existing infrastructure with a Terraform resource
  login         Obtain and save credentials for a remote host
  logout        Remove locally-stored credentials for a remote host
  output        Show output values from your root module
  providers     Show the providers required for this configuration
  refresh       Update the state to match remote systems
  show          Show the current state or a saved plan
  state         Advanced state management
  taint         Mark a resource instance as not fully functional
  untaint       Remove the 'tainted' state from a resource instance
  version       Show the current Terraform version
  workspace     Workspace management

Global options (use these before the subcommand, if any):
  -chdir=DIR    Switch to a different working directory before executing the
                given subcommand.
  -help         Show this help output, or the help for a specified subcommand.
  -version      An alias for the "version" subcommand.

주요 명령어

  • init : 작업 디렉토리 초기화
  • validate : 구성 파일 검증
  • plan : 변경 사항 미리보기
  • apply : 인프라 생성/변경
  • destroy : 인프라 삭제
728x90

실행 흐름(표준 워크플로우)

terraform init
terraform validate
terraform plan
terraform apply

plan 상세 옵션 확인

terraform -help plan
Usage: terraform plan [options] [DIR]

  Generates a speculative execution plan, showing what actions Terraform
  would take to apply the current configuration. This command will not
  actually perform the planned actions.

  You can optionally save the plan to a file, which you can then pass to
  the "apply" command to perform exactly the actions described in the plan.

Options:

  -compact-warnings   If Terraform produces any warnings that are not
                      accompanied by errors, show them in a more compact form
                      that includes only the summary messages.

  -destroy            If set, a plan will be generated to destroy all resources
                      managed by the given configuration and state.

  -detailed-exitcode  Return detailed exit codes when the command exits. This
                      will change the meaning of exit codes to:
                      0 - Succeeded, diff is empty (no changes)
                      1 - Errored
                      2 - Succeeded, there is a diff

  -input=true         Ask for input for variables if not directly set.

  -lock=true          Lock the state file when locking is supported.

  -lock-timeout=0s    Duration to retry a state lock.

  -no-color           If specified, output won't contain any color.

  -out=path           Write a plan file to the given path. This can be used as
                      input to the "apply" command.

  -parallelism=n      Limit the number of concurrent operations. Defaults to 10.

  -refresh=true       Update state prior to checking for differences.

  -state=statefile    Path to a Terraform state file to use to look
                      up Terraform-managed resources. By default it will
                      use the state "terraform.tfstate" if it exists.

  -target=resource    Resource to target. Operation will be limited to this
                      resource and its dependencies. This flag can be used
                      multiple times.

  -var 'foo=bar'      Set a variable in the Terraform configuration. This
                      flag can be set multiple times.

  -var-file=foo       Set variables in the Terraform configuration from
                      a file. If "terraform.tfvars" or any ".auto.tfvars"
                      files are present, they will be automatically loaded.

Amazon Linux 2에 Terraform을 설치하는 방법

TERRAFORM_VER=`curl -s https://api.github.com/repos/hashicorp/terraform/releases/latest |  grep tag_name | cut -d: -f2 | tr -d \"\,\v | awk '{$1=$1};1'`
wget https://releases.hashicorp.com/terraform/${TERRAFORM_VER}/terraform_${TERRAFORM_VER}_linux_amd64.zip
unzip terraform_${TERRAFORM_VER}_linux_amd64.zip
mv terraform /usr/local/bin/
terraform version
Terraform v1.0.8
on linux_amd64

최신 버전 자동 설치(스크립트)

#!/bin/bash
set -e

echo "== Install Terraform (Latest) =="

# 최신 버전 조회
TERRAFORM_VER=$(curl -s https://api.github.com/repos/hashicorp/terraform/releases/latest \
| grep tag_name | cut -d: -f2 | tr -d '",v ')

echo "[INFO] Latest Version: $TERRAFORM_VER"

# 다운로드
wget https://releases.hashicorp.com/terraform/${TERRAFORM_VER}/terraform_${TERRAFORM_VER}_linux_amd64.zip

# 압축 해제
unzip terraform_${TERRAFORM_VER}_linux_amd64.zip

# 설치
sudo mv terraform /usr/local/bin/
chmod +x /usr/local/bin/terraform

# 확인
terraform version

echo "[INFO] Terraform installation completed."

 

참고URL

- Install Terraform : https://developer.hashicorp.com/terraform/downloads

- Install Terraform(Tutorials/AWS/Install) : https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli

 

728x90
반응형