基于SQS深度的自动缩放组

问题描述 投票:0回答:1

我正在编写执行以下操作的 Terraform 代码:设置 S3 存储桶和 S3 事件通知,以及由 S3 通知触发的 Lambda。然后它将消息发送到 SQS。然后是一个基于 SQS 深度的自动缩放组(使用公式消息数/实例数),上限为 5 个实例(因此在 ASG 中运行的实例永远不会超过 5 个)。我为此创建了自动缩放策略,并了解到这种类型的策略可以自行触发并且不需要警报(?)。

terraform apply
运行成功,但随后我通过在 SQS 中发送消息在 AWS 中进行测试,但没有看到 ASG 中添加任何实例。做错了什么/遗漏了什么?

resource "aws_sqs_queue" "file_checker_sqs_queue" {
  name                      = var.sqs_queue_name
  max_message_size          = 256000
  message_retention_seconds = 86400
  visibility_timeout_seconds = 30
    receive_wait_time_seconds = 10
  }

 data "aws_availability_zones" "available" {
   state = "available"
   # Only retrieve the main AZs, no local zones.
   filter {
     name = "opt-in-status"
     values = ["opt-in-not-required"]
   }
 }

 data "aws_ami" "audio_ami" {
   most_recent = true

   filter {
     name   = "image-id"
     values = ["ami-067d1e60475437ey2"]
   }
 }

 resource "aws_launch_configuration" "audio_asg_launch_config" {
   name_prefix   = var.launch_config_name_prefix
   image_id      = data.aws_ami.audio_ami.id
   instance_type = "t2.micro"

   lifecycle {
     create_before_destroy = true
   }
 }

 resource "aws_autoscaling_group" "audio_auto_scaling_group" {
   availability_zones        = data.aws_availability_zones.available.names
   name                      = var.asg_name
   max_size                  = 5
   min_size                  = 0
   health_check_grace_period = 300
   health_check_type         = "EC2"
   force_delete              = true
   launch_configuration      = aws_launch_configuration.audio_asg_launch_config.name
 }

 resource "aws_autoscaling_policy" "asg_audio_scale_up_policy" {
   autoscaling_group_name = var.asg_name
   name                   = var.asg_policy_name
   policy_type            = "TargetTrackingScaling"
   depends_on             = [ aws_autoscaling_group.audio_auto_scaling_group ]
      
   target_tracking_configuration {
      target_value = 1
      customized_metric_specification {
        metrics {
          label = "Get the queue size (the number of messages waiting to be processed)"
          id    = "m1"
          metric_stat {
            metric {
              namespace   = "AWS/SQS"
              metric_name = "ApproximateNumberOfMessagesVisible"
              dimensions {
              name  = "QueueName"
              value = var.sqs_queue_name
           }
        }
        stat = "Sum"
      }
      return_data = false
    }
    metrics {
      label = "Get the group size (the number of InService instances)"
       id    = "m2"
       metric_stat {
         metric {
           namespace   = "AWS/AutoScaling"
           metric_name = "GroupInServiceInstances"
           dimensions {
             name  = "AutoScalingGroupName"
             value = var.asg_name
           }
         }
         stat = "Average"
       }
       return_data = false
     }
     metrics {
       label       = "Calculate the backlog per instance"
       id          = "e1"
       expression  = "IF(m2 == 0, m1, m1 / m2)"
       return_data = true
      }
    }
  }
}

更新:尝试解决评论,以下是我的代码的更新部分(在 SQS 中发送消息后,我的 ASG 中仍然没有任何反应):

resource "aws_launch_template" "audio_asg_launch_template" {
  name_prefix   = var.launch_config_name_prefix
  image_id      = data.aws_ami.audio_ami.id
  instance_type = "t2.micro"
}
resource "aws_autoscaling_group" "audio_auto_scaling_group" {
  availability_zones        = data.aws_availability_zones.available.names
  name                      = var.asg_name
  max_size                  = var.asg_max_size
  min_size                  = var.asg_min_size
  desired_capacity = 0
  health_check_grace_period = var.asg_health_check_grace_period
  health_check_type         = "EC2"
  force_delete              = true
  launch_template {
    id      = aws_launch_template.audio_asg_launch_template.id
    version = "$Latest"
  }
}
resource "aws_autoscaling_policy" "asg_audio_scale_up_policy" {
  autoscaling_group_name = aws_autoscaling_group.audio_auto_scaling_group.name
  name                   = var.asg_policy_name
  policy_type            = "TargetTrackingScaling"
  
  target_tracking_configuration {
    target_value = 1
    customized_metric_specification {
      metrics {
        label = "Get the queue size (the number of messages waiting to be processed)"
        id    = "m1"
        metric_stat {
          metric {
            namespace   = "AWS/SQS"
            metric_name = "ApproximateNumberOfMessagesVisible"
            dimensions {
              name  = "QueueName"
              value = aws_sqs_queue.file_checker_sqs_queue.name
            }
          }
          stat = "Sum"
        }
        return_data = false
      }
      metrics {
        label = "Get the group size (the number of InService instances)"
        id    = "m2"
        metric_stat {
          metric {
            namespace   = "AWS/AutoScaling"
            metric_name = "GroupInServiceInstances"
            dimensions {
              name  = "AutoScalingGroupName"
              value = aws_autoscaling_group.audio_auto_scaling_group.name
            }
          }
          stat = "Average"
        }
        return_data = false
      }
      metrics {
        label       = "Calculate the backlog per instance"
        id          = "e1"
        expression  = "IF(m2 == 0, m1, m1 / m2)"
        return_data = true
      }
    }
  }
}
amazon-web-services terraform amazon-sqs terraform-provider-aws aws-auto-scaling
1个回答
0
投票

想通了!策略本身一切正常,问题是 ASG 默认情况下不启用指标。一旦我将它包含到我的代码中 - 一切就开始工作了。因此,我的问题中的初始代码保持不变,我们仅添加以下内容:

resource "aws_autoscaling_group" "audio_auto_scaling_group" {
  # the rest of the code stays the same

  # Enabling metrics collection for AutoScaling Group
  enabled_metrics = ["GroupInServiceInstances"]
}

© www.soinside.com 2019 - 2024. All rights reserved.