Justin Latimer

ec2

EC2 Dynamic DNS with dnsimple and Powershell

I've been wanting to do some native development on Windows, and it's actually been quite a while since I've had a Windows machine in my house. So, to the cloud! AWS lets you rent machines by the hour, with a service called EC2.

The new T2 instance types seem to be just what is needed for this project - they've got a cool system while when the box is idling, they build up CPU credits, which can then be used when CPU bursts are needed. Because most of the time the machine will be idling while I'm typing and thinking, it will build up credits and compiling will burn through them.

I don't want to leave the machine running when I'm not using it, and I don't want to pay the extra for an Elastic IP - I'm trying to keep this as low cost as possible. This means that the public IP changes every time the machine starts, which is a minor inconvenience, I wanted to assign a subdomain to this machine to make RDPing easier. I host my DNS with dnsimple and they have an API. So I whipped up a Powershell script which runs on boot that uses the dnsimple API to update the DNS for the machine.

$ip = Invoke-RestMethod -Method Get -Uri "http://icanhazip.com/"
$body = @{ record = @{ content = $ip } } | ConvertTo-Json -Compress
Invoke-RestMethod -Method Put `
    -ContentType "application/json" `
    -Headers @{
        "X-DNSimple-Domain-Token" = "YOU_TOKEN_HERE";
     } `
    -Uri "https://api.dnsimple.com/v1/domains/YOUR_DOMAIN_HERE/records/YOU_RECORD_ID_HERE" `
    -Body $body

I've got it running from an Windows Scheduled Task that runs on startup. You've got to make sure the task runs as "Network Service" and set it to run before a user logs on. I also configured it to try 3 times if it fails, just in case the network takes a bit to come up. Works really well! Here are some pictures of the Scheduled Task configuration for reference.

DNS Scheduled Task Security Settings

DNS Scheduled Task Trigger Settings

DNS Scheduled Task Actions Settings

DNS Scheduled Task Settings

Tags: