紀錄一下怎麼更新DNS Record on Route 53

Step 1: Get HOSTED_ZONE_ID on route 53

Just open the console of Route 53 and copy the HOSTED_ZONE_ID.

Step 2: Get server ip

$ wget http://ipinfo.io/ip -qO -

Step 3: Create updated record

updated.json

{
  "Comment": "Update the A record set",
  "Changes": [
    {
      "Action": "UPSERT",
      "ResourceRecordSet": {
        "Name": "nfs.aws.com.",
        "Type": "A",
        "TTL": 60,
        "ResourceRecords": [
          {
            "Value": "127.0.0.1"
          }
        ]
      }
    }
  ]
}

P.S If u need to get the ordinary record u can use following command

$ aws route53 list-resource-record-sets --hosted-zone-id 'xxxxxxxxx'

Step 4: Using sed command to replace ip

updateDNS.sh

#!/bin/bash
HOSTED_ZONE_ID='xxxxxxxxxx';
#echo $HOSTED_ZONE_ID;
IP=`wget http://ipinfo.io/ip -qO -`;
#echo $IP;
INPUT_JSON=$( cat ./nfs.hxcld.com.json | sed "s/127\.0\.0\.1/$IP/" );
echo $INPUT_JSON;

You can echo the $INPUT_JSON to see the result.

Step 5: Update record

#!/bin/bash
HOSTED_ZONE_ID='ZFJGL656RIZOU';
echo $HOSTED_ZONE_ID;
IP=`wget http://ipinfo.io/ip -qO -`;
echo $IP;
INPUT_JSON=$( cat ./nfs.hxcld.com.json | sed "s/127\.0\.0\.1/$IP/" );
echo $INPUT_JSON;
INPUT_JSON="{ \"ChangeBatch\": $INPUT_JSON }"
aws route53 change-resource-record-sets --hosted-zone-id "$HOSTED_ZONE_ID" --cli-input-json "$INPUT_JSON"