티스토리 뷰

반응형

입력 변수

Terraform 코드를 작성하다면 아래와 같이 중복된 내용을 여러번 작성하는 경우가 발생한다.

resource "aws_security_group" "instance" {
    name = "ch4njun-example-instance"

    ingress {
        from_port = 8080
        to_port = 8080
        protocol = "tcp"
        cidr_blocks = ["0.0.0.0/0"]
    }
}

resource "aws_instance" "example" {
    ami = "ami-40d28157"
    instance_type = "t2.micro"
    vpc_security_group_ids = ["${aws_security_group.instance.id}"]

    user_data = <<-EOF
                #!/bin/bash
                echo "Hello, World" > index.html
                nohup busybox httpd -f -p 8080 &
                EOF
    tags = {
        Name = "ch4njun-example1"
    }
}

 

포트번호 8080에 대해서 중복 배제원칙(DRY, Don't Repeat Yourself)에 어긋나게 작성되었다. 이럴 경우 입력변수를 정의해 사용할 수 있다.

 

variable "NAME" {	
	[CONFIG ...]
}

CONFIG 자리에는 아래 세 개의 매개변수를 추가할 수 있다.

 

description : 변수 사용방법을 작성한다.

default : 변수에 값이 제공되지 않는다면 이 값을 사용한다.

            변수에 값이 제공되지 않는데 이 값까지 없다면 테라폼에서 사용자 입력을 받도록 화면에 뜬다.

type : 문자열, 리스트, 맵(map) 값 중 하나여야 한다.

         지정하지 않을 경우 기본값의 변수 속성을 선택할 것이고, default도 없으면 문자열을 선택한다.

 

variable "list_example" {
    description = "An example of a list in Terraform"
    type = "list"
    default = [1, 2, 3]
}

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

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

 

만약 아래 코드와 같이 default를 작성하지 않는다면 terraform plan 명령어를 수행할 때 아래와 같이 사용자에게 입력할 수 있도록 출력한다.

variable "server_port" {
    description = "The port the server will use for HTTP requests"
}

 

아니면 이와같이 명령어 줄 옵션 -var를 사용해 입력 변수에 값을 넣어줄 수 있다.

 

하지만 매번 plan, apply를 할 때마다 넣어주는 것이 번거롭고 까먹을 수 있기 때문에 입력 변수 default 매개변수를 추가하는 것이 효율적이다.

 

그렇다면 이렇게 생성한 변수는 어떻게 사용할까?

variable "server_port" {
    description = "The port the server will use for HTTP requests"
}

resource "aws_security_group" "instance" {
    name = "ch4njun-example-instance"

    ingress {
        from_port = "${var.server_port}"
        to_port = "${var.server_port}"
        protocol = "tcp"
        cidr_blocks = ["0.0.0.0/0"]
    }
}

resource "aws_instance" "example" {
    ami = "ami-40d28157"
    instance_type = "t2.micro"
    vpc_security_group_ids = ["${aws_security_group.instance.id}"]

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

 

위 코드에서 볼 수 있지만 "${var.[변수명]}" 의 형태로 선언된 입력 변수를 사용할 수 있다.

 


출력 변수

출력 변수는 apply 명령어를 수행하면 가장 마지막에 출력을 희망하는 것을 말한다. 예를 들면 apply 명령어를 모두 완료한 후 public_ip를 콘솔창에 띄워주고 싶은 경우 출력 변수를 사용하게 된다.

 

output "NAME" {
	value = VALUE
}

이런 형태로 출력 변수를 설정할 수 있다. 위 예시 상황을 실제로 작성해보면 아래와 같다.

 

output "public_ip" {
    value = "${aws_instance.example.public_ip}"
}

variable "server_port" {
    description = "The port the server will use for HTTP requests"
}

resource "aws_security_group" "instance" {
    name = "ch4njun-example-instance"

    ingress {
        from_port = "${var.server_port}"
        to_port = "${var.server_port}"
        protocol = "tcp"
        cidr_blocks = ["0.0.0.0/0"]
    }
}

resource "aws_instance" "example" {
    ami = "ami-40d28157"
    instance_type = "t2.micro"
    vpc_security_group_ids = ["${aws_security_group.instance.id}"]

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

이와 같이 출력되는 것을 확인할 수 있다.

반응형
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함