서버 인프라/DevOps

Terraform 훑어보기

트리맨스 2023. 4. 2. 23:35
반응형

 

이번에 회사에서 terraform을 사용해서 간단히 정리해 보았다.

 

Terraform이란


클라우드, 온프레미스 리소스들을 코드로 정의할 수 있는 도구이다. 컴퓨팅 리소스, 스토리지, 네트워킹 리소스, DNS, SaaS 등의 다양한 구성 용소를 코드로 정의할 수도 있다. 테라폼 API를 통해서 앞에서 말한 리소스들을 지정할 수 있다. 

코드로 정의하기 때문에 기존의 코드를 작성하는 것처럼 협업이 가능하고 버전 관리가 용이하다.

 

테라폼의 워크플로에는 주로 3개의 단계가 있다. 먼저 코드를 작성하는 write 단계가 있다. 말 그대로 리소스를 코드로 작성한다. 두 번쨰는 plan이다. 기존에 있던 인프라를 기반으로 이후 변화될 계획을 구상한다. 구상하는 과정은 테라폼 자체적으로 알아서 순서를 정한다. 마지막으로는 apply를 한다. 이전 단계에 있던 plan을 실제로 적용하는 단계이다.

 

권장하는 테라폼 프로젝트 구조


  • 루트 모듈
  • readme
  • main.tf, variables.tf, outputs.tf
  • modules 폴더 아래에 중첩 모듈 사용, example 폴더 아래에 중첩 모듈 사용
  • key, secret 같은 중요 파일에 대한 내용은 *.tfvars 파일 안에 저장한 후, gitignore에 *.tfvars를 추가하여 보안을 유지한다.
$ tree complete-module/
.
├── README.md
├── main.tf
├── variables.tf
├── outputs.tf
├── ...
├── modules/
│   ├── nestedA/
│   │   ├── README.md
│   │   ├── variables.tf
│   │   ├── main.tf
│   │   ├── outputs.tf
│   ├── nestedB/
│   ├── .../
├── examples/
│   ├── exampleA/
│   │   ├── main.tf
│   ├── exampleB/
│   ├── .../

 

aks에 kubernetes를 올리는 예시 파일을 확인하자.

# main.tf

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "example" {
  name     = "${var.prefix}-k8s-resources"
  location = var.location
}

resource "azurerm_kubernetes_cluster" "example" {
  name                = "${var.prefix}-k8s"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
  dns_prefix          = "${var.prefix}-k8s"

  default_node_pool {
    name       = "default"
    node_count = 1
    vm_size    = "Standard_DS2_v2"
  }

  identity {
    type = "SystemAssigned"
  }
}

main.tf 파일을 보면 resource로 생성할 리소스에 대해서 설정을 해 두었다.

 

# outputs.tf
output "id" {
  value = azurerm_kubernetes_cluster.example.id
}

output "kube_config" {
  value = azurerm_kubernetes_cluster.example.kube_config_raw
}

output "client_key" {
  value = azurerm_kubernetes_cluster.example.kube_config.0.client_key
}

output "client_certificate" {
  value = azurerm_kubernetes_cluster.example.kube_config.0.client_certificate
}

output "cluster_ca_certificate" {
  value = azurerm_kubernetes_cluster.example.kube_config.0.cluster_ca_certificate
}

output "host" {
  value = azurerm_kubernetes_cluster.example.kube_config.0.host
}

outputs.tf는 다른 곳에서 생성된 클러스터에 대한 정보를 알 수 있게 지정한다.

 

# variable.tf
variable "prefix" {
  description = "A prefix used for all resources in this example"
}

variable "location" {
  description = "The Azure Region in which all resources in this example should be provisioned"
}

여기서는 사실상 의미없는 변수들만 적어 둔 파일이다. 만약 secret같이 중요한 내용이 있는 경우에는 위에서 말했듯 .tfvars로 끝나는 파일에 변수를 지정한 다음, gitignore에 *.tfvars를 추가해 secret을 보호한다.

 

참고자료


공식 문서가 워낙 잘 되어 있어서 많이 참고했다.

https://developer.hashicorp.com/terraform/language

 

Overview - Configuration Language | Terraform | HashiCorp Developer

Use the Terraform configuration language to describe the infrastructure that Terraform manages.

developer.hashicorp.com

 

반응형