使用witihin循环中的变量设置AutoScaleGroup所需容量

时间:2019-01-14 12:57:52

标签: bash aws-cli

我正在运行以下脚本,该脚本获取通过名称Test过滤的堆栈的名称。

我可以输出当前的容量,最大值和最小值,我可以在通过ang循环获取值时回显比例组的名称, 但似乎无法在$ group变量上设置所需的容量,我一直收到null值,我尝试了几种变体,但都返回null。

命令

read -p 'Enter Name Where Scaling Groups will be updated: "Default Stack" = ' -e -i 'Test' stackname \ 

scalegroups=($(aws autoscaling describe-auto-scaling-groups --query 'AutoScalingGroups[?contains(Tags[?Key==`aws:cloudformation:stack-name`].Value, `'$stackname'`)].AutoScalingGroupName')) \
echo "Total Stacks Found :" ${scalegroups[*]} \
for group in "${scalegroups[@]//,/}" \

do 
    currentcapacity=$(aws autoscaling describe-auto-scaling-groups --query 'AutoScalingGroups[?AutoScalingGroupName==`'$group'`].[DesiredCapacity]' --output text) \
    echo $group 
    aws autoscaling set-desired-capacity --auto-scaling-group-name $group --desired-capacity $((currentcapacity + 1)) --honor-cooldown \
    latestcapacity=$(aws autoscaling describe-auto-scaling-groups --query 'AutoScalingGroups[?AutoScalingGroupName==`'$group'`].[DesiredCapacity]' --output text) \
    echo "Latest Capacity = " $latestcapacity \
done 

错误

An error occurred (ValidationError) when calling the SetDesiredCapacity operation: AutoScalingGroup name not found - null

1 个答案:

答案 0 :(得分:0)

很难说,因为您的脚本中有很多语法错误,至少与您在此处粘贴的内容一样。

我修复了脚本,以使其按预期的方式工作。我将其更改为:

echo -n 'Enter Name Where Scaling Groups will be updated: "Default Stack" = ' -e -i 'Test ' ; read stackname

scalegroups=($(aws autoscaling describe-auto-scaling-groups \
  --query 'AutoScalingGroups[?contains(Tags[?Key==`aws:cloudformation:stack-name`].Value, `'$stackname'`)].AutoScalingGroupName' \
  --output text))

echo "Total Stacks Found :" ${scalegroups[*]}

for group in "${scalegroups[@]//,/}"
do 
  echo "Processing group: $group"

  currentcapacity=$(aws autoscaling describe-auto-scaling-groups \
    --query 'AutoScalingGroups[?AutoScalingGroupName==`'$group'`].DesiredCapacity' \
    --output text)

  aws autoscaling set-desired-capacity \
    --auto-scaling-group-name $group \
    --desired-capacity $((currentcapacity + 1)) \
    --honor-cooldown

  latestcapacity=$(aws autoscaling describe-auto-scaling-groups \
    --query 'AutoScalingGroups[?AutoScalingGroupName==`'$group'`].DesiredCapacity' \
    --output text)

  echo "Latest Capacity = " $latestcapacity
done

几点:

  • 我认为您的错误可能在比例组行中。您丢失了--output text
  • 请注意,我使用换行符(\字符)使其更具可读性。
  • 我删除了DesiredCapacity对数组[DesiredCapacity]的多余强制。