# !/bin/bash
INSTANCE_ID=$(curl http://169.254.169.254/latest/meta-data/instance-id)
PRIVATE_IP=$(curl http://169.254.169.254/latest/meta-data/local-ipv4)
DOMAIN_NAME=$(aws route53 get-hosted-zone --id "<Hosted Zone ID >" --query 'HostedZone.Name' --output text | sed 's/.$//')
hostnamectl set-hostname hostname."${DOMAIN_NAME}"
CN=`echo $PRIVATE_IP | cut -d . -f 3`
echo $CN
a=5
if [ $CN == $a ]
then
aws route53 change-resource-record-sets --hosted-zone-id "<Hosted Zone ID >" --change-batch '{"Changes": [{"Action": "UPSERT","ResourceRecordSet": {"Name": "'"Dns Name"'","Type": "A","TTL": 60,"ResourceRecords": [{"Value": "'"${PRIVATE_IP}"'"}]}}]}'
else
aws route53 change-resource-record-sets --hosted-zone-id "<Hosted Zone ID >" --change-batch '{"Changes": [{"Action": "UPSERT","ResourceRecordSet": {"Name": "'"< Dns Name>"'","Type": "A","TTL": 60,"ResourceRecords": [{"Value": "'"${PRIVATE_IP}"'"}]}}]}'
fi