Add --max-capacity support to migrate.sh (#117)

- Adds support for `CAPACITY_MAX="${MAX_CAPACITY}"`, if scaling up.
`CAPACITY_MAX = 0`, if scaling down.
- Adds support for `--max-capacity` in the `${ADJUST_AUTOSCALING}` logic
This commit is contained in:
Jorge Falcon authored and GitHub committed 2025-10-24 12:47:04 -04:00
1 parent 3a48d9a93f
commit fc25a6954a
5 files changed
+19 -7

No files matched your search

+1
View File
@@ -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"]
+3 -1
View File
@@ -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 |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | 6.8.0 |
| <a name="provider_aws"></a> [aws](#provider\_aws) | 6.17.0 |
| <a name="provider_null"></a> [null](#provider\_null) | 3.2.4 |
## Modules
@@ -87,6 +88,7 @@ No modules.
| <a name="input_desired_count"></a> [desired\_count](#input\_desired\_count) | n/a | `number` | n/a | yes |
| <a name="input_ecs_cluster"></a> [ecs\_cluster](#input\_ecs\_cluster) | n/a | `string` | n/a | yes |
| <a name="input_ecs_service"></a> [ecs\_service](#input\_ecs\_service) | n/a | `string` | n/a | yes |
| <a name="input_max_capacity"></a> [max\_capacity](#input\_max\_capacity) | n/a | `number` | n/a | yes |
| <a name="input_min_capacity"></a> [min\_capacity](#input\_min\_capacity) | n/a | `number` | n/a | yes |
| <a name="input_security_groups"></a> [security\_groups](#input\_security\_groups) | n/a | `list(string)` | n/a | yes |
| <a name="input_subnets"></a> [subnets](#input\_subnets) | n/a | `list(string)` | n/a | yes |
+1 -1
View File
@@ -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}"
}
}
+9 -5
View File
@@ -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:?}"
+5
View File
@@ -13,6 +13,11 @@ variable "min_capacity" {
nullable = false
}
variable "max_capacity" {
type = number
nullable = false
}
variable "desired_count" {
type = number
nullable = false