Skip to content

Instantly share code, notes, and snippets.

@ToQoz
Last active December 7, 2015 16:35
Show Gist options
  • Save ToQoz/0c93845444b10d1c7044 to your computer and use it in GitHub Desktop.
Save ToQoz/0c93845444b10d1c7044 to your computer and use it in GitHub Desktop.
jaws deploys lambdas/endpoints + aws cli(MFA enabled user) updates cloudformation template
#!/bin/bash
# NAME
# jaws-deploy
#
# DESCRIPTION
# jaws deploys lambdas/endpoints + aws cli (MFA enabled user) updates cloudformation template
#
# - user "$ADMIN_AWS_PROFILE" deploys lambdas/endpoints
# - user "$ADMIN_AWS_PROFILE-cloudformation" (MFA enabled) updates cloudformation template
#
# REQUIREMENTS
# - jaws
# - aws cli
# - peco
# - jq
source ./admin.env
source ./.env
PROFILE=$ADMIN_AWS_PROFILE
STAGE=$JAWS_STAGE
REGION=$(cat jaws.json | jq -r ".stages.$STAGE[].region" | peco --select-1)
APP=$(cat cloudformation/$STAGE/$REGION/resources-cf.json | jq -r '.Parameters.aaProjectName.Default')
CMD_CFN="aws cloudformation"
CMD_CFN_GET="$CMD_CFN get-template --region $REGION --profile $PROFILE"
CMD_CFN_UPDATE="$CMD_CFN update-stack --region $REGION --profile $PROFILE-cloudformation"
CMD_CFN_DESCRIBE="$CMD_CFN describe-stack-events --region $REGION --profile $PROFILE-cloudformation"
R_STACK_NAME="$STAGE-$APP-r"
L_STACK_NAME="$STAGE-$APP-l"
R_TEMPLATE=$($CMD_CFN_GET --stack-name $R_STACK_NAME)
L_TEMPLATE=$($CMD_CFN_GET --stack-name $L_STACK_NAME)
ls-modules() {
find aws_modules -maxdepth 2 -mindepth 2 -type d
}
watch-cfn-status() {
while true; do
status=$($CMD_CFN_DESCRIBE --stack-name $1 | jq -r '.StackEvents[0].ResourceStatus')
echo "$1: $status"
if [ "$status" != "UPDATE_IN_PROGRESS" ]; then
break
fi
sleep 3
done
}
# Check resources-cf.json's diff
R_LOCAL=$(cat cloudformation/$STAGE/$REGION/resources-cf.json)
R_DIFF=$(diff -u <(echo "$R_TEMPLATE" | jq --sort-keys ".TemplateBody") <(echo "$R_LOCAL" | jq --sort-keys "."))
if [ $(echo "$R_DIFF" | sed '/^$/d' | wc -l) -gt 0 ]; then
echo "resources-cf.json has diff"
echo "$R_DIFF"
read -p "Deploy resources-cf.json? [n/y]: " DEPLOY_RESOURCE
# Deploy resources-cf
if [ "$DEPLOY_RESOURCE" = "y" ]; then
echo -n "Update stack: "
$CMD_CFN_UPDATE --stack-name $R_STACK_NAME --template-body "$R_LOCAL" --capabilities CAPABILITY_IAM | jq -r ".StackId"
watch-cfn-status $R_STACK_NAME
fi
fi
# Deploy lambdas
read -p "Deploy lambdas? [n/y]: " DEPLOY_LAMBDA
if [ "$DEPLOY_LAMBDA" = "y" ]; then
while true; do
read -p "How to deploy lambdas [p,a,?]: " DEPLOY_LAMBDA_TYPE
if [ "$DEPLOY_LAMBDA_TYPE" = "a" ]; then
jaws tag --tag-all lambda
break
elif [ "$DEPLOY_LAMBDA_TYPE" = "p" ]; then
jaws tag --untag-all lambda
ls-modules | peco | xargs -I{} sh -c 'cd {} && jaws tag lambda'
break
else
echo "a - deploy all lambdas"
echo "p - select lambdas by peco and deploy"
fi
done
jaws deploy -t lambda --dont-exe-cf
fi
# Check lambdas-cf's diff
L_LOCAL=$(cat cloudformation/$STAGE/$REGION/lambdas-cf.json)
L_DIFF=$(diff -u <(echo "$L_TEMPLATE" | jq --sort-keys ".TemplateBody") <(echo "$L_LOCAL" | jq --sort-keys "."))
if [ "$DEPLOY_LAMBDA" = "y" -o $(echo "$L_DIFF" | sed '/^$/d' | wc -l) -gt 0 ]; then
echo "lambdas-cf.json has following diff"
echo "$L_DIFF"
read -p "Deploy lambdas-cf.json? [n/y]: " DEPLOY_LAMBDA_CF
# Deploy lambdas
if [ "$DEPLOY_LAMBDA_CF" = "y" ]; then
echo -n "Update stack: "
$CMD_CFN_UPDATE --stack-name $L_STACK_NAME --template-body "$L_LOCAL" | jq -r ".StackId"
watch-cfn-status $L_STACK_NAME
fi
fi
# Deploy endpoints
read -p "Deploy endpoints? [n/y]: " DEPLOY_ENDPOINT
if [ "$DEPLOY_ENDPOINT" = "y" ]; then
while true; do
read -p "How to deploy endpoints [p,a,?]: " DEPLOY_ENDPOINT_TYPE
if [ "$DEPLOY_ENDPOINT_TYPE" = "a" ]; then
jaws tag --tag-all endpoint
break
elif [ "$DEPLOY_ENDPOINT_TYPE" = "p" ]; then
jaws tag --untag-all endpoint
ls-modules | peco | xargs -I{} sh -c 'cd {} && jaws tag endpoint'
break
else
echo "a - deploy all endpoints"
echo "p - select endpoints by peco and deploy"
fi
done
# 今のとこ jaws deploy -t endpoint は cloudformation の update をしないから
# 意味ないっぽいけど、今後変更があるかどうかも分からんのでとりあえず --dont-exe-cf つけとく
jaws deploy -t endpoint --dont-exe-cf
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment