Skip to content

Commit f57a6ba

Browse files
authored
migrator addon vuln service compatibility (#17430)
initial pass after pairing session -- not yet tested but wanted to get early feedback
1 parent ce82e4e commit f57a6ba

File tree

4 files changed

+33
-11
lines changed

4 files changed

+33
-11
lines changed

terraform/addons/external-vuln-scans/outputs.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@ output "extra_environment_variables" {
33
FLEET_VULNERABILITIES_DISABLE_SCHEDULE = "true"
44
}
55
}
6+
7+
output "vuln_service_arn" {
8+
value = aws_ecs_service.fleet.id
9+
}

terraform/addons/migrations/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ resource "null_resource" "main" {
55
task_definition_revision = var.task_definition_revision
66
}
77
provisioner "local-exec" {
8-
command = "/bin/bash ${path.module}/migrate.sh REGION=${data.aws_region.current.name} 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}"
8+
command = "/bin/bash ${path.module}/migrate.sh REGION=${data.aws_region.current.name} 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}"
99
}
1010
}

terraform/addons/migrations/migrate.sh

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,60 @@ set -e
33

44
function scale_services(){
55
UP_DOWN="${1:?}"
6+
SERVICE_NAME="${2:?}" # Take service name as an argument
7+
ADJUST_AUTOSCALING="${3:-}"
8+
COUNT="${4:-1}"
9+
610
# 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.
711

812
# This is a bit hacky, but the update-service has to happen first when scaling up and second when scaling down.
913
# Assume scaling down unless "up".
1014
CAPACITY=0
1115
if [ "${UP_DOWN:?}" = "up" ]; then
12-
aws ecs update-service --region "${REGION:?}" --cluster "${ECS_CLUSTER:?}" --service "${ECS_SERVICE:?}" --desired-count "${DESIRED_COUNT:?}"
16+
aws ecs update-service --region "${REGION:?}" --cluster "${ECS_CLUSTER:?}" --service "${SERVICE_NAME:?}" --desired-count "${COUNT:?}"
1317
CAPACITY="${MIN_CAPACITY:?}"
1418
fi
15-
aws application-autoscaling register-scalable-target --region "${REGION:?}" --service-namespace ecs --resource-id "service/${ECS_CLUSTER:?}/${ECS_SERVICE:?}" --scalable-dimension "ecs:service:DesiredCount" --min-capacity "${CAPACITY:?}"
19+
20+
if [ -n "${ADJUST_AUTOSCALING}" ]; then
21+
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:?}"
22+
fi
1623
# We are scaling down, make it 0
1724
if [ "${UP_DOWN:?}" != "up" ]; then
18-
aws ecs update-service --region "${REGION:?}" --cluster "${ECS_CLUSTER:?}" --service "${ECS_SERVICE:?}" --desired-count 0
25+
aws ecs update-service --region "${REGION:?}" --cluster "${ECS_CLUSTER:?}" --service "${SERVICE_NAME:?}" --desired-count 0
1926
fi
20-
# The first task defintion might never get stable because it never had initial migrations so don't wait before continuing
27+
# The first task definition might never get stable because it never had initial migrations so don't wait before continuing
2128
if [ "${TASK_DEFINITION_REVISION}" != "1" ]; then
2229
# Wait for scale-down to succeed
23-
aws ecs wait services-stable --region "${REGION:?}" --cluster "${ECS_CLUSTER:?}" --service "${ECS_SERVICE:?}"
30+
aws ecs wait services-stable --region "${REGION:?}" --cluster "${ECS_CLUSTER:?}" --services "${SERVICE_NAME:?}"
2431
fi
2532
}
2633

2734
for ARGUMENT in "$@"
2835
do
2936
KEY=$(echo $ARGUMENT | cut -f1 -d=)
30-
3137
KEY_LENGTH=${#KEY}
3238
VALUE="${ARGUMENT:$KEY_LENGTH+1}"
33-
3439
export "$KEY"="$VALUE"
3540
done
3641

37-
scale_services down
42+
scale_services down "${ECS_SERVICE:?}" true "${DESIRED_COUNT}"
43+
44+
if [ -n "${VULN_SERVICE}" ]; then
45+
scale_services down "${VULN_SERVICE:?}"
46+
fi
3847

3948
# Call aws ecs run-task
4049
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)"
4150

4251
# Wait for completion
4352
aws ecs wait tasks-stopped --region "${REGION:?}" --cluster="${ECS_CLUSTER:?}" --tasks="${TASK_ARN:?}"
4453

45-
scale_services up
54+
scale_services up "${ECS_SERVICE:?}" true "${DESIRED_COUNT}"
55+
56+
if [ -n "${VULN_SERVICE}" ]; then
57+
scale_services up "${VULN_SERVICE:?}"
58+
fi
4659

4760
# Exit with task's exit code
48-
TASK_EXIT_CODE=$(aws ecs describe-tasks --region "${REGION:?}" --cluster ${ECS_CLUSTER:?} --tasks ${TASK_ARN:?} --query "tasks[0].containers[?name=='fleet'].exitCode" --output text)
61+
TASK_EXIT_CODE=$(aws ecs describe-tasks --region "${REGION:?}" --cluster "${ECS_CLUSTER:?}" --tasks "${TASK_ARN:?}" --query "tasks[0].containers[?name=='fleet'].exitCode" --output text)
4962
exit "${TASK_EXIT_CODE}"

terraform/addons/migrations/variables.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,8 @@ variable "security_groups" {
3737
type = list(string)
3838
nullable = false
3939
}
40+
41+
variable "vuln_service" {
42+
default = ""
43+
}
44+

0 commit comments

Comments
 (0)