我无法使用 Terraform 将自动缩放组分配给 ECS 集群

I'm not able to assign autoscaling group to ECS cluster using Terraform

我正在尝试创建一个带有服务的 ECS 集群,但我无法设置自动缩放,因此集群中没有启动任何实例:

service my_service was unable to place a task because no container instance met all of its requirements. Reason: No Container Instances were found in your cluster. For more information, see the Troubleshooting section.

这是我的 Terraform 配置(仅相关配置):

集群和服务

resource "aws_ecs_cluster" "my_cluster" {
  name = "my_cluster"
}

resource "aws_ecs_service" "my_service" {
  name            = "my_service"
  cluster         = "${aws_ecs_cluster.my_cluster.id}"
  task_definition = "${aws_ecs_task_definition.my_tf.arn}"
  desired_count   = 1
  iam_role        = "${aws_iam_role.ecs-service-role.id}"
}

自动缩放

resource "aws_launch_configuration" "launch_config" {
  name_prefix          = "my_lc"
  image_id             = "${data.aws_ami.ubuntu.id}"
  instance_type        = "t2.micro"
  user_data            = "${data.template_file.user_data.rendered}"
  security_groups      = ["${aws_security_group.my_sg.id}"]
  iam_instance_profile = "${aws_iam_instance_profile.ecs-instance-profile.id}"

  lifecycle {
    create_before_destroy = true
  }
}

resource "aws_autoscaling_group" "autoscaling_group" {
  name                 = "my_autoscaling_group"
  max_size             = 2
  min_size             = 1
  launch_configuration = "${aws_launch_configuration.launch_config.name}"
  vpc_zone_identifier  = ["${aws_subnet.public.id}"]
}


data "template_file" "user_data" {
  template = "${file("${path.module}/templates/user-data.sh")}"

  vars {
    cluster_name = "${aws_ecs_cluster.my_cluster.name}"
  }
}

templates/user-data.sh

#!/bin/bash

# ECS config
{
  echo "ECS_CLUSTER=${cluster_name}"
} >> /etc/ecs/ecs.config

start ecs

AMI

data "aws_ami" "ubuntu" {
  most_recent = true

  filter {
    name   = "name"
    values = ["ubuntu/images/hvm-ssd/ubuntu-trusty-14.04-amd64-server-*"]
  }

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

  owners = ["099720109477"]
}

据我所知,集群和自动缩放组是通过启动配置中的 user_data 链接的,但它似乎不起作用。

我错过了什么?

为了使其正常工作,您缺少 key 组件。 Cloudwatch 警报

Cloudwatch 将提醒自动缩放组在需要时采取行动。示例配置:

这将创建必要的警报以在机器未充分利用时缩小规模。通过变量调整您的需求。

resource "aws_cloudwatch_metric_alarm" "alarm-cpu-down" {
  alarm_name          = "ecs-down"
  comparison_operator = "LessThanOrEqualToThreshold"
  evaluation_periods  = "${var.evaluation_periods}"
  metric_name         = "${var.metric_name}"
  namespace           = "${var.namespace}"
  period              = "${var.period_down}"
  statistic           = "${var.statistic}"
  threshold           = "${var.threshold_down}"
  dimensions          = "${map(var.dimension_name, var.dimension_value == "false" ? var.autoscaling_group_name[count.index] : var.dimension_value)}"

  alarm_description = "This metric monitors CPU utilization down"
  alarm_actions     = ["${aws_autoscaling_policy.down-scale.*.arn}"]
}

这将创建必要的警报以在需要一台机器时进行扩展(CPUReservation(=您的服务需要的)大于可用的)

resource "aws_cloudwatch_metric_alarm" "alarm-cpu-up" {
  alarm_name          = "${var.environment}-${var.project}-${var.name}-${var.metric_name}-up${count.index}"
  comparison_operator = "GreaterThanOrEqualToThreshold"
  evaluation_periods  = "${var.evaluation_periods}"
  metric_name         = "${var.metric_name}"
  namespace           = "${var.namespace}"
  period              = "${var.period_up}"
  statistic           = "${var.statistic}"
  threshold           = "${var.threshold_up}"
  dimensions          = "${map(var.dimension_name, var.dimension_value == "false" ? var.autoscaling_group_name[count.index] : var.dimension_value)}"

  alarm_description = "This metric monitors CPU utilization up"
  alarm_actions     = ["${aws_autoscaling_policy.up-scale.*.arn}"]
}

这些是实际的自动缩放策略。这是触发警报时 cloudwatch 警报需要执行的操作。

resource "aws_autoscaling_policy" "up-scale" {   
    count = "${var.num_asg}"   
    name  = "${var.environment}-${var.project}-${var.name}-${var.metric_name}-up${count.index}" 
    autoscaling_group_name = "${var.autoscaling_group_name[count.index]}"  
    adjustment_type        = "${var.adjustment_type}"   
    policy_type            = "${var.policy_type}"   
    cooldown               = "${var.cooldown_up}"   
    scaling_adjustment     = "${var.adjustment_up}" 
}

resource "aws_autoscaling_policy" "down-scale" {   
    count = "${var.num_asg}"   
    name  = "${var.environment}-${var.project}-${var.name}-${var.metric_name}-down${count.index}" 
    autoscaling_group_name = "${var.autoscaling_group_name[count.index]}"  
    adjustment_type        = "${var.adjustment_type}"   
    policy_type            = "${var.policy_type}"   
    cooldown               = "${var.cooldown_down}"   
    scaling_adjustment     = "${var.adjustment_down}" 
}

您需要 ECS 优化 AMI 之一:https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-optimized_AMI.html

您需要创建自己的 AMI,这并不容易。