Skip to content

Instantly share code, notes, and snippets.

@namuan
Created March 5, 2017 08:36
Show Gist options
  • Save namuan/b1781ed4edc01b961f59c60b5a998ce5 to your computer and use it in GitHub Desktop.
Save namuan/b1781ed4edc01b961f59c60b5a998ce5 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# From: http://blog.slashdeploy.com/2016/03/05/bootstrapping-dns-on-aws/
set -euo pipefail
declare stack_name="dns"
usage() {
echo "${0} COMMAND [options] [arguments]"
echo
echo "deploy -- deploy clouformation changes"
echo "status -- show clouformation status"
echo "ns -- print NS records"
echo "destroy -- Delete stack"
}
cloudformation() {
aws --profile slashdeploy --region eu-west-1 cloudformation "$@"
}
r53() {
aws --profile slashdeploy --region eu-west-1 route53 "$@"
}
deploy() {
if cloudformation describe-stacks --stack-name "${stack_name}" &> /dev/null ; then
cloudformation update-stack \
--stack-name "${stack_name}" \
--template-body "file://cloudformation.json"
else
cloudformation create-stack \
--stack-name "${stack_name}" \
--template-body "file://cloudformation.json"
fi
}
destroy() {
cloudformation delete-stack --stack-name "${stack_name}"
}
show_status() {
cloudformation describe-stacks \
--stack-name "${stack_name}" \
| jq -re '.Stacks[0].StackStatus'
}
validate_stack() {
cloudformation validate-template \
--template-body "file://cloudformation.json"
}
show_ns() {
declare zone_id
zone_id="$(cloudformation describe-stacks --stack-name "${stack_name}" \
| jq -re '.Stacks[0].Outputs[0].OutputValue')"
r53 list-resource-record-sets --hosted-zone-id="${zone_id}" \
| jq -re '.ResourceRecordSets[] | select(.Type == "NS") | .ResourceRecords | map(.Value)[]'
}
main() {
case "${1:-}" in
deploy)
shift
deploy "$@"
;;
destroy)
shift
destroy "$@"
;;
status)
shift
show_status "$@"
;;
validate)
shift
validate_stack "$@"
;;
ns)
shift
show_ns "$@"
;;
*)
usage 1>&2
return 1
;;
esac
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment