Skip to content

Commit

Permalink
Variables - min and max task count for autoscaling (#10)
Browse files Browse the repository at this point in the history
* min and max task count for autoscaling

* additional scaling parameters

* removed duplicated parameters
  • Loading branch information
VladVolchkov authored and ToROxI committed Jul 8, 2019
1 parent 3f599c3 commit 0a11800
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 19 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module "ecs" {
alb_target_group_arn = "arn:aws:elasticloadbalancing:< region >:< account ID >:targetgroup/< target group name >/3b4e9fbf82439af5"
container_port = "80"
availability_zones = "${var.availability_zones}"
container_definitions = <<EOF
Expand Down Expand Up @@ -53,7 +53,6 @@ EOF
| container\_port | exposed port in container | string | `80` | no |
| ecs\_cluster\_id | ID of existing ECS cluster (if want to attach service and etc to existing cluster) | string | `none` | no |
| environment | Environment name is used to identify resources | string | `env` | no |
| minimum\_service\_capacity | The number of instances of the task definition to place and keep running | string | `1` | no |
| health\_check\_grace\_period\_seconds | Seconds to ignore failing load balancer health checks on newly instantiated tasks | string | `30` | no |
| project | Project name is used to identify resources | string | `test` | no |
| service | Service name (will be used as family name in task definition) | string | `SuperService` | no |
Expand All @@ -65,6 +64,10 @@ EOF
| launch\_type | Launch type for ECS (FARGATE or EC2 ) | string | `FARGATE` | no |
| volume\_type | Volume type for EC2 | string | `standard` | no |
| volume\_size | Volume size for EC2 | string | `100` | no |
| autoscaling\_min\_capacity | Amount of min running task or EC2 instances | string | `1` | no |
| autoscaling\_max\_capacity | Amount of max running task or EC2 instances | string | `10` | no |
| autoscaling\_cpu\_high\_threshold | Autoscaling CPU threshold for scale-up | string | `40` | no |
| autoscaling\_cpu\_low\_threshold | Autoscaling CPU threshold for scale-down | string | `40` | no |
| availability\_zones | List of availability zones which will be provisined by autoscailing group | list | `[]` | yes |
| vpc\_id | The ID of VPC | string | - | yes |

Expand Down Expand Up @@ -109,4 +112,4 @@ EOF

## License

Apache2.0 Licensed. See [LICENSE](https://github.com/lean-delivery/tf-module-aws-ecs/tree/master/LICENSE) for full details.
Apache2.0 Licensed. See [LICENSE](https://github.com/lean-delivery/tf-module-aws-ecs/tree/master/LICENSE) for full details.
18 changes: 9 additions & 9 deletions autoscaling.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ resource "aws_cloudwatch_metric_alarm" "cpu-high" {
evaluation_periods = "1"
namespace = "AWS/ECS"
period = "60"
threshold = "50"
threshold = "${var.autoscaling_cpu_high_threshold}"

dimensions {
ClusterName = "${local.ecs_cluster_name}"
Expand All @@ -26,7 +26,7 @@ resource "aws_cloudwatch_metric_alarm" "cpu-low" {
evaluation_periods = "2"
namespace = "AWS/ECS"
period = "60"
threshold = "40"
threshold = "${var.autoscaling_cpu_low_threshold}"

dimensions {
ClusterName = "${local.ecs_cluster_name}"
Expand All @@ -45,7 +45,7 @@ resource "aws_cloudwatch_metric_alarm" "cpu-high_ec2" {
evaluation_periods = "1"
namespace = "AWS/EC2"
period = "60"
threshold = "40"
threshold = "${var.autoscaling_cpu_high_threshold}"

dimensions {
AutoScalingGroupName = "${aws_autoscaling_group.autoscaling-group.name}"
Expand All @@ -64,7 +64,7 @@ resource "aws_cloudwatch_metric_alarm" "cpu-low_ec2" {
evaluation_periods = "2"
namespace = "AWS/EC2"
period = "60"
threshold = "40"
threshold = "${var.autoscaling_cpu_low_threshold}"

dimensions {
AutoScalingGroupName = "${aws_autoscaling_group.autoscaling-group.name}"
Expand Down Expand Up @@ -117,8 +117,8 @@ resource "aws_appautoscaling_policy" "scale_policy_low" {
}

resource "aws_appautoscaling_target" "ecs_target" {
max_capacity = 10
min_capacity = 1
max_capacity = "${var.autoscaling_max_capacity}"
min_capacity = "${var.autoscaling_min_capacity}"
resource_id = "service/${local.ecs_cluster_name}/${aws_ecs_service.this.name}"

### https://docs.aws.amazon.com/autoscaling/application/userguide/application-auto-scaling-service-linked-roles.html
Expand Down Expand Up @@ -148,9 +148,9 @@ resource "aws_autoscaling_policy" "scale_policy_low_ec2" {

resource "aws_autoscaling_group" "autoscaling-group" {
name = "${var.service}-autoscaling-group"
max_size = "3"
min_size = "1"
desired_capacity = "1"
max_size = "${var.autoscaling_max_capacity}"
min_size = "${var.autoscaling_min_capacity}"
desired_capacity = "${var.autoscaling_min_capacity}"

availability_zones = ["${var.availability_zones}"]
vpc_zone_identifier = ["${var.subnets}"]
Expand Down
2 changes: 1 addition & 1 deletion ecs-cluster.tf
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ resource "aws_ecs_service" "this" {
deployment_maximum_percent = "200"
deployment_minimum_healthy_percent = "100"

desired_count = "${var.minimum_service_capacity}"
desired_count = "${var.autoscaling_min_capacity}"
health_check_grace_period_seconds = "${var.health_check_grace_period_seconds}"

network_configuration {
Expand Down
27 changes: 21 additions & 6 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@ variable "container_memory" {
default = "1024"
}

variable "minimum_service_capacity" {
description = "The number of instances of the task definition to place and keep running"
default = "1"
}

variable "health_check_grace_period_seconds" {
description = "Seconds to ignore failing load balancer health checks on newly instantiated tasks to prevent premature shutdown"
default = "30"
Expand Down Expand Up @@ -111,7 +106,7 @@ variable "key-pair-name" {
}

variable "instance_type" {
description = "ARN of target group"
description = "EC2 instance type"
type = "string"
default = "t2.small"
}
Expand All @@ -137,3 +132,23 @@ variable "availability_zones" {
description = "List of availability zones which will be provisined by autoscailing group"
type = "list"
}

variable "autoscaling_min_capacity" {
description = "Amount of min running task or EC2 instances"
default = "1"
}

variable "autoscaling_max_capacity" {
description = "Amount of max running task or EC2 instances"
default = "10"
}

variable "autoscaling_cpu_high_threshold" {
description = "Autoscaling CPU threshold for scale-up"
default = "50"
}

variable "autoscaling_cpu_low_threshold" {
description = "Autoscaling CPU threshold for scale-down"
default = "40"
}

0 comments on commit 0a11800

Please sign in to comment.