Skip to content

Instantly share code, notes, and snippets.

@chusiang
Created November 14, 2016 08:01
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 chusiang/36331e4243d775e9876bdb822022d477 to your computer and use it in GitHub Desktop.
Save chusiang/36331e4243d775e9876bdb822022d477 to your computer and use it in GitHub Desktop.
Quick check domain address and ec2 public ip.
#!/bin/bash
# ============================================================
# Author: Chu-Siang Lai / chusiang (at) drx.tw
# Filename: route53-ec2-ip-match.sh
# Modified: 2016-11-14 15:31
# Description: Quick check domain address and ec2 public ip.
#
# - It's need install the `jq` package.
#
# Reference:
#
# 1. describe-instances — AWS CLI 1.11.14 Command Reference
# - http://docs.aws.amazon.com/cli/latest/reference/ec2/describe-instances.html
# 2. list-resource-record-sets — AWS CLI 1.11.14 Command Reference
# - http://docs.aws.amazon.com/cli/latest/reference/route53/list-resource-record-sets.html
# 3. 玩具烏托邦: json 轉檔萬用瑞士刀 jq
# - http://newtoypia.blogspot.tw/2015/03/json-jq.html
#
# ===========================================================
## Route 53
#
HOSTED_ZONE_ID="<Your Hosted Zone ID>"
# example: note.drx.tw.
DOMAIN_NAME="<Your Domain Name>"
# Get DOMAIN_ADDRESS with awscli.
DOMAIN_ADDRESS=$(aws route53 list-resource-record-sets --hosted-zone-id $HOSTED_ZONE_ID --query "ResourceRecordSets[?Name == '$DOMAIN_NAME']" | jq "map( .ResourceRecords[].Value)")
## EC2
#
INSTANCE_NAME="<Your Instance Name>"
# Get EC2_PUBLIC_IP with awscli.
EC2_PUBLIC_IP=$(aws ec2 describe-instances --filters "Name=tag:Name,Values=$INSTANCE_NAME" --query "Reservations[].Instances[].PublicIpAddress" | jq .)
## Main
#
if [ "$EC2_PUBLIC_IP" == "$DOMAIN_ADDRESS" ]; then
# filter the char of '"'.
IP_ADDRESS=`echo $EC2_PUBLIC_IP | awk -F '\"' '{ print $2 }'`
echo "[ok] The 'DNS Record' and 'EC2 Public IP Address' has be matched."
echo
echo "- Domain name: $DOMAIN_NAME"
echo "- IP Address: $IP_ADDRESS"
else
echo "[warn] It's not matched."
echo
echo "EC2_PUBLIC_IP = $EC2_PUBLIC_IP"
echo "DOMAIN_ADDRESS = $DOMAIN_ADDRESS"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment