首页 > 解决方案 > 尝试为 Cloudformation 创建/更新 bash 脚本时出现 AlreadyExistsException 错误

问题描述

嗨我正在尝试编写一个 bash 来快速创建/更新我的部署创建的新堆栈工作正常,但是当我尝试更新堆栈时,我得到以下错误 调用 CreateStack 操作时发生错误(AlreadyExistsException):堆栈 [测试-stack] 已经存在

下面是 BASH 脚本

    #!/usr/bin/env bash

usage="Usage: $(basename "$0") region stack-name [aws-cli-opts]
where:
  region       - the AWS region
  stack-name   - the stack name
  aws-cli-opts - extra options passed directly to create-stack/update-stack
"

if [ "$1" == "-h" ] || [ "$1" == "--help" ] || [ "$1" == "help" ] || [ "$1" == "usage" ] ; then
  echo "$usage"
  exit -1
fi

if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ] ; then
  echo "$usage"
  exit -1
fi

shopt -s failglob
set -eu -o pipefail

echo "Checking if stack exists ..."

if ! aws cloudformation describe-stacks --region $1 --stack-name $2 ; then

  echo -e "\nStack does not exist, creating ..."
  aws cloudformation create-stack \
    --region $1 \
    --stack-name $2 \
    ${@:3}

  echo "Waiting for stack to be created ..."
  aws cloudformation wait stack-create-complete \
    --region $1 \
    --stack-name $2 \

else

  echo -e "\nStack exists, attempting update ..."

  set +e
  update_output=$( aws cloudformation update-stack \
    --region $1 \
    --stack-name $2 \
    ${@:3}  2>&1)
  status=$?
  set -e

  echo "$update_output"

  if [ $status -ne 0 ] ; then

    # Don't fail for no-op update
    if [[ $update_output == *"ValidationError"* && $update_output == *"No updates"* ]] ; then
      echo -e "\nFinished create/update - no updates to be performed"
      exit 0
    else
      exit $status
    fi

  fi

  echo "Waiting for stack update to complete ..."
  aws cloudformation wait stack-update-complete \
    --region $1 \
    --stack-name $2 \

fi

echo "Finished create/update successfully!"

标签: linuxbashamazon-web-servicesshellamazon-cloudformation

解决方案


当我没有该describe-stacks功能的权限时,似乎可以触发此错误。检查您的权限以运行它。

由于没有权限,它会进入创建堆栈逻辑。


推荐阅读