diff --git a/addons/migrations/.header.md b/addons/migrations/.header.md index 4c32f8e..aec6d78 100644 --- a/addons/migrations/.header.md +++ b/addons/migrations/.header.md @@ -19,6 +19,7 @@ module "fleet_migration" { task_definition = "arn:aws:ecs:us-west-2:123456789012:task-definition/my-fleet-app" # Base ARN without revision task_definition_revision = 5 # The *new* revision to migrate *to* min_capacity = 0 # Scale down to this during migration + max_capacity = 5 # Set maximum target scale after migration desired_count = 2 # Scale back up to this after migration subnets = ["subnet-xxxxxxxxxxxxxxxxx", "subnet-yyyyyyyyyyyyyyyyy"] security_groups = ["sg-xxxxxxxxxxxxxxxxx"] diff --git a/addons/migrations/README.md b/addons/migrations/README.md index acfdde8..6fea82e 100644 --- a/addons/migrations/README.md +++ b/addons/migrations/README.md @@ -19,6 +19,7 @@ module "fleet_migration" { task_definition = "arn:aws:ecs:us-west-2:123456789012:task-definition/my-fleet-app" # Base ARN without revision task_definition_revision = 5 # The *new* revision to migrate *to* min_capacity = 0 # Scale down to this during migration + max_capacity = 5 # Set maximum target scale after migration desired_count = 2 # Scale back up to this after migration subnets = ["subnet-xxxxxxxxxxxxxxxxx", "subnet-yyyyyyyyyyyyyyyyy"] security_groups = ["sg-xxxxxxxxxxxxxxxxx"] @@ -64,7 +65,7 @@ No requirements. | Name | Version | |------|---------| -| [aws](#provider\_aws) | 6.8.0 | +| [aws](#provider\_aws) | 6.17.0 | | [null](#provider\_null) | 3.2.4 | ## Modules @@ -87,6 +88,7 @@ No modules. | [desired\_count](#input\_desired\_count) | n/a | `number` | n/a | yes | | [ecs\_cluster](#input\_ecs\_cluster) | n/a | `string` | n/a | yes | | [ecs\_service](#input\_ecs\_service) | n/a | `string` | n/a | yes | +| [max\_capacity](#input\_max\_capacity) | n/a | `number` | n/a | yes | | [min\_capacity](#input\_min\_capacity) | n/a | `number` | n/a | yes | | [security\_groups](#input\_security\_groups) | n/a | `list(string)` | n/a | yes | | [subnets](#input\_subnets) | n/a | `list(string)` | n/a | yes | diff --git a/addons/migrations/main.tf b/addons/migrations/main.tf index e8d96e0..f5c0659 100644 --- a/addons/migrations/main.tf +++ b/addons/migrations/main.tf @@ -5,6 +5,6 @@ resource "null_resource" "main" { task_definition_revision = var.task_definition_revision } provisioner "local-exec" { - command = "/bin/bash ${path.module}/migrate.sh REGION=${data.aws_region.current.region} VULN_SERVICE=${var.vuln_service} ECS_CLUSTER=${var.ecs_cluster} TASK_DEFINITION=${var.task_definition} TASK_DEFINITION_REVISION=${var.task_definition_revision} SUBNETS=${jsonencode(var.subnets)} SECURITY_GROUPS=${jsonencode(var.security_groups)} ECS_SERVICE=${var.ecs_service} MIN_CAPACITY=${var.min_capacity} DESIRED_COUNT=${var.desired_count} ASSUME_ROLE_ARN=${var.assume_role_arn} ASSUME_ROLE_SESSION_NAME=${var.assume_role_session_name}" + command = "/bin/bash ${path.module}/migrate.sh REGION=${data.aws_region.current.region} VULN_SERVICE=${var.vuln_service} ECS_CLUSTER=${var.ecs_cluster} TASK_DEFINITION=${var.task_definition} TASK_DEFINITION_REVISION=${var.task_definition_revision} SUBNETS=${jsonencode(var.subnets)} SECURITY_GROUPS=${jsonencode(var.security_groups)} ECS_SERVICE=${var.ecs_service} MIN_CAPACITY=${var.min_capacity} MAX_CAPACITY=${var.max_capacity} DESIRED_COUNT=${var.desired_count} ASSUME_ROLE_ARN=${var.assume_role_arn} ASSUME_ROLE_SESSION_NAME=${var.assume_role_session_name}" } } diff --git a/addons/migrations/migrate.sh b/addons/migrations/migrate.sh index bc2be9c..6907125 100644 --- a/addons/migrations/migrate.sh +++ b/addons/migrations/migrate.sh @@ -6,20 +6,24 @@ function scale_services(){ SERVICE_NAME="${2:?}" # Take service name as an argument ADJUST_AUTOSCALING="${3:-}" COUNT="${4:-1}" + MAX_CAPACITY="${5:-1}" # Set the minimum capacity and desired count in the cluster to 0 to scale down or to the original size to scale back to normal. # This is a bit hacky, but the update-service has to happen first when scaling up and second when scaling down. # Assume scaling down unless "up". CAPACITY=0 + CAPACITY_MAX=0 if [ "${UP_DOWN:?}" = "up" ]; then aws ecs update-service --region "${REGION:?}" --cluster "${ECS_CLUSTER:?}" --service "${SERVICE_NAME:?}" --desired-count "${COUNT:?}" CAPACITY="${MIN_CAPACITY:?}" + CAPACITY_MAX="${MAX_CAPACITY}" fi - if [ -n "${ADJUST_AUTOSCALING}" ]; then - aws application-autoscaling register-scalable-target --region "${REGION:?}" --service-namespace ecs --resource-id "service/${ECS_CLUSTER:?}/${SERVICE_NAME:?}" --scalable-dimension "ecs:service:DesiredCount" --min-capacity "${CAPACITY:?}" - fi + if [ -n "${ADJUST_AUTOSCALING}" ]; then + aws application-autoscaling register-scalable-target --region "${REGION:?}" --service-namespace ecs --resource-id "service/${ECS_CLUSTER:?}/${SERVICE_NAME:?}" --scalable-dimension "ecs:service:DesiredCount" --min-capacity "${CAPACITY:?}" --max-capacity "${CAPACITY_MAX:?}" + fi + # We are scaling down, make it 0 if [ "${UP_DOWN:?}" != "up" ]; then aws ecs update-service --region "${REGION:?}" --cluster "${ECS_CLUSTER:?}" --service "${SERVICE_NAME:?}" --desired-count 0 @@ -54,7 +58,7 @@ if [ -n "${ASSUME_ROLE_ARN:-}" ]; then export AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN fi -scale_services down "${ECS_SERVICE:?}" true "${DESIRED_COUNT}" +scale_services down "${ECS_SERVICE:?}" true "${DESIRED_COUNT}" "${MAX_CAPACITY}" if [ -n "${VULN_SERVICE}" ]; then scale_services down "${VULN_SERVICE:?}" @@ -66,7 +70,7 @@ TASK_ARN="$(aws ecs run-task --region "${REGION:?}" --cluster "${ECS_CLUSTER:?}" # Wait for completion aws ecs wait tasks-stopped --region "${REGION:?}" --cluster="${ECS_CLUSTER:?}" --tasks="${TASK_ARN:?}" -scale_services up "${ECS_SERVICE:?}" true "${DESIRED_COUNT}" +scale_services up "${ECS_SERVICE:?}" true "${DESIRED_COUNT}" "${MAX_CAPACITY}" if [ -n "${VULN_SERVICE}" ]; then scale_services up "${VULN_SERVICE:?}" diff --git a/addons/migrations/variables.tf b/addons/migrations/variables.tf index fafc3cd..f530543 100644 --- a/addons/migrations/variables.tf +++ b/addons/migrations/variables.tf @@ -13,6 +13,11 @@ variable "min_capacity" { nullable = false } +variable "max_capacity" { + type = number + nullable = false +} + variable "desired_count" { type = number nullable = false