0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

AWS CloufFormationのスタックをAWS CLIでリスト形式にして出力し、古いものを削除するコマンド

Posted at

基本構文

aws cloudformation describe-stacks --region ap-northeast-1 --profile profile

変数化

REGION=ap-northeast-1 PROFILE=profile
aws cloudformation describe-stacks --region ${REGION} --profile ${PROFILE}

作成順で並び替え

REGION=ap-northeast-1 PROFILE=profile
aws cloudformation describe-stacks --region ${REGION} --profile ${PROFILE} | jq -r '.Stacks | sort_by(.CreationTime)'

スタック名と作成日を抜き出す

REGION=ap-northeast-1 PROFILE=profile
aws cloudformation describe-stacks --region ${REGION} --profile ${PROFILE} | jq -r '.Stacks | .[] | [.StackName, .CreationTime]'

スタック名で特定の文字列が含まれているもの(先頭がooで始まるもの)

下記を入れる

select(.StackName | test("^'${PROJECT_HEADER}'")

コマンド

REGION=ap-northeast-1 PROFILE=profile
PREFIX=my-stack-
aws cloudformation describe-stacks --region ${REGION} --profile ${PROFILE} | jq -r '.Stacks | .[] | select(.StackName | test("^'${PREFIX}'")) | .StackName'

スタック名が特定のスタック名以外(masterやdevelop以外のスタックを表示)

(contains("develop") | not)
REGION=ap-northeast-1 PROFILE=profile
aws cloudformation describe-stacks --region ${REGION} --profile ${PROFILE} | jq -r '.Stacks | .[] | select(.StackName |(contains("develop") | not) and (contains("master") | not) ) | .StackName'

特定の件数を表示

下記のようにすると10件目以降を表示する。

.['10':][]

sort_by(.CreationTime) と組み合わせると最新10件を表示する。(10件目以降の古いスタックを表示)

REGION=ap-northeast-1 PROFILE=profile
LIMIT=10
aws cloudformation describe-stacks --region ${REGION} --profile ${PROFILE} | jq -r '.Stacks | sort_by(.CreationTime) | .['${LIMIT}':][] | .StackName'

古いスタックをすべて表示

これまでのものを組み合わせて利用。

REGION=ap-northeast-1 PROFILE=profile
LIMIT=10
PREFIX=my-stack

aws cloudformation describe-stacks --region ${REGION} --profile ${PROFILE} | jq -r '.Stacks | sort_by(.CreationTime) | .['${LIMIT}':][] | { 'StackName': .StackName, 'CreationTime': .CreationTime } | select(.StackName | test("^'${PREFIX}'") and (contains("develop") | not) and (contains("master") | not) ) | .StackName'

古いスタックを削除する

delete.sh
stacks=$(aws cloudformation describe-stacks --region ${REGION} --profile ${PROFILE} | jq -r '.Stacks | sort_by(.CreationTime) | .['${LIMIT}':][] | { 'StackName': .StackName, 'CreationTime': .CreationTime } | select(.StackName | test("^'${PREFIX}'") and (contains("develop") | not) and (contains("master") | not) ) | .StackName')


for STACK in ${stacks}
do
  read -n1 -p "${STACK} : delete? (y/N):" yn
  echo ""
  case "$yn" in [yY]*) echo "Deleting ${BUCKET_NAME} ...";
   aws cloudformation delete-stack --stack-name ${STACK} --region ${REGION} --profile ${PROFILE};
   echo "done" ;; *) continue ;;
  esac
done
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?