최신글

테라폼 시리즈 18편. auto scaling group생성

반응형

목표

테라폼으로 auto scaling group을 생성합니다.

 

요구사항

 

상세내용

auto scaling group생성 코드는 template 또는 configuration설정 코드가 많은 비중을 차지합니다.

 

테라폼에서도 auto scaling group생성은 간단합니다.

resource "aws_autoscaling_group" "example" {
  launch_configuration = aws_launch_configuration.example.name
  vpc_zone_identifier  = [aws_subnet.example.id, aws_subnet.example2.id]

  min_size = 1
  max_size = 5

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

 

테라폼 코드가 많이 있는 부분은 launch_configuration입니다. 옵션과 필수 필드가 있습니다. ami_id, instance_type, security group 필드가 필수입니다.

resource "aws_launch_configuration" "example" {
  image_id        = "ami-0e9bfdb247cc8de84"
  instance_type   = "t2.micro"
  security_groups = [aws_security_group.instance.id]
}

 

코드 작성

github 링크: https://github.com/sungwook-practice/terraform-study/tree/main/week2/auto_scaling_group

vpc.tf, sg.tf, asg.tf파일로 분리했습니다. ami는 data block을 사용했습니다.

 

- vpc.tf

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

resource "aws_vpc" "akbun-vpc" {
  cidr_block       = "10.10.0.0/16"
  enable_dns_support   = true
  enable_dns_hostnames = true

  tags = {
    Name = "t101-study"
  }
}

resource "aws_subnet" "akbun-subnet1" {
  vpc_id     = aws_vpc.akbun-vpc.id
  cidr_block = "10.10.1.0/24"

  availability_zone = "ap-northeast-2a"

  tags = {
    Name = "t101-subnet1"
  }
}

resource "aws_subnet" "akbun-subnet2" {
  vpc_id     = aws_vpc.akbun-vpc.id
  cidr_block = "10.10.2.0/24"

  availability_zone = "ap-northeast-2c"

  tags = {
    Name = "t101-subnet2"
  }
}

resource "aws_internet_gateway" "akbun-igw" {
  vpc_id = aws_vpc.akbun-vpc.id

  tags = {
    Name = "t101-igw"
  }
}

# route table 생성
resource "aws_route_table" "akbun-rt" {
  vpc_id = aws_vpc.akbun-vpc.id

  tags = {
    Name = "t101-rt"
  }
}

# route table과 subnet 연결
resource "aws_route_table_association" "akubun-rt-association1" {
  subnet_id      = aws_subnet.akbun-subnet1.id
  route_table_id = aws_route_table.akbun-rt.id
}

# route table과 subnet 연결
resource "aws_route_table_association" "akubun-rt-association2" {
  subnet_id      = aws_subnet.akbun-subnet2.id
  route_table_id = aws_route_table.akbun-rt.id
}

# route 규칙 추가
resource "aws_route" "mydefaultroute" {
  route_table_id         = aws_route_table.akbun-rt.id
  destination_cidr_block = "0.0.0.0/0"
  gateway_id             = aws_internet_gateway.akbun-igw.id
}

 

- sg.tf

resource "aws_security_group" "akbun-mysg" {
  vpc_id      = aws_vpc.akbun-vpc.id
  name        = "T101 SG"
  description = "T101 Study SG"
}

resource "aws_security_group_rule" "mysginbound" {
  type              = "ingress"
  from_port         = 0
  to_port           = 80
  protocol          = "tcp"
  cidr_blocks       = ["0.0.0.0/0"]
  security_group_id = aws_security_group.akbun-mysg.id
}

resource "aws_security_group_rule" "mysgoutbound" {
  type              = "egress"
  from_port         = 0
  to_port           = 0
  protocol          = "-1"
  cidr_blocks       = ["0.0.0.0/0"]
  security_group_id = aws_security_group.akbun-mysg.id
}

 

- asg.tf

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

  min_size = 1
  max_size = 4

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

resource "aws_launch_configuration" "akbun-launchconfig" {
  image_id        = data.aws_ami.my_amazonlinux2.id
  instance_type   = "t2.nano"
  security_groups = [aws_security_group.akbun-mysg.id]
  
  # Required when using a launch configuration with an auto scaling group.
  lifecycle {
    create_before_destroy = true
  }
}

 

- data.tf

data "aws_ami" "my_amazonlinux2" {
  most_recent = true
  filter {
    name   = "owner-alias"
    values = ["amazon"]
  }

  filter {
    name   = "name"
    values = ["amzn2-ami-hvm-*-x86_64-ebs"]
  }

  owners = ["amazon"]
}

 

코드 반영

terraform apply명령어로 테라폼 코드를 aws에 반영합니다.

terraform apply

 

Launch Configuration 대시보드를 방문하여 리소스가 잘 생성되었는지 확인합니다.

 

이제 auto scaling group이 잘 생성됬는지 확인해봅시다. auto scaling대시보드로 이동하여 생성된 리소스를 클릭합니다.

 

테라폼 코드대로 설정이 잘 되었는지 확인합니다.

 

Instance Management탭으로 이동하면 실행 중인 EC2 Instance목록을 볼 수 있습니다. Minimum capacity를 1로 설정했으므로 Instance가 1개 실행하고 있습니다. Instance는 EC2 대시보드에서도 확인할 수 있습니다.

 

아직은 EC2 Instance가 증감하는 조건을 설정하지 않았으므로 EC2 Instance는 항상 1개만 실행됩니다.

반응형