Cloud/Terraform

Terraform - Launch Configuration과 Auto Scaling Group 구성 및 작동 확인

잇(IT) 2022. 7. 10. 11:13

resource "aws_launch_configuration" "as_conf" {
  name_prefix   = "terraform-lc-example-"
  image_id      = data.aws_ami.ubuntu.id
  instance_type = "t2.micro"

  lifecycle {
    create_before_destroy = true
  }
}

 

resource "aws_placement_group" "test" {
  name     = "test"
  strategy = "cluster"
}

resource "aws_autoscaling_group" "bar" {
  name                      = "foobar3-terraform-test"
  max_size                  = 5
  min_size                  = 2
  health_check_grace_period = 300
  health_check_type         = "ELB"
  desired_capacity          = 4
  force_delete              = true
  placement_group           = aws_placement_group.test.id
  launch_configuration      = aws_launch_configuration.foobar.name
  vpc_zone_identifier       = [aws_subnet.example1.id, aws_subnet.example2.id]

  initial_lifecycle_hook {
    name                 = "foobar"
    default_result       = "CONTINUE"
    heartbeat_timeout    = 2000
    lifecycle_transition = "autoscaling:EC2_INSTANCE_LAUNCHING"

    notification_metadata = <<EOF
{
  "foo": "bar"
}
EOF

    notification_target_arn = "arn:aws:sqs:us-east-1:444455556666:queue1*"
    role_arn                = "arn:aws:iam::123456789012:role/S3Access"
  }

  tag {
    key                 = "foo"
    value               = "bar"
    propagate_at_launch = true
  }

  timeouts {
    delete = "15m"
  }

  tag {
    key                 = "lorem"
    value               = "ipsum"
    propagate_at_launch = false
  }
}

- asg.tf 전체 코드

# 가장 최신의 아마존 리눅스 이미지 파일을 받아올 것이라는 의미다.

data "aws_ami" "amzn2" {
  most_recent = true

  filter {
    name   = "name"
    values = ["amzn2-ami-hvm-2.0.????????.?-x86_64-gp2"]
  }

  filter {
    name   = "virtualization-type"
    values = ["hvm"]
  }

  owners = ["amazon"] # Canonical
}

# SG 생성

resource "aws_security_group" "tf-asg-sg" {
  name        = "tf-asg-sg"
  description = "Allow web-asg inbound traffic"
  vpc_id      = aws_vpc.vpc-10-10-0-0.id

  ingress {
    description      = "tf-asg-sg from VPC"
    from_port        = 0
    to_port          = 0
    protocol         = "-1"
    cidr_blocks      = ["0.0.0.0/0"]
    ipv6_cidr_blocks = ["::/0"]
  }

  egress {
    from_port        = 0
    to_port          = 0
    protocol         = "-1"
    cidr_blocks      = ["0.0.0.0/0"]
    ipv6_cidr_blocks = ["::/0"]
  }

  tags = {
    Name = "tf-asg-sg"
  }
}

# alb 생성성

resource "aws_security_group" "tf-asg-alb-sg" {
  name        = "tf-asg-alb-sg"
  description = "Allow alb inbound traffic"
  vpc_id      = aws_vpc.vpc-10-10-0-0.id

  ingress {
    description      = "tf-asg-alb-sg from VPC"
    from_port        = 0
    to_port          = 0
    protocol         = "-1"
    cidr_blocks      = ["0.0.0.0/0"]
    ipv6_cidr_blocks = ["::/0"]
  }

  egress {
    from_port        = 0
    to_port          = 0
    protocol         = "-1"
    cidr_blocks      = ["0.0.0.0/0"]
    ipv6_cidr_blocks = ["::/0"]
  }

  tags = {
    Name = "tf-asg-alb-sg"
  }
}

resource "aws_lb" "tf-asg-alb" {
  name               = "tf-asg-alb"
  internal           = false
  load_balancer_type = "application"
  security_groups    = [aws_security_group.tf-asg-alb-sg.id]
  subnets            = [aws_subnet.sub-pub1-10-10-1-0.id, aws_subnet.sub-pub2-10-10-2-0.id]

  enable_deletion_protection = false

  tags = {
    Name = "tf-asg-alb"
  }
}

resource "aws_lb_target_group" "tf-asg-alb-tg" {
  name     = "tf-asg-alb-tg"
  port     = 80
  protocol = "HTTP"
  vpc_id   = aws_vpc.vpc-10-10-0-0.id
  
  health_check {
        enabled             = true
        healthy_threshold   = 3
        interval            = 5
        matcher             = "200"
        path                = "/"
        port                = "traffic-port"
        protocol            = "HTTP"
        timeout             = 2
        unhealthy_threshold = 2
    }
}

resource "aws_lb_listener" "tf-asg-alb-ln" {
  load_balancer_arn = aws_lb.tf-asg-alb.arn
  port              = "80"
  protocol          = "HTTP"

  default_action {
    type             = "forward"
    target_group_arn = aws_lb_target_group.tf-asg-alb-tg.arn
  }
}

# alb와 as에 attachment를 같이 사용할 수 없다.

resource "aws_launch_configuration" "as_conf" {
  name_prefix   = "terraform-lc-example-"
  # 자동으로 유니크한 이름을 생성해준다.
  image_id      = data.aws_ami.amzn2.id
  # 각자 생성한 이미지가 있다면 해당 이미지를 넣으면 된다.
  instance_type = "t2.micro"
  iam_instance_profile = "isbaek-role"
  security_groups = [aws_security_group.tf-asg-sg.id]
  key_name = "AWS_ISBAEK"
  user_data = file("./userdata.sh")

  lifecycle {
    create_before_destroy = true
  }
}

# auto scaling group 생성

resource "aws_placement_group" "test" {
  name     = "test"
  strategy = "cluster"
}

resource "aws_autoscaling_group" "tf-asg" {
  name                      = "terraform-asg-example"
  max_size                  = 4
  min_size                  = 2
  health_check_grace_period = 5
  health_check_type         = "EC2"
  desired_capacity          = 2
  force_delete              = true
  launch_configuration      = aws_launch_configuration.as_conf.name
  vpc_zone_identifier       = [aws_subnet.sub-pri1-10-10-3-0.id, aws_subnet.sub-pri2-10-10-4-0.id]

  tag {
    key                 = "Name"
    value               = "tf-asg"
    propagate_at_launch = false
  }
}

728x90