우선 환경 구성에 앞서서 Terraform을 사용할 수
있게 Terraform 공식 홈페이지에 들어가서 Terraform 버전을 다운로드합니다.
그리고 다음과 같이 자신의 버전에 맞게 설치를
진행합니다.
설치가 완료되었다면, 사진과 같이 C드라이브에
압축해제를 하고 윈도우즈 키와 R키를 누르고 "sysdm.cpl ,3" 을 입력하여 실행합니다.
환경 변수를 선택하고 시스템 변수에서 Path를
더블클릭합니다.
그리고 다음과 같이 Terraform 디렉토리
위치를 입력하고 확인하고 나옵니다.
Terraform 환경변수가 제대로 입력되었는지
확인하기 위해 PowerShell에서 terraform version을 통해 확인합니다.
버전이 나올경우 성공! ><
PS C:\Users\jinwoo> terraform version
Terraform v1.1.8
on windows_amd64
Your version of Terraform is out of date! The latest version
is 1.1.9. You can update by downloading from https://www.terraform.io/downloads.html
Terraform 간단한 용어 설명 및 구성
이전에 Terraform
용어에 대해서 간단하게 설명한 적이 있습니다.
실습에서는 Plan, Apply,
validate, destroy를 사용합니다.
모르는 용어가 나오면 표를 통해
확인합시다!
Terraform 리소스 구성 및 구축
자! 이제
VScode에서 terraform code를 작성해봅시다!
작업을 하는 곳에 디렉토리를
만들고,
VScode로 들어가서
워크스페이스를 추가합니다.
디렉토리를 추가하면 다음과 같이
나타납니다.
그리고 그림과 같이 파일을
추가합니다.
파일이름은 main.tf라고
지어보겠습니다.
코드를 작성하기위해서 Terraform 공식 사이트로 들어가서 하시코프 테라폼 공급자를 main.tf에 입력합니다.
# Azure Provider source and version being used
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "=3.0.0"
}
}
}
# Configure the Microsoft Azure Provider
provider "azurerm" {
features {}
}
여기서
잠깐!
코드를 입력했을 때 인식을 못 할 경우,
VScode의 확장에서 hashicorp terraform을 설치합니다!
그러면 다음과 같이 이쁘게 생성됩니다!
이제 디렉토리로 이동했다면 terraform
init을 통해 초기화해줍니다.
\azure_terraform> terraform init
그럼 다음과 같이 Terraform을
시작할 수 있는 것을 확인할 수 있습니다.
Initializing the backend...
Initializing provider plugins...
- Finding hashicorp/azurerm versions matching "3.0.0"...
- Installing hashicorp/azurerm v3.0.0...
- Installed hashicorp/azurerm v3.0.0 (signed by HashiCorp)
Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.
Terraform has been successfully initialized!
You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.
If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
Init이 끝났으면 유효성을 검증을 한후
성공되면 성공입니다!
\azure_terraform> terraform validate
Success! The configuration is valid.
유효성 검증까지 마치면 VScode에서
다음과 같이 테라폼 프로그램과 init 파일이 생성되는 것을 확인할 수 있습니다.
이제 리소스 그룹을
생성해봅시다!
생성에 앞서서 plan을 통해 어떤
리소스를 배포할 계획인지 알아봅니다.
리소스 그룹 코드는 다음과 같이
실행합니다.
\azure_terraform> terraform plan
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:
# azurerm_resource_group.azure_rg will be created
+ resource "azurerm_resource_group" "azure_rg" {
+ id = (known after apply)
+ location = "koreacentral"
+ name = "test_rg"
}
Plan: 1 to add, 0 to change, 0 to destroy.
댓글