Skip to content

Instantly share code, notes, and snippets.

@guitarrapc
Created February 3, 2015 06:28
Show Gist options
  • Save guitarrapc/76355bdb8223612431b1 to your computer and use it in GitHub Desktop.
Save guitarrapc/76355bdb8223612431b1 to your computer and use it in GitHub Desktop.
Amazon Route53 resource record set PowerShell
# Initialize
$resourceSubDomainName = "sample-subdomain"
$zoneName = "contoso.local"
$hostedZone = Get-R53HostedZones | where Name -eq $zoneName
# Set ResourceRecordSet
$resourceName = $resourceSubDomainName + "." + $zoneName
$resourceRecordSet = New-Object Amazon.Route53.Model.ResourceRecordSet
$resourceRecordSet.Name = $resourceName
$resourceRecordSet.Type = "A"
$resourceRecordSet.ResourceRecords = New-Object Amazon.Route53.Model.ResourceRecord ("192.168.0.100")
$resourceRecordSet.TTL = 300
$resourceRecordSet.Weight = 0
# Set Action
if (((Get-R53ResourceRecordSet -HostedZoneId $hostedZone.id).ResourceRecordSets | where Name -eq $resourceName | measure).Count -eq 0)
{
$action = [Amazon.Route53.ChangeAction]::CREATE
}
else
{
$action = [Amazon.Route53.ChangeAction]::UPSERT
}
# Set Change
$change = New-Object Amazon.Route53.Model.Change ($action, $resourceRecordSet)
# Execute
Edit-R53ResourceRecordSet -HostedZoneId $hostedZone.Id -ChangeBatch_Change $change
@venkatamutyala
Copy link

This script was very helpful! For some reason I was getting the error below but I just removed line #13 "$resourceRecordSet.Weight = 0" and I was able to get my record created in Route53.

Thank you!

Error I had:

image

@avvi00
Copy link

avvi00 commented Mar 1, 2016

Excellent - helped me too. thanks!

@goalautomation
Copy link

I get this error when running locally to test. any idea why?

"Get-R53ResourceRecordSet : Request object does not have required field HostedZoneId set"

@SpyderDave
Copy link

@goalautomation Did you ever figure out your error? I am getting the same issue.

@bjmaynard01
Copy link

Question...
I'm using part of your script in one I have to update numerous records, and for the most part, it's working. The only thing I'm running into is that the UPSERT is inserting an additional IP into a record set. Do you know how to remove this extra IP from a record with the AWS PowerShell module? I'm trying to use the Amazon.Route53.Model.Change.ResourceRecordSet.ResourceRecords.Remove method, but it's not working. I know I'm missing something from the docs, and hoping someone here can point me in the right direction.

@jmeldrum76
Copy link

This was amazing! I am so grateful you published this.

Thanks again!

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