Terraform으로 Auto Scaling Group 배포

변재한·2023년 4월 12일
0
post-thumbnail

Terraform으로 Auto Scaling Group 배포

먼저, ASG를 배포하기 위한 사전 준비사항은 아래와 같다.

VPC, 서브넷, 라우팅 테이블 구성

# vpc 생성
resource "aws_vpc" "terraform-vpc" {
  cidr_block = "10.0.0.0/16"

  tags = {
    name = "terraform-vpc"
  }
}

# 서브넷 생성
resource "aws_subnet" "terraform-subnet1" {
    vpc_id = aws_vpc.terraform-vpc.id
    cidr_block = "10.0.1.0/24"
    availability_zone = "ap-northeast-2a"

    tags = {
      name = "terraform-subnet1"
    }
}

resource "aws_subnet" "terraform-subnet2" {
  vpc_id = aws_vpc.terraform-vpc.id
  cidr_block = "10.0.2.0/24"
  availability_zone = "ap-northeast-2c"

  tags = {
    name = "terraform-subnet2"
  }
}

# 인터넷 게이트웨이 생성
resource "aws_internet_gateway" "terraform-igw" {
  vpc_id = aws_vpc.terraform-vpc.id
  tags = {
    name = "terraform-igw"
  }
}

# 라우팅 테이블 생성
resource "aws_route_table" "terraform-route-table" {
  vpc_id = aws_vpc.terraform-vpc.id

  route {
    cidr_block = "0.0.0.0/0"
    gateway_id = aws_internet_gateway.terraform-igw.id
  }

  tags = {
    name = "terraform-route-table"
  }
}

# 라우팅 테이블과 서브넷 연결
resource "aws_route_table_association" "subent1-association" {
  subnet_id = aws_subnet.terraform-subnet1.id
  route_table_id = aws_route_table.terraform-route-table.id
}

resource "aws_route_table_association" "subent2-association" {
  subnet_id = aws_subnet.terraform-subnet2.id
  route_table_id = aws_route_table.terraform-route-table.id
}

또한, 리소스 전부를 한 개의 tf 확장자 파일에 담으려 하니 코드가 난잡해져서

리소스를 어느정도 Categorizing하여 파일을 아래와 같이 분류하였다.

provider.tf

환경변수로 일일이 Key를 설정하기가 번거로워 aws cli profile 설정을 하였다.

provider "aws" {
    profile = "example"
    region = "ap-northeast-2"
}

asg.tf

대시보드에서 asg를 생성할 때 시작 구성을 만들고 시작하는 프로세스와 똑같이

먼저, 시작 구성 리소스를 만듦.

resource "aws_launch_configuration" "terraform-launconfig" {
  image_id = data.aws_ami.ubuntu.id
  instance_type = "t2.micro"
  security_groups = [aws_security_group.terraform-sg.id]

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

  # Auto Scaling Group에서 시작 구성을 사용할 때 필요
  lifecycle {
    create_before_destroy = true
  }
}

resource "aws_autoscaling_group" "terraform-asg" {
    launch_configuration = aws_launch_configuration.terraform-launconfig.name
    vpc_zone_identifier = [aws_subnet.terraform-subnet1.id, aws_subnet.terraform-subnet2.id]

    min_size = 2
    max_size = 5

    tag {
        key = "name"
        value = "terraform-asg"
        propagate_at_launch = true
    }
}

data.tf

asg의 인스턴스가 ubuntu 22.04의 최신 릴리즈를 사용할 수 있게 aws ami ubuntu 22.04 최신 릴리즈를 쿼리하여 가져올 수 있게 하였다.

data "aws_ami" "ubuntu" {
    most_recent = true
    
    filter {
        name   = "name"
        values = ["ubuntu/images/hvm-ssd/ubuntu-*-22.04-amd64-server-*"]
    }
    
    filter {
        name   = "owner-alias"
        values = ["amazon"]
    }
}

output "latest_ami_id" {
    value = data.aws_ami.ubuntu.id
    description = "The AMI ID latest ubuntu 22.04 verison"
}

sg.tf

resource "aws_security_group" "terraform-sg" {
  name = "terraform-sg"
  vpc_id = aws_vpc.terraform-vpc.id

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

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

대시보드

profile
Infra and Devops 엔지니어가 되고 싶어용

0개의 댓글