๐Ÿ“‚ IaC/Terraform

[Terraform] Variable ์ž…๋ ฅ ๋ณ€์ˆ˜

dhyuck 2022. 9. 3. 21:58
๋ฐ˜์‘ํ˜•

variables / ์ž…๋ ฅ ๋ณ€์ˆ˜

  • ์ž…๋ ฅ ๋ณ€์ˆ˜(Input variable)๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํ…Œ๋ผํผ ๊ตฌ์„ฑ ํŒŒ์ผ์„ ์ˆ˜์ •ํ•˜์ง€ ์•Š๊ณ  ์œ ์—ฐํ•˜๊ฒŒ ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์ž…๋ ฅ ๋ณ€์ˆ˜๋Š” ํ…Œ๋ผํผ์ด ์‹คํ–‰ ๋˜๊ธฐ ์ „์— ํ• ๋‹น๋˜๊ธฐ์— ์ธํ”„๋ผ๋ฅผ ๋ณด๋‹ค ์•ˆ์ „ํ•˜๊ฒŒ ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๋ณ€์ˆ˜ ์„ ์–ธ์€ ๋ชจ๋“  ๊ตฌ์„ฑํŒŒ์ผ์˜ ๋ชจ๋“  ์œ„์น˜์—์„œ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, variables.tf์—์„œ ์„ ์–ธ ํ•  ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค.

๋ณ€์ˆ˜ ์„ ์–ธ ๋ธ”๋Ÿญ

  • description : ๋ณ€์ˆ˜์˜ ๋ชฉ์ ์„ ๋ฌธ์„œํ™”ํ•˜๋Š” ๊ฐ„๋‹จํ•œ ์„ค๋ช…
  • type : ๋ณ€์ˆ˜์˜ ๋ฐ์ดํ„ฐ ํƒ€์ž…
  • default : ๋ณ€์ˆ˜์˜ ๊ธฐ๋ณธ ๊ฐ’
  • ๋ชจ๋“  ๋ณ€์ˆ˜์— ๋Œ€ํ•ด description๊ณผ type์„ ์ง€์ •ํ•˜๊ณ , ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ default๊นŒ์ง€ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.
variable "aws_region" {
  description = "AWS region"
  type        = string
  default     = "us-west-2"
}
  • ํ…Œ๋ผํผ ๊ตฌ์„ฑ์—์„œ ์ž…๋ ฅ ๋ณ€์ˆ˜๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜ํ™”(parameterize)ํ•˜๋ ค๋ฉด ์ž…๋ ฅ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜๊ณ  ๊ตฌ์„ฑ ํŒŒ์ผ์—์„œ ํ•˜๋“œ์ฝ”๋”ฉ๋œ ์ •๋ณด๋ฅผ ๋ณ€์ˆ˜์— ๋Œ€ํ•œ ์ฐธ์กฐ๋กœ ๋ณ€๊ฒฝํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.
  • var.<variable_name>์œผ๋กœ ๋ณ€์ˆ˜๋ฅผ ์ฐธ์กฐ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ํ‘œํ˜„์‹์„ ๋ฌธ์ž์—ด์— ์‚ฝ์ž…ํ•˜๋Š” ๋ฐฉ๋ฒ•์ธ Interpolation(๋ณด๊ฐ„)๋„โ€œ${var.cluster_name}-web-sgโ€์™€ ๊ฐ™์ด ๊ฐ€๋Šฅ

๋ณ€์ˆ˜ ์ž๋ฃŒํ˜•

  • number

      variable "instance_count" {
        description = "Number of instances to provision."
        type        = number
        default     = 2
      }
  • bool

      variable "enable_vpn_gateway" {
        description = "Enable a VPN gateway in your VPC."
        type        = bool
        default     = false
      }
  • list

    • ๊ฐ™์€ ์œ ํ˜•์˜ ๊ฐ’ ์‹œํ€€์Šค, 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋Š” ์ธ๋ฑ์Šค๋กœ ๊ฐœ๋ณ„ ํ•ญ๋ชฉ์„ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

      variable "public_subnet_cidr_blocks" {
      description = "Available cidr blocks for public subnets."
      type        = list(string)
      default     = [
        "10.0.1.0/24",
        "10.0.2.0/24"
      ]
      }
  • map

    • ๊ฐ™์€ ์œ ํ˜•์˜ ๋ฐ์ดํ„ฐ๋ฅผ ํ‚ค์™€ ๊ฐ’์œผ๋กœ ์ผ์น˜์‹œํ‚ค๋Š” ๋ฃฉ์—…ํ…Œ์ด๋ธ”

    • ํ‚ค๋Š” ํ•ญ์ƒ ๋ฌธ์ž์—ด(string)์ด๊ณ , type์ด map(number)๋ผ๋ฉด ๊ฐ’์€ ์ˆซ์ž์ด๋‹ค.

    • var.resource_tags[โ€environmentโ€]์™€ ๊ฐ™์ด ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค

      variable "resource_tags" {
      description = "Tags to set for all resources"
      type        = map(string)
      default.    = {
        project     = "project-alpha", 
        environment = "dev"
      }
      }
  • set

    • ๊ฐ™์€ ์œ ํ˜•์ธ ๊ณ ์œ ํ•œ ๊ฐ’์ด ์ €์žฅ๋˜๋ฉฐ ์ •๋ ฌ๋˜์ง€ ์•Š๋Š” ์ž๋ฃŒํ˜•์ž…๋‹ˆ๋‹ค.

      variable "users" {
      description = "user names"
      type        = set(string)
      default     = ["nice", "value", "lion"]
      }
  • object

    • ์—ฌ๋Ÿฌ๊ฐ€์ง€ ํƒ€์ž…์˜ value๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š” ์ž๋ฃŒํ˜•

      variable "user_information" {
      type = object({
        no     = number
        name   = string
        leader = bool
      })
      default = {
        no     = 0,
        name   = "hyuckang"
        leader = true
      }
      }
  • tuple

    • ๊ณ ์ •๋œ ๊ธธ์ด๋กœ ์—ฌ๋Ÿฌ ํƒ€์ž…์˜ ๋ณ€์ˆ˜๋ฅผ ์žˆ๋Š” ์ž๋ฃŒํ˜•

      variable "team_leader_info" {
      description = "Team leader information"
      type        = tuple([number, string, bool])
      default     = [0, "hyuckang", true]
      }

๋ณ€์ˆ˜ ํ• ๋‹น

  • ๋ณ€์ˆ˜์— default๋ฅผ ์„ค์ •ํ•˜์ง€ ์•Š์œผ๋ฉด terraform apply ์ด์ „์— ๋ณ€์ˆ˜๋ฅผ ์ง€์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ…Œ๋ผํผ์€ unassigned variable์„ ์ง€์›ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • ํ…Œ๋ผํผ์— ๋ณ€์ˆ˜๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์—ฌ๋Ÿฌ๊ฐ€์ง€์ด๋ฉฐ, ์—ฌ๋Ÿฌ๊ฐœ์˜ ๋ณ€์ˆ˜๊ฐ€ ์ „๋‹ฌ๋˜์—ˆ์„ ๊ฒฝ์šฐ ์šฐ์„ ์ˆœ์œ„์— ๋”ฐ๋ผ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰์— ์ฐพ์€ ๊ฐ’์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
  • ๋ณ€์ˆ˜ ํ• ๋‹นํ•˜๋Š” ๋ฐฉ๋ฒ•
    1. prompted : ํ• ๋‹น๋˜์ง€ ์•Š์€ ๋ณ€์ˆ˜๊ฐ€ ์žˆ๋Š” ์ฑ„๋กœ plan, destroy, apply ํ•  ๊ฒฝ์šฐ ์‚ฌ์šฉ
    2. file : ํŒŒ์ผ๋ช…์ด terraform.tfvars, *.auto.tfvars ์ธ ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๋ฉด ์ž๋™์œผ๋กœ ๋ณ€์ˆ˜๊ฐ€ ๋กœ๋”ฉ ๋˜๋ฉฐ, -var-file ํ”Œ๋ž˜๊ทธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค๋ฅธ ํŒŒ์ผ๋ช…์„ ๊ฐ€์ง„ ๋ณ€์ˆ˜ ํŒŒ์ผ๋„ ๋กœ๋“œ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    3. CLI flag : CLI flag๋ฅผ ํ†ตํ•ด์„œ ๋ณ€์ˆ˜๋ฅผ ํ• ๋‹น

๋ณ€์ˆ˜ ์œ ํšจ์„ฑ ๊ฒ€์ฆ

  • ๋ฆฌ์†Œ์Šค๋ฅผ ์„ ์–ธํ•  ๋•Œ ๋ฆฌ์†Œ์Šค์˜ ์ด๋ฆ„์˜ ๊ธธ์ด, ํ—ˆ์šฉ๋˜๋Š” ๋ฌธ์ž ๋“ฑ์˜ ์ œํ•œ์ด ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค.

    ์ด๋ฅผ ์œ„ํ•ด variable ๋ธ”๋Ÿญ ์•ˆ์˜ validation์œผ๋กœ ๋ณ€์ˆ˜์˜ ๊ฐ’์„ ์‚ฌ์ „์— ๊ฒ€์ฆ ํ•  ์ˆ˜ ์žˆ๋‹ค.

      variable "resource_tags" {
        description = "Tags to set for all resources"
        type        = map(string)
        default     = {
          project     = "my-project",
          environment = "dev"
        }
    
        validation {
          condition     = length(var.resource_tags["project"]) <= 16
          error_message = "The project tag must be no more than 16 characters, and only contain letters, numbers, and hyphens."
        }
      }

๋ฐ˜์‘ํ˜•