Skip to content

Instantly share code, notes, and snippets.

@ts1
Created September 4, 2020 06:04
Show Gist options
  • Save ts1/28d6044500074324b1860bfcae2e3187 to your computer and use it in GitHub Desktop.
Save ts1/28d6044500074324b1860bfcae2e3187 to your computer and use it in GitHub Desktop.
Dynamic DNS update using AWS Lightsail Domain API
#!/bin/bash
set -eo pipefail
DOMAIN=example.com
ENTRY=examplehost
#LINE_TOKEN=xxxxxx
get_ip() {
curl -s 'https://api.ipify.org'
}
get_dns() {
aws lightsail get-domain --domain-name=$DOMAIN |
jq -r ".domain.domainEntries[] | select(.name == \"$ENTRY.$DOMAIN\") | .target"
}
get_id() {
aws lightsail get-domain --domain-name=$DOMAIN |
jq -r ".domain.domainEntries[] | select(.name == \"$ENTRY.$DOMAIN\") | .id"
}
update() {
local ID=$1
local IP=$2
aws lightsail update-domain-entry --domain-name $DOMAIN --domain-entry id=$ID,name=$ENTRY.$DOMAIN,type=A,target=$IP
}
IP=`get_ip`
DNS=`get_dns`
if [ "$IP" != "$DNS" ]; then
echo "Updating $ENTRY.$DOMAIN from $DNS to $IP"
ID=`get_id`
update $ID $IP
## Send a notification
#curl -H "Authorization: Bearer $LINE_TOKEN" \
# -d message="$ENTRY.$DOMAIN changed IP to $IP" \
# https://notify-api.line.me/api/notify
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment