Ssoon

[T101] 02 왜 Terraform 인가? - (5) 구성 가능한 웹 서버 배포 본문

Terraform 101 Study

[T101] 02 왜 Terraform 인가? - (5) 구성 가능한 웹 서버 배포

구구달스 2022. 10. 18. 20:13
CloudNet@ 팀의 가시다님이 진행하는 Terraform 101 Study 1주차 정리입니다.

1. variable.tf - 입력변수 정의

variable "NAME" {

  [CONFIG...]

}

variable "instance_type" {
  description = "instance_type"
  type = string
  default = "t3.micro"  
}
variable "keypair" {
  description = "keypair"
  type = string
  default = "[TEST] keypair"
}
variable "server_port" {
  description = "server_port"
  type = number
  default = 50000
}
variable "ssh_port" {
  description = "ssh_port"
  type = number
  default = 22
}

2. output.tf - 출력변수 정의

output "NAME" {

  value = <VALUE>

  [CONFIG ...]

}

output "public_ip" {
  value = aws_instance.example.public_ip
  description = "public ip of ssoon server"
}

 

★ Data Sources

data "<PROVIDER>_<TYPE>" "<NANE>" {

  [CONFIG ...]

}

  • 데이터 소스를 통해 Terraform은 Terraform 외부에서 정의되거나 다른 별도의 Terraform 구성에 의해 정의되거나 기능에 의해 수정된 정보를 사용할 수 있습니다.
  • PROVIDER : AWS .. 공급자
  • TYPE : 사용하려는 data source 의 유형
  • NAME : terraform 코드에서 이 data source 를 참조하는 데 사용할 수 있는 식별자

data.tf - 최신 ami 를 사용하는 data source 정의

data "aws_ami" "amzlinux2" {
  most_recent = true
  owners = [ "amazon" ]
  filter {
    name = "name"
    values = [ "amzn2-ami-hvm-*-gp2" ]
  }
  filter {
    name = "root-device-type"
    values = [ "ebs" ]
  }
  filter {
    name = "virtualization-type"
    values = [ "hvm" ]
  }
  filter {
    name = "architecture"
    values = [ "x86_64" ]
  }
}

 

3. main.tf - variable 입력변수 / data source 참조

var.<VARIABLE_NAME>

  • 8008 서버 포트를 var.server_port 로 참조
  • 22 ssh 포트를 var.ssh_port 로 참조

data.<PROVIDER>_<TYPE>.<NAME>.<ATTRIBUTE>

  • data source 에서 data 를 가져오는 속성 참조 구문입니다.
provider "aws" {
  region = "ap-northeast-2"
}

resource "aws_instance" "example" {
  ami                    = data.aws_ami.amzlinux2.id
  instance_type          = var.instance_type
  key_name               = var.keypair
  vpc_security_group_ids = [aws_security_group.instance.id]
  
  user_data              = <<-EOF
  #!/bin/bash
  yum install docker -y
  systemctl enable --now docker
  mkdir -p /var/www
  echo "<h1>Welcome to Hell! - Ssoon</h1>" >> /var/www/index.html
  docker run --name nginx \
  -v /var/www:/usr/share/nginx/html \
  -d \
  -p ${var.server_port}:80 \
  nginx
  EOF

  tags = {
    "Name" = "ssoon-webserver"
  }
}

resource "aws_security_group" "instance" {
  name = "ssoon-webserver-SG"

  ingress {
    cidr_blocks = ["0.0.0.0/0"]
    from_port   = var.server_port
    to_port     = var.server_port
    protocol    = "tcp"
  }
  ingress {
    cidr_blocks = ["0.0.0.0/0"]
    from_port   = var.ssh_port
    to_port     = var.ssh_port
    protocol    = "tcp"
  }
  egress {
    cidr_blocks = ["0.0.0.0/0"]
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
  }
}

4. terraform plan -> terraform apply

  • 앞서 정의한 output 값을 확인할 수 있습니다.

5. Console 결과 확인

6. ssh 접속 후 확인

  • Terraform 으로 생성된 Instance 에서 user_data 로 생성된 Container 및 변경된 index.html을 확인할 수 있습니다.

Comments