Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
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 31
Archives
Today
Total
관리 메뉴

하마롱크의 블로그

AWS에서 Terraform 기본 구성(3) 본문

Terraform

AWS에서 Terraform 기본 구성(3)

하마롱크 2021. 10. 30. 08:57

12. ALB 생성

resource "aws_lb" "test_lb" {
  name                   = "test-alb"
  internal               = false
  load_balancer_type     = "application"
  security_groups        = [aws_security_group.test_websg.id]
  subnets                = [aws_subnet.test_puba.id, aws_subnet.test_pubc.id]
  
  tags = {
    Name = "test-alb"
  }
}

 

13. Load Balancer Target Group 생성

resource "aws_lb_target_group" "test_lbtg" {
    name = "test-lbtg"
    port = 80
    protocol = "HTTP"
    vpc_id = aws_vpc.test_vpc.id  

    health_check {
      enabled = true
      healthy_threshold = 3
      interval = 5
      matcher = "200"
      path = "/health.html"
      port = "traffic-port"
      protocol = "HTTP"
      timeout = 2
      unhealthy_threshold = 2

    }
}

 

14. Load Balancer Lister 생성

resource "aws_lb_listener" "test_lblist" {
    load_balancer_arn = aws_lb.test_lb.arn
    port = 80
    protocol = "HTTP"

    default_action {
      type = "forward"
      target_group_arn = aws_lb_target_group.test_lbtg.arn

    }
}

 

15. Load Balancer Target 연결

resource "aws_lb_target_group_attachment" "test_lbtg_att" {
  target_group_arn = aws_lb_target_group.test_lbtg.arn
  target_id        = aws_instance.test_weba.id
  port             = 80
}

 

16. AMI 생성

resource "aws_ami_from_instance" "test_ami" {
    name = "test-ami"
    source_instance_id = aws_instance.test_weba.id
}

 

17. Launch Configuration 생성

resource "aws_launch_configuration" "test_lacf" { 
  name          = "test-web"
  image_id      = aws_ami_from_instance.test_ami.id
  instance_type = "t2.micro"
  iam_instance_profile = "admin-role"
  security_groups = [aws_security_group.test_websg.id]
  key_name = "tf-key"
  user_data =<<-EOF
              #!/bin/bahs
              systemctl start httpd
              systemctl enable httpd
              EOF
  lifecycle {
    create_before_destroy = true
  }
}

 

18. Auto Scaling Group 생성

resource "aws_placement_group" "test_pg" {
    name = "test-pg"
    strategy = "cluster"
}

resource "aws_autoscaling_group" "test_atsg" {
    name                      = "test-atrg"
    min_size                  = 2
    max_size                  = 8
    health_check_grace_period = 300
    health_check_type         = "ELB"
    desired_capacity          =  2
    force_delete              = true
    launch_configuration      = aws_launch_configuration.test_lacf.name
    vpc_zone_identifier       = [aws_subnet.test_puba.id,aws_subnet.test_pubc.id]
}

 

19. Auto Scaling 연결

resource "aws_autoscaling_attachment" "test-atatt" {
    autoscaling_group_name = aws_autoscaling_group.test_atsg.id
    alb_target_group_arn = aws_lb_target_group.test_lbtg.arn
    
}

resource "aws_autoscaling_group" "test_atsg" {
    name                      = "test-atrg"
    min_size                  = 2
    max_size                  = 8
    health_check_grace_period = 300
    health_check_type         = "ELB"
    desired_capacity          =  2
    force_delete              = true
    launch_configuration      = aws_launch_configuration.test_lacf.name
    vpc_zone_identifier       = [aws_subnet.test_puba.id,aws_subnet.test_pubc.id]
}

 

20. DB Subnet Group 생성 및 RDS(MySQL) 생성

resource "aws_db_subnet_group" "test_dbsn" {
    name = "test-db-group"
    subnet_ids = [aws_subnet.test_pria.id,aws_subnet.test_pric.id]
    tags = {
        Name = "test-dbsb-group"
    }
  }

resource "aws_db_instance" "test_wpdb" {
  allocated_storage = 10
  engine = "mysql"
  engine_version = "5.7"
  instance_class = "db.t2.micro"
  name = "wordpress"
  identifier = "wp-db"
  username = "admin"
  password = "It12345!"
  parameter_group_name = "default.mysql5.7"
  availability_zone = "ap-northeast-2a"
  db_subnet_group_name = aws_db_subnet_group.test_dbsn.id
  skip_final_snapshot = true
  tags = {
      Name = "test-wpdb"
  }
}

'Terraform' 카테고리의 다른 글

테라폼 모듈(Terraform Module)  (0) 2021.11.02
AWS에서 Terraform 기본 구성(2)  (0) 2021.10.29
AWS에서 Terraform 기본 구성(1)  (0) 2021.10.27
테라폼(Terraform)  (0) 2021.10.27