Skip to content

Instantly share code, notes, and snippets.

@snaka
Last active December 24, 2022 07:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save snaka/c3b7a1f8c03d95e24d6298bdaf2160de to your computer and use it in GitHub Desktop.
Save snaka/c3b7a1f8c03d95e24d6298bdaf2160de to your computer and use it in GitHub Desktop.
Shell scripts to manipulate ECS AutoScaling settings

Shell scripts to manipulate ECS AutoScaling settings

Requirement

  • jq
  • aws cli

Usage

Show Auto Scaling policies

ecs-autoscaling show policy

Show Auto Scaling target

ecs-autoscaling show policy

Apply Auto Scaling policy & target

ecs-autoscaling show policy path/to/files
{
"cluster": "connected-base-dev",
"service": "web",
"min_capacity": 1,
"max_capacity": 4
}
#!/bin/bash
set -eu -o pipefail
ProgName=$(basename $0)
sub_help() {
echo "Usage: $ProgName <subcommand>"
echo ""
echo "Subcommands:"
echo " show"
echo " apply"
echo ""
echo "Example:"
echo " $ProgName show target"
echo " $ProgName show policy"
echo " $ProgName apply <path>"
echo ""
exit 1
}
sub_show() {
Type=${1:-""}
case $Type in
target)
aws application-autoscaling describe-scalable-targets --service-namespace ecs
;;
policy)
aws application-autoscaling describe-scaling-policies --service-namespace ecs
;;
*)
echo "Usage: $ProgName show {target|policy}"
echo ""
;;
esac
}
as_policy_name="ecs-service-scaling"
sub_apply() {
set -x
path=${1:-""}
if [ -z "$path" ]; then
echo "path is required"
exit 1
fi
cluster=$(jq -r '.cluster' $path/config.json)
service=$(jq -r '.service' $path/config.json)
min_capacity=$(jq -r '.min_capacity' $path/config.json)
max_capacity=$(jq -r '.max_capacity' $path/config.json)
aws application-autoscaling register-scalable-target \
--service-namespace ecs \
--resource-id service/$cluster/$service \
--scalable-dimension ecs:service:DesiredCount \
--min-capacity $min_capacity \
--max-capacity $max_capacity
aws application-autoscaling put-scaling-policy \
--service-namespace ecs \
--resource-id service/$cluster/$service \
--scalable-dimension ecs:service:DesiredCount \
--policy-name $as_policy_name \
--policy-type TargetTrackingScaling \
--target-tracking-scaling-policy-configuration file://$path/policy.json
}
subcommand=${1:-""}
case $subcommand in
"" | "-h" | "--help" | "help")
sub_help
;;
*)
shift
sub_${subcommand} $@
if [ $? = 127 ]; then
echo "Error: '$subcommand' is not a known subcommand." >&2
echo " Run '$ProgName --help' for a list of known subcommands." >&2
exit 1
fi
;;
esac
{
"TargetValue": 60.0,
"PredefinedMetricSpecification": {
"PredefinedMetricType": "ECSServiceAverageCPUUtilization"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment