This script will update the A record of a Route53 hosted zone with either the IP of from another A record or a specific IP4 address.
A use case (but not limited to this) is updating the A record of an apex domain (e.g. example.com) to point to the public IP of a "DynDns" IP. Since Route53 does not support CNAME records for apex domains, this script will update the A record of the apex domain to point to the IP of another A record. This script is intended to be run as a cron job.
Credentials for AWS (with sufficient permissions) and the Hosted Zone ID of the domain to update are required.
To create AWS credentials that are limited to updating Route 53 records, you will need to:
- Create an IAM Policy with the required permissions.
- Create an IAM User and attach the policy to that user.
- Obtain the Access Key ID and Secret Access Key for the IAM User.
- Sign in to the AWS Management Console.
- Go to IAM (Identity and Access Management) .
- In the left navigation pane, click on Policies.
- Click the Create policy button.
- Switch to the JSON tab and paste the following JSON policy:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"route53:ChangeResourceRecordSets",
"route53:ListHostedZones",
"route53:ListResourceRecordSets"
],
"Resource": [
"arn:aws:route53:::hostedzone/YOUR_HOSTED_ZONE_ID"
]
}
]
}
Replace YOUR_HOSTED_ZONE_ID with the actual Hosted Zone ID for your domain.
- Click Review policy.
- Give your policy a Name (e.g., Route53UpdatePolicy) and add an optional Description.
- Click Create policy.
- In the IAM console, click on Users in the left navigation pane.
- Click the Add user button.
- Enter a User name (e.g., Route53Updater).
- For Access type, select Programmatic access.
- Click Next: Permissions.
- Select Attach existing policies directly.
- Search for the policy you created earlier (e.g., Route53UpdatePolicy) and select it.
- Click Next: Tags (you can optionally add tags) and then Next: Review.
- Review the settings and click Create user.
- After creating the user, you should see a success message with the user's Access Key ID and Secret Access Key.
- Download the .csv file containing these credentials or copy them to a secure location since this is the only time you'll be able to see the secret access key.
docker build -t route53-domain-updater .
Replace the environment variables with your actual values.
docker run --rm \
-e AWS_ACCESS_KEY_ID=MY_AWS_ACCESS_KEY_ID \
-e AWS_SECRET_ACCESS_KEY=MY_AWS_ACCESS_KEY_ID \
-e AWS_DEFAULT_REGION=MY_AWS_DEFAULT_REGION \
-e R53DU_HOSTED_ZONE_ID=MY_HOSTED_ZONE_ID \
-e R53DU_TARGET_A_RECORD=MY_APEX_DOMAIN \
-e R53DU_SOURCE_A_RECORD=MY_A_RECORD \
route53-domain-updater