Skip to content

Instantly share code, notes, and snippets.

@yyoshiki41
Created March 30, 2018 08:02
Show Gist options
  • Save yyoshiki41/8ff441eb245490b0a50a3e6c52f45402 to your computer and use it in GitHub Desktop.
Save yyoshiki41/8ff441eb245490b0a50a3e6c52f45402 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
# depend on awscli
export AWS_DEFAULT_REGION=ap-northeast-1
export AWS_ACCESS_KEY_ID=
export AWS_SECRET_ACCESS_KEY=
# 1. Parse the arguments
mode=${1}
case "$mode" in
"up" ) echo "Start scale up" ;;
"down" ) echo "Start scale down" ;;
* ) echo "Please specify arg (up or down)" && exit 1 ;;
esac
# 2. Get the auto scaling groups
asGroups=$(aws autoscaling describe-auto-scaling-groups --query 'AutoScalingGroups[].AutoScalingGroupName' --output text)
if [ "x${asGroups}" == "x" ]; then
echo "${asGroups} does not exist" && exit 1
fi
echo "${asGroups}"
# 3. Get the auto scaling group for prod env from asGroups
codeDeployProd=""
for group in $asGroups; do
if [[ $group = *"CodeDeploy_prod"* ]]; then
codeDeployProd=$group
break
fi
done
if [ "x${codeDeployProd}" == "x" ]; then
echo "Target auto scaling group does not exist" && exit 1
fi
echo "Target auto scaling group is $codeDeployProd"
# 4. Get the current desired capacity
cap=$(aws autoscaling describe-auto-scaling-groups --auto-scaling-group-names "$codeDeployProd" --query 'AutoScalingGroups[].DesiredCapacity' --output text)
echo "DesiredCapacity is $cap"
# 5. Set the desired capacity
newCap=$((cap + 1))
if [ "${mode}" == "down" ]; then
newCap=$((cap - 1))
if [ "$newCap" -le 0 ]; then
echo "New DesiredCapacity is less than 0" && exit 1
fi
fi
echo "New DesiredCapacity is $newCap"
aws autoscaling set-desired-capacity --auto-scaling-group-name "$codeDeployProd" --desired-capacity "$newCap"
if [ $? != 0 ]; then
echo "Failed to set DesiredCapacity" && exit 1
fi
echo "Success"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment