Skip to content

Instantly share code, notes, and snippets.

@papes1ns
Created March 28, 2022 12:55
Show Gist options
  • Save papes1ns/112356acfee327ac27e781cb189d95dd to your computer and use it in GitHub Desktop.
Save papes1ns/112356acfee327ac27e781cb189d95dd to your computer and use it in GitHub Desktop.
ECS run task with overrides
#!/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