通过CLI将自动扩展策略附加到ECS服务

时间:2018-11-07 04:32:34

标签: amazon-web-services amazon-cloudwatch amazon-ecs aws-fargate

我有一个在Fargate部署的ECS上运行的服务。我正在使用ecs-cli compose启动此服务。这是我当前使用的命令:

ecs-cli compose service up --cluster my_cluster —-launch-type FARGATE

我还有一个ecs-params.yml来配置此服务。这是内容:

version: 1
task_definition:
  task_execution_role: ecsTaskExecutionRole
  task_role_arn: arn:aws:iam::XXXXXX:role/MyExecutionRole 
  ecs_network_mode: awsvpc
  task_size:
    mem_limit: 2GB
    cpu_limit: 1024
run_params:
  network_configuration:
    awsvpc_configuration:
      subnets:
        - "subnet-XXXXXXXXXXXXXXXXX"
        - "subnet-XXXXXXXXXXXXXXXXX"
      security_groups:
        - "sg-XXXXXXXXXXXXXX"
      assign_public_ip: ENABLED

创建服务后,我必须登录AWS控制台并通过AWS GUI附加自动缩放策略。通过CLI或在我的YAML配置中,是否有更简单的方法来附加自动缩放策略?

1 个答案:

答案 0 :(得分:0)

虽然您可以使用AWS CLI本身(请参阅文档中的application-autoscaling

我认为在一个部署中执行整个操作会更好,为此,您拥有Terraform之类的工具。

您可以使用Github的 arminc 编写的terraform-ecs module,也可以自己使用!这是整个集群的一个快速(而且确实很肮脏)的示例,但是如果您不想将整个部署放在一个地方,也可以抓住自动伸缩部分并使用它:

provider "aws" {
  region = "us-east-1" # insert your own region
  profile = "insert aw cli profile, should be located in ~/.aws/credentials file"
  # you can also use your aws credentials instead
  # access_key = "insert_access_key"
  # secret_key = "insert_secret_key"
}


resource "aws_ecs_cluster" "cluster" {
  name = "my-cluster"
}

resource "aws_ecs_service" "service" {
  name = "my-service"
  cluster = "${aws_ecs_cluster.cluster.id}"

  task_definition = "${aws_ecs_task_definition.task_definition.family}:${aws_ecs_task_definition.task_definition.revision}"

  network_configuration {
    # These can also be created with Terraform and applied dynamically instead of hard-coded
    # look it up in the Docs

    security_groups = ["SG_IDS"]
    subnets         = ["SUBNET_IDS"] # can also be created with Terraform
    assign_public_ip = true
  }
}

resource "aws_ecs_task_definition" "task_definition" {
  family = "my-service"
  execution_role_arn = "ecsTaskExecutionRole"
  task_role_arn = "INSERT_ARN"
  network_mode = "awsvpc"
  container_definitions = <<DEFINITION
[
  {
    "name": "my_service"
    "cpu": 1024,
    "environment": [{
      "name": "exaple_ENV_VAR",
      "value": "EXAMPLE_VALUE"
    }],
    "essential": true,
    "image": "INSERT IMAGE URL",
    "memory": 2048,
    "networkMode": "awsvpc"
  }
]
DEFINITION
}

#
# Application AutoScaling resources
#
resource "aws_appautoscaling_target" "main" {
  service_namespace  = "ecs"
  resource_id    = "service/${var.cluster_name}/${aws_ecs_service.service.name}"
  scalable_dimension = "ecs:service:DesiredCount"
  # Insert Min and Max capacity here
  min_capacity       = "1"
  max_capacity       = "4"

  depends_on = [
    "aws_ecs_service.main",
  ]
}

resource "aws_appautoscaling_policy" "up" {
  name               = "scaling_policy-${aws_ecs_service.service.name}-up"
  service_namespace  = "ecs"
  resource_id        = "service/${aws_ecs_cluster.cluster.name}/${aws_ecs_service.service.name}"
  scalable_dimension = "ecs:service:DesiredCount"

  step_scaling_policy_configuration {
    adjustment_type         = "ChangeInCapacity"
    cooldown                = "60" # In seconds
    metric_aggregation_type = "Average"

    step_adjustment {
      metric_interval_lower_bound = 0
      scaling_adjustment          = 1 # you can also use negative numbers for scaling down
    }
  }

  depends_on = [
    "aws_appautoscaling_target.main",
  ]
}

resource "aws_appautoscaling_policy" "down" {
  name               = "scaling_policy-${aws_ecs_service.service.name}-down"
  service_namespace  = "ecs"
  resource_id        = "service/${aws_ecs_cluster.cluster.name}/${aws_ecs_service.service.name}"
  scalable_dimension = "ecs:service:DesiredCount"

  step_scaling_policy_configuration {
    adjustment_type         = "ChangeInCapacity"
    cooldown                = "60" # In seconds
    metric_aggregation_type = "Average"

    step_adjustment {
      metric_interval_upper_bound = 0
      scaling_adjustment          = -1 # scale down example
    }
  }

  depends_on = [
    "aws_appautoscaling_target.main",
  ]
}