Skip to content

Instantly share code, notes, and snippets.

@martinjt
Created August 3, 2017 16:04
Show Gist options
  • Save martinjt/a156c2cf38d6e53128ff858605fadf59 to your computer and use it in GitHub Desktop.
Save martinjt/a156c2cf38d6e53128ff858605fadf59 to your computer and use it in GitHub Desktop.
Updates a route53 entry with the public hostname of the current EC2 instance. Usage: `Update-R53ForCurrentHost "servers.example.com" "s01"`
function Update-R53ForCurrentHost {
param($zoneName, $subdomain)
Import-Module AWSPowershell
Write-Output "Updating R53 record for $subdomain.$zoneName"
$publicHostname = Invoke-WebRequest `
-Uri http://169.254.169.254/latest/meta-data/public-hostname
Write-Output "New HostName is:$publicHostname"
$hostedZone = Get-R53HostedZones | Where-Object Name -eq ($zoneName + ".")
# Set ResourceRecordSet
$resourceName = $subdomain + "." + $zoneName + "."
$resourceRecordSet = New-Object Amazon.Route53.Model.ResourceRecordSet
$resourceRecordSet.Name = $resourceName
$resourceRecordSet.Type = "CNAME"
$resourceRecordSet.ResourceRecords = New-Object Amazon.Route53.Model.ResourceRecord ($publicHostname)
$resourceRecordSet.TTL = 300
# Set Action
Write-Output "Checking for status of: $resourceName"
if (((Get-R53ResourceRecordSet -HostedZoneId $hostedZone.id).ResourceRecordSets | Where-Object Name -eq $resourceName | Measure-Object).Count -eq 0) {
Write-Output "Domain does not exist... Creating new domain"
$action = [Amazon.Route53.ChangeAction]::CREATE
}
else {
Write-Output "Domain exists... Updating domain"
$action = [Amazon.Route53.ChangeAction]::UPSERT
}
# Set Change
$change = New-Object Amazon.Route53.Model.Change ($action, $resourceRecordSet)
# Execute
Write-Output "Updating...."
Edit-R53ResourceRecordSet -HostedZoneId $hostedZone.Id -ChangeBatch_Change $change
Write-Output "Update Done"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment