상세 컨텐츠

본문 제목

[테라폼/Terraform]테라폼 변수

Development/테라폼

by J-Developer 2022. 5. 31. 14:35

본문

반응형

▶ 구성 가능한 웹 서버 배포

 

이전 단일 웹 서버 배포 시 데이터 구성을 보면 from_port, to_port에 8080포트가 두번 들어가는 것을 볼 수 있습니다. (AWS 기준)

이렇게 중복되는 코드는 변수를 선언하여 관리할 수 있습니다.

 

variable "NAME" {
	description = "<description>"
	default = <default>
	type = <type>
}

 

- description(Option) : 변수를 설명하는 변수입니다.

- default(Option) : 변수에 값을 전달하는 여러가지 방법이 있는데 -var(명령줄), -var-file(파일), ‘TF_VAR_<variable_name>’(환경변수)를 통해 값을 전달할 수 있습니다. 만약 값이 전달되지 않으면 기본값을 할당합니다. 기본값이 없는 경우 테라폼은 사용자에게 변수에 대한 정보를 묻습니다.

- type(Option) : 변수의 유형을 지정할 수 있습니다. string(문자열), number(숫자), bool(불리언), list(리스트), map(맵), set(집합), object(객체), tuple(튜플)등의 유형이 있습니다. 유형을 지정하지 않으면 any로 간주합니다.

 

 

- ex) 변수 선언 예시

variable "number_example" {
  description = "An example of a number variable in Terraform"
  type        = number
  default     = 42
}

variable "list_example" {
  description = "An example of a list in Terraform"
  type        = list
  default     = ["a", "b", "c"]
}

variable "list_numeric_example" {
  description = "An example of a numeric list in Terraform"
  type        = list(number)
  default     = [1, 2, 3]
}

variable "map_example" {
  description = "An example of a map in Terraform"
  type        = map(string)

  default = {
    key1 = "value1"
    key2 = "value2"
    key3 = "value3"
  }
}

variable "object_example" {
  description = "An example of a structural type in Terraform"
  type        = object({
    name    = string
    age     = number
    tags    = list(string)
    enabled = bool
  })

  default = {
    name    = "value1"
    age     = 42
    tags    = ["a", "b", "c"]
    enabled = true
  }
}

 

만약 변수에 유형이 일치하지 않는다면 테라폼은 오류를 표시합니다.

 

- ex) 변수에 값 전달 예시

#명령줄
$ terraform plan -var "server_port=8080"

#환경변수
$ export TF_VAR_server_port=8080
$ terraform plan

 

해당 변수를 참조시에는 아래와 같이 두가지 방법으로 참조 할 수 있습니다.

allow {
    protocol = "tcp"
    ports    = [var.server_port]
}

metadata_startup_script = <<-EOF
      #!/bin/bash
      echo "Hello, World" > index.html
      nohup busybox httpd -f -p ${var.server_port} &
      EOF

 

테라폼은 입력 변수뿐만 아니라 출력 변수도 정의할 수 있습니다.

output "NAME" {
	value = "<value>"
	description = <description>
	senstive = <senstive>
}

 

- value : 출력하려는 값

- description(Option) : 출력 변수를 설명하는 변수

- senstive(Option) : 변수 출력 실행이 끝날때 기록하지 않도록 하려면 senstive를 true로 설정해야합니다. 출력 변수에 개인정보 등 민감한 데이터가 있을 시 유용합니다.

 

ex) 출력 변수 예시

output "public_ip" {
  value       = aws_instance.example.public_ip
  description = "The public IP address of the web server"
}

 

apply 명령어를 실행하면 콘솔에 출력 변수가 표시가 됩니다.

예를 들어 위의 코드를 실행 시 웹 서버가 배포 된 후 테스트할 IP를 알려줄 수 있습니다.

 

더보기

※ terraform output

output 명령어를 통해 변경사항을 적용하지 않고 결과값을 확인할 수도 있습니다.

terraform output으로 모든 출력 변수값을 확인도 가능하지만 terraform output <OUTPUT_NAME>을 실행하여 특정 변수의 값을 확인할 수도 있습니다.

 

참조 : https://book.naver.com/bookdb/book_detail.naver?bid=20489970
 

테라폼 업앤러닝

이 책은 예제 소개를 뛰어넘어 실제 환경에서 테라폼을 사용하는 방법에 중점을 두고 만들어졌다. 외국어에 능통해지려면 원어민과 대화하고, 외국어 TV 쇼를 보고, 외국 음악을 듣는데 시간을

book.naver.com

 

반응형

관련글 더보기

댓글 영역