Terraform/Study

Terraform-101-Study Week6 - [협업, TFC]

식사법 2023. 8. 13. 01:36

이번 스터디 주제는 TFC관련된 내용이 많아

저는 Terraform Cloud 혹은 Terraform Enterprise를 Terraform code 를 통해 관리하는 모습을 소개하겠습니다

1. tfe Provider

  • terraform 을 통해 TFE, TFC를 관리하기 위해서는 tfe Provider을 사용합니다. 해당 프로바이더를 사용해 Org, Workspace, Project, Team, Variable set 등 대부분의 TFC, TFE 기능들을 관리할 수 있습니다.
  • 링크 - TFE Provider

하단에서 간단하게 제가 제 Terraform Cloud를 어떻게 관리하고 있는지 소개하겠습니다.

2. 기초 설정

  • Org, Workspace, Project , Team 등을 TFE Provider를 사용하여 관리하는 코드들 입니다
  • team 설정 또한 가능하지만, TFC 무료 버전의 경우 team 기능을 지원하지 않습니다.

1) Organization

#variable
variable "org_name" {
  type        = string
  description = "terraform enterprise organization name"
}

variable "org_email" {
  type        = string
  description = "terrafrom enterprise email address"
}
# org.tf
resource "tfe_organization" "test-org" {
  name  = var.org_name #조직 이름
  email = var.org_email #조직 이메일
}
  • TFC Console 확인

2) Project & Workspace

#variable
variable "project_name" {
  type        = string
  description = "terraform enterprise project_name"
}

variable "ws_list" {
  type = list(any)
}
variable "tag_names" {
  description = "creating workspace tag name "
}

variable "exec_mode" {
  description = "workspace execution mode local or remote"
}

variable "tfe_ver" {
  description = "creating using terraform version"
}
resource "tfe_project" "test-project" {
  organization = tfe_organization.test-org.name
  name         = var.project_name
}

resource "tfe_workspace" "test-ws" {
  count             = length(var.ws_list)
  name              = var.ws_list[count.index]
  project_id        = tfe_project.test-project.id
  organization      = tfe_organization.test-org.name
  execution_mode    = var.exec_mode
  tag_names         = var.tag_names
  terraform_version = var.tfe_ver
}
  • TFC Console 확인

3. Variable Set 설정

  • TFC 에서 Variable Set 또한 tfe Provider 를 사용하여 관리할 수 있습니다.
#variable
variable "aws_secret" {
  description = "aws access key"
}

variable "aws_key" {
  description = "aws access key id"
}
#varialbe Set 생성
resource "tfe_variable_set" "test-aws-sec" {
  depends_on   = [tfe_organization.test-org]
  name         = "AWS Credentials"
  description  = "AWS Credentials for projecdt"
  organization = var.org_name
}

#variable Set 내의 변수 생성 ( AWS ACCESS KEY )
resource "tfe_variable" "aws-key-id" {
  key             = "AWS_ACCESS_KEY_ID"
  value           = var.aws_key
  category        = "env"
  description     = "AWS_ACCESS_KEY_ID 의 값"
  variable_set_id = tfe_variable_set.test-aws-sec.id
}

#Variable Set 내의 변수 생성 ( AWS SECRET KEY )
resource "tfe_variable" "aws-sec-key" {
  key             = "AWS_SECRET_ACCESS_KEY"
  value           = var.aws_secret
  category        = "env"
  sensitive       = true # Sensitive 를 ture로 함으로서 console상에서도 값을 숨길수 있습니다
  description     = "AWS_SECRET_ACCESS_KEY 의 값"
  variable_set_id = tfe_variable_set.test-aws-sec.id
}
  • TFC Console 확인
    • 예시를 보여주기 위함으로 실제 값을 넣지는 않았다

4. Notification 설정

  • Terraform Cloud 에서 특정 행위 ( apply, plan , destory 등 ) 이 이루어 졌을 때 알림을 받도록 할 수 있습니다
  • 참고로 notification의 경우 workspace가 remote 환경으로 설정되어 있을 때에만 설정 가능하다. 하지만 tfe Provider 설정시 local 환경에서도 Console에서 확인이 불가능 하지만 설정은 할 수 있습니다.
#variable

# notification 에 대한 webhook url 이 필요합니다.
variable "noti_url" {
  type = string
}
resource "tfe_notification_configuration" "worksapce-notification" {
  count            = length(tfe_workspace.test-ws)
  name             = "notification"
  enabled          = true
  destination_type = "slack" # 저는 슬랙을 통해 알림을 받고 있습니다
    # 생성, 플랜, 에러 시 알림을 출력하도록 하였습니다.
  triggers         = ["run:created", "run:planning", "run:errored"]
  url              = var.noti_url
  workspace_id     = tfe_workspace.test-ws[count.index].id
}
  • TFC Console 밑 Slack 연동 확인