Skip to content

Instantly share code, notes, and snippets.

@pahud
Last active August 17, 2016 08:54
Show Gist options
  • Save pahud/c3b90d5151c0471efa0fb018b4ff813b to your computer and use it in GitHub Desktop.
Save pahud/c3b90d5151c0471efa0fb018b4ff813b to your computer and use it in GitHub Desktop.
update_route53_on_ec2_launch
#!/bin/bash
# save this file as /root/update_route53.sh and run "bash /root/update_route53.sh" in the UserData
# Author: pahudnet@gmail.com 2016/08/10
publicIp=$(curl -s http://169.254.169.254/latest/meta-data/public-ipv4)
hostId='XXXXXXXXX'
cat > /tmp/update.json <<EOF
{
"Changes": [
{
"Action": "UPSERT",
"ResourceRecordSet": {
"Name": "myhost.example.com",
"Type": "A",
"TTL": 60,
"ResourceRecords": [
{
"Value": "${publicIp}"
}
]
}
}
]
}
EOF
aws route53 change-resource-record-sets --hosted-zone-id $hostId --change-batch file:////tmp/update.json
@pahud
Copy link
Author

pahud commented Aug 10, 2016

make sure the EC2 run as appropriate role with the following policy

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "Stmt1470806437000",
            "Effect": "Allow",
            "Action": [
                "route53:ChangeResourceRecordSets"
            ],
            "Resource": [
                "arn:aws:route53:::hostedzone/<YOUR_ZONE_ID>"
            ]
        }
    ]
}

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