Created
March 28, 2022 12:55
-
-
Save papes1ns/112356acfee327ac27e781cb189d95dd to your computer and use it in GitHub Desktop.
ECS run task with overrides
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# step 1: define the AWS parameters to target the latest task definition for an | |
# ECS service and build the parameter to override the default container command. | |
profile=[your-profile] | |
region=[your-region] | |
cluster=[your-cluster] | |
service=[your-cluster-service] | |
cmd=$(cat <<EOF | |
{"containerOverrides": [ | |
{ | |
"name": "$service", | |
"command": ["bundle", "exec", "rails", "db:migrate"] | |
} | |
] | |
} | |
EOF | |
) | |
# step 2: query AWS for the latest task definition given parameters specified | |
# above and strip it down to the latest revision of the task definition | |
taskDefinition=$( \ | |
aws ecs list-task-definitions --region $region \ | |
--profile $profile \ | |
--family-prefix $service \ | |
--output text \ | |
| grep $service | tail -n1 | awk -F '/' '{print $NF}' \ | |
) | |
# step 3: run a new ECS task that runs the override command specified above | |
# using the latest revision of the task definition for the targeted service | |
echo "running database migration using '$taskDefinition' task definition" | |
aws ecs run-task --profile $profile \ | |
--region $region \ | |
--cluster $cluster \ | |
--task-definition $taskDefinition \ | |
--overrides "$cmd" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment