Make migration timeouts configurable (#253)
This commit is contained in:
1 parent
4e16c99b1c
commit
02a5a25e61
5 files changed
+40
-5
No files matched your search
@@ -31,6 +31,10 @@ module "fleet_migration" {
|
||||
# assume_role_arn = "arn:aws:iam::123456789012:role/MyMigrationRole"
|
||||
# assume_role_session_name = "TerraformFleetMigration"
|
||||
|
||||
# Optional: Maximum time in seconds to wait for the migration task (default 600).
|
||||
# Increase to 1800+ for large production databases with heavy schema migrations.
|
||||
# timeout_seconds = 600
|
||||
|
||||
# Ensure this module depends on the resource that creates/updates the task definition revision
|
||||
# For example:
|
||||
# depends_on = [aws_ecs_task_definition.fleet_app]
|
||||
|
||||
@@ -31,6 +31,10 @@ module "fleet_migration" {
|
||||
# assume_role_arn = "arn:aws:iam::123456789012:role/MyMigrationRole"
|
||||
# assume_role_session_name = "TerraformFleetMigration"
|
||||
|
||||
# Optional: Maximum time in seconds to wait for the migration task (default 600).
|
||||
# Increase to 1800+ for large production databases with heavy schema migrations.
|
||||
# timeout_seconds = 600
|
||||
|
||||
# Ensure this module depends on the resource that creates/updates the task definition revision
|
||||
# For example:
|
||||
# depends_on = [aws_ecs_task_definition.fleet_app]
|
||||
@@ -65,8 +69,8 @@ No requirements.
|
||||
|
||||
| Name | Version |
|
||||
|------|---------|
|
||||
| <a name="provider_aws"></a> [aws](#provider\_aws) | 6.17.0 |
|
||||
| <a name="provider_null"></a> [null](#provider\_null) | 3.2.4 |
|
||||
| <a name="provider_aws"></a> [aws](#provider\_aws) | n/a |
|
||||
| <a name="provider_null"></a> [null](#provider\_null) | n/a |
|
||||
|
||||
## Modules
|
||||
|
||||
@@ -94,6 +98,7 @@ No modules.
|
||||
| <a name="input_subnets"></a> [subnets](#input\_subnets) | n/a | `list(string)` | n/a | yes |
|
||||
| <a name="input_task_definition"></a> [task\_definition](#input\_task\_definition) | n/a | `string` | n/a | yes |
|
||||
| <a name="input_task_definition_revision"></a> [task\_definition\_revision](#input\_task\_definition\_revision) | n/a | `number` | n/a | yes |
|
||||
| <a name="input_timeout_seconds"></a> [timeout\_seconds](#input\_timeout\_seconds) | Maximum time in seconds to wait for the migration ECS task to complete. Default 600 (10 min). Increase to 1800+ for large production databases. | `number` | `600` | no |
|
||||
| <a name="input_vuln_service"></a> [vuln\_service](#input\_vuln\_service) | n/a | `string` | `""` | no |
|
||||
|
||||
## Outputs
|
||||
|
||||
@@ -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} 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}"
|
||||
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} TIMEOUT_SECONDS=${var.timeout_seconds}"
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
#!/bin/bash
|
||||
set -e
|
||||
export AWS_PAGER=""
|
||||
|
||||
function scale_services(){
|
||||
UP_DOWN="${1:?}"
|
||||
@@ -81,8 +82,27 @@ fi
|
||||
# Call aws ecs run-task
|
||||
TASK_ARN="$(aws ecs run-task --region "${REGION:?}" --cluster "${ECS_CLUSTER:?}" --task-definition "${TASK_DEFINITION:?}":"${TASK_DEFINITION_REVISION:?}" --launch-type FARGATE --network-configuration "awsvpcConfiguration={subnets="${SUBNETS:?}",securityGroups="${SECURITY_GROUPS:?}"}" --query 'tasks[].taskArn' --overrides '{"containerOverrides": [{"name": "fleet", "command": ["fleet", "prepare", "db"]}]}' --output text | rev | cut -d'/' -f1 | rev)"
|
||||
|
||||
# Wait for completion
|
||||
aws ecs wait tasks-stopped --region "${REGION:?}" --cluster="${ECS_CLUSTER:?}" --tasks="${TASK_ARN:?}"
|
||||
# Wait for completion with configurable timeout
|
||||
ELAPSED=0
|
||||
while [ "$ELAPSED" -lt "${TIMEOUT_SECONDS:-600}" ]; do
|
||||
sleep 30
|
||||
ELAPSED=$((ELAPSED + 30))
|
||||
TASK_STATUS=$(aws ecs describe-tasks --region "${REGION:?}" --cluster "${ECS_CLUSTER:?}" --tasks "${TASK_ARN:?}" --query "tasks[0].lastStatus" --output text)
|
||||
if [ "$TASK_STATUS" = "STOPPED" ]; then
|
||||
break
|
||||
fi
|
||||
echo "Migration task status: ${TASK_STATUS} (elapsed: ${ELAPSED}s / ${TIMEOUT_SECONDS:-600}s)" >&2
|
||||
done
|
||||
|
||||
if [ "$TASK_STATUS" != "STOPPED" ]; then
|
||||
echo "ERROR: Migration task timed out after ${TIMEOUT_SECONDS:-600} seconds." >&2
|
||||
echo "Task ARN: ${TASK_ARN}" >&2
|
||||
echo "Task Status: ${TASK_STATUS}" >&2
|
||||
echo "Check the task manually:" >&2
|
||||
echo " aws ecs describe-tasks --region ${REGION} --cluster ${ECS_CLUSTER} --tasks ${TASK_ARN}" >&2
|
||||
echo "If the task completed successfully, re-running terraform apply will detect this and continue." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
scale_services up "${ECS_SERVICE:?}" true "${DESIRED_COUNT}"
|
||||
|
||||
|
||||
@@ -58,3 +58,9 @@ variable "assume_role_session_name" {
|
||||
default = ""
|
||||
}
|
||||
|
||||
variable "timeout_seconds" {
|
||||
description = "Maximum time in seconds to wait for the migration ECS task to complete. Default 600 (10 min). Increase to 1800+ for large production databases."
|
||||
type = number
|
||||
default = 600
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user