Skip to content

Instantly share code, notes, and snippets.

@heiwa4126
Created December 15, 2020 05:54
Show Gist options
  • Save heiwa4126/57831f4a3607de798a116eea5ac49298 to your computer and use it in GitHub Desktop.
Save heiwa4126/57831f4a3607de798a116eea5ac49298 to your computer and use it in GitHub Desktop.
ElasticIPなしのEC2で外部IPをroute53でFQDNをふるスクリプト
#!/bin/bash
# see [Amazon Route 53: How to automatically update IP addresses without using Elastic IPs - DEV](https://dev.to/aws/amazon-route-53-how-to-automatically-update-ip-addresses-without-using-elastic-ips-h7o)
AWS=/usr/local/bin/aws
# Extract information about the Instance
INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id/)
AZ=$(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone/)
MY_IP=$(curl -s http://169.254.169.254/latest/meta-data/public-ipv4/)
# Extract tags associated with instance
ZONE_TAG=$($AWS ec2 describe-tags --region ${AZ::-1} --filters "Name=resource-id,Values=${INSTANCE_ID}" --query 'Tags[?Key==`AUTO_DNS_ZONE`].Value' --output text)
NAME_TAG=$($AWS ec2 describe-tags --region ${AZ::-1} --filters "Name=resource-id,Values=${INSTANCE_ID}" --query 'Tags[?Key==`AUTO_DNS_NAME`].Value' --output text)
# DEBUG
cat <<EOF
INSTANCE_ID = $INSTANCE_ID
AZ = $AZ
MY_IP = $MY_IP
ZONE_TAG = $ZONE_TAG
NAME_TAG = $NAME_TAG
EOF
# Update Route 53 Record Set based on the Name tag to the current Public IP address of the Instance
$AWS route53 change-resource-record-sets --hosted-zone-id $ZONE_TAG --change-batch '{"Changes":[{"Action":"UPSERT","ResourceRecordSet":{"Name":"'$NAME_TAG'","Type":"A","TTL":300,"ResourceRecords":[{"Value":"'$MY_IP'"}]}}]}'
@heiwa4126
Copy link
Author

オリジナルはAmazon Route 53: How to automatically update IP addresses without using Elastic IPs - DEVを、ちょっとだけアレンジ。EC2数台あるので、curlでとりやすいようにgistに置いてみた。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment