Skip to content

Instantly share code, notes, and snippets.

@anhdiepmmk
Created July 26, 2022 08:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anhdiepmmk/c0bd22019008ac7efc4d63f51d89d4e2 to your computer and use it in GitHub Desktop.
Save anhdiepmmk/c0bd22019008ac7efc4d63f51d89d4e2 to your computer and use it in GitHub Desktop.
#!/bin/bash
export AWS_ACCESS_KEY_ID="..."
export AWS_SECRET_ACCESS_KEY="..."
export AWS_SESSION_TOKEN="..."
should_terminate_ec2_instance=no
sleep_after_terminate_ec2_instance=30s
usage() {
echo "Usage: ./update-private-dns.sh --terminate-ec2 no --sleep 40s --service a-alias"
}
while [ "$#" -gt 0 ]
do
case "$1" in
--help)
usage
exit 0
;;
--terminate-ec2)
should_terminate_ec2_instance="$2"
;;
--sleep)
sleep_after_terminate_ec2_instance="$2"
;;
--service)
service="$2"
;;
--)
break
;;
# an option argument, continue
*) ;;
esac
shift
done
if [[ -z "$service" ]]; then
echo "service can not be empty"
exit 1
fi
HOSTED_ZONE_ID=an-example
# ec2 mapping
declare -A SERVICE_NAMES_TO_EC2_NAMES=(
["a-alias"]="a-instance-name"
["b-alias"]="b-instance-name"
["c-alias"]="c-instance-name"
)
# route53 mapping
declare -A SERVICE_NAMES_TO_RECORD_NAMES=(
["a-alias"]="a-record-name"
["b-alias"]="b-record-name"
["c-alias"]="c-record-name"
)
instance_name=${SERVICE_NAMES_TO_EC2_NAMES[$service]}
record_name=${SERVICE_NAMES_TO_RECORD_NAMES[$service]}
if [[ -z "$instance_name" || -z "$record_name" ]]; then
echo "invalid service"
exit 1
fi
get_ec2_instance_by_ec2_instance_name() {
local name=$1
aws ec2 describe-instances --region ap-southeast-1 --no-cli-pager \
--filters Name=tag:Name,Values=${name} Name=instance-state-name,Values=running \
--query 'Reservations[0].Instances[0]' --output json
}
terminate_ec2_instance_by_instance_id() {
local instance_id=$1
aws ec2 terminate-instances --instance-ids $instance_id --no-cli-pager
}
update_private_dns() {
local value=$1
local record_name=$2
aws route53 change-resource-record-sets --no-cli-pager --hosted-zone-id $HOSTED_ZONE_ID \
--change-batch file://<(cat << EOF
{
"Comment": "Update record to reflect private dns of ec2 instance",
"Changes": [
{
"Action": "UPSERT",
"ResourceRecordSet": {
"Name": "${record_name}",
"Type": "CNAME",
"TTL": 60,
"ResourceRecords": [
{
"Value": "${value}"
}
]
}
}
]
}
EOF
)
}
echo "get ec2 instance details: $instance_name"
instance_details=$(get_ec2_instance_by_ec2_instance_name $instance_name)
instance_id=$(echo $instance_details | jq -r '.InstanceId')
private_dns_name=$(echo $instance_details | jq -r '.PrivateDnsName')
echo "ec2 instance id: $instance_id, private dns name: $private_dns_name"
if [[ $should_terminate_ec2_instance == 'yes' ]]; then
echo "terminate ec2 ${instance_id}"
terminate_ec2_instance_by_instance_id $instance_id
echo "waiting for new ec2 instance provison"
sleep $sleep_after_terminate_ec2_instance
echo "get new ec2 instance details: $instance_name"
instance_details=$(get_ec2_instance_by_ec2_instance_name $instance_name)
instance_id=$(echo $instance_details | jq -r '.InstanceId')
private_dns_name=$(echo $instance_details | jq -r '.PrivateDnsName')
echo "new ec2 instance id: $instance_id, private dns name: $private_dns_name"
fi
echo "update private dns name ${private_dns_name}, record name ${record_name}"
update_private_dns $private_dns_name $record_name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment