※ CloudFormation実行用のシェル
#!/bin/bash

CURRENT_DIR=`dirname $0`
MODE=$1

STAGE=dev
REGION=
FORCE=
while [ "$1" != "" ]; do
  if [ "$1" == "--region" ]; then
    shift
    REGION=$1
  fi
  if [ "$1" == "--env" ]; then
    shift
    STAGE=$1
  fi
  if [ "$1" == "--force" ]; then
    FORCE=y
  fi
  shift
done

# テンプレートファイル
TEMPLATE=template.yml

# スタック名 ( gitプロジェクトの場合はgitリポジトリ名、それ以外の場合はフォルダ名をスタック名とする )
cd $CURRENT_DIR
STACK_NAME=`basename \`pwd\``
if [ -e ".git" ]; then
    STACK_NAME=`cat .git/config | grep url | head -1 | awk -F"/" '{print $NF}'`
fi
STACK_NAME=`echo $STACK_NAME | sed 's/_/-/g' | sed 's/Repo$/Stack/'`
STACK_NAME=${STACK_NAME}-${STAGE}

# アカウントIDの取得
ACCOUNT_ID=`aws sts get-caller-identity | grep Account | awk '{print $2}' | sed -e "s/[^0-9]//g"`

# リージョン指定
if [ "${REGION}" == "" ]; then
  REGION=`aws configure list | grep "region" | awk '{print $2}'`
fi
REGION_PARAM="--region ${REGION}"

# スタック作成時のイベント確認
if [ "${MODE}" == "events" ]; then
    echo "Display events of Stack: ${STACK_NAME}"
    echo `date "+%Y-%m-%d %H:%M:%S"`" - START"
    aws cloudformation describe-stack-events --region $REGION --stack-name $STACK_NAME
    echo `date "+%Y-%m-%d %H:%M:%S"`" - END"
    exit 0
fi

# 削除
if [ "${MODE}" == "delete" ]; then
    if [ "$FORCE" != "y" ] && [ "$STAGE" == "prod" ]; then
      echo ""
      read -p "Delete Stack in production environment? (y/n): " yn
      if [ "$yn" != "y" ]; then
        echo "\nDelete Stack Canceled."
        echo ""
        exit 0
      fi
    fi
    echo "Delete Stack: ${STACK_NAME}"
    echo `date "+%Y-%m-%d %H:%M:%S"`" - START"
    aws cloudformation delete-stack --region $REGION --stack-name $STACK_NAME
    aws cloudformation wait stack-delete-complete --region $REGION --stack-name $STACK_NAME
    echo `date "+%Y-%m-%d %H:%M:%S"`" - END"
    exit 0
fi

# 登録/更新
if [ "${MODE}" == "deploy" ]; then
    if [ "$FORCE" != "y" ] && [ "$STAGE" == "prod" ]; then
      echo ""
      read -p "Create/Update Stack in production environment? (y/n): " yn
      if [ "$yn" != "y" ]; then
        echo "Create/Update Stack Canceled."
        echo ""
        exit 0
      fi
    fi
    echo "Create/Update Stack: ${STACK_NAME}"
    echo `date "+%Y-%m-%d %H:%M:%S"`" - START"

    # S3バケットがない場合は作る(バケット名は世界で唯一である必要がある為、末尾にアカウントID等を付与しておく)
    #BUCKET_NAME=stack-${STACK_NAME}-${ACCOUNT_ID}
    BUCKET_NAME=cf-templates-${REGION}-${ACCOUNT_ID}
    BUCKET_COUNT=`aws s3api list-buckets --region $REGION | grep -e "\"${BUCKET_NAME}\"" | wc -l | awk '{print $1}'`
    if [ "${BUCKET_COUNT}" == "0" ]; then
        echo Create s3 bucket: ${BUCKET_NAME}
        if [ "${REGION}" == "us-east-1" ]; then
          # es-east-1 の場合は LocationConstraint の指定なしで作成
          aws s3api create-bucket --region $REGION --bucket $BUCKET_NAME
        else
          aws s3api create-bucket --region $REGION --create-bucket-configuration "{\"LocationConstraint\": \"${REGION}\"}" --bucket $BUCKET_NAME
        fi
    fi  

    # 検証&パッケージング&デプロイ(成功時は作成したAPIのURIを表示する)
    #aws cloudformation validate-template --template-body file://${TEMPLATE} \
    aws cloudformation package --region $REGION --template-file $TEMPLATE --s3-bucket $BUCKET_NAME --output-template-file packaged-template.yml \
      && aws cloudformation deploy --region $REGION --template-file packaged-template.yml --stack-name $STACK_NAME --parameter-overrides StageName="$STAGE" --capabilities CAPABILITY_IAM CAPABILITY_NAMED_IAM CAPABILITY_AUTO_EXPAND \
      && echo "" \
      && echo "### Exported Value ###" \
      && aws cloudformation describe-stacks --region $REGION --stack-name $STACK_NAME \
           | awk 'BEGIN{key=""}{ if ($1 == "\"OutputKey\":") key=$2; if ($1 == "\"OutputValue\":") print key" : "$2 }' \
           | sed 's/[",]//g' \
      && echo "######################/" \
      && echo ""
    echo `date "+%Y-%m-%d %H:%M:%S"`" - END"
    exit 0
fi

echo "Usage)"
echo ""
echo "  ${0} (deploy|delete|events) [--region regionName] [--env envName] [--force]"
echo ""
echo "Example)"
echo ""
echo "  # create or update stack named '${STACK_NAME}'"
echo "  ${0} deploy --env prod"
echo "  ${0} deploy --env prod --region us-east-1"
echo ""
echo "  # delete stack named '${STACK_NAME}'"
echo "  ${0} delete --env prod"
echo "  ${0} delete --env prod --region us-east-1"
echo ""
echo "  # display events details of create or update or delete stack of '${STACK_NAME}'"
echo "  ${0} events --env prod"
echo "  ${0} events --env prod --region us-east-1"
echo ""
echo "Details)"
echo ""
echo "  StackName ... The stack name will be the git repository name or folder name"
echo ""

※ 関連 AWS CloudFormationメモ


トップ   差分 バックアップ リロード   一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2019-08-19 (月) 03:56:59 (630d)