PowerShell: Register AWS EC2 instances in AWS R53

UPDATE: #PowerShell: Register AWS EC2 instances in Amazon Route 53 (new way)

Suppose you have a variable $lab:

$lab = @{id='ami-7300091a'; name='1.dc.mylab.com'; type='m1.small'; maxbid='0.02'; CNAME='ec2-54-000-117-7.compute-1.amazonaws.com'},
@{id='ami-49000e20'; name='1.sql.mylab.com'; type='cc1.4xlarge'; maxbid='0.09'; CNAME='ec2-184-73-000-182.compute-1.amazonaws.com'}

Let’s update AWS R53 entries by using instances CNAME:

$zoneId = 'ZB00X5FI00YBB'
foreach($computer in $lab){
  "[$($computer.name)]`t[R53] update... " | Out-Default
  $result = Get-R53ResourceRecordSet -HostedZoneId $zoneId -StartRecordName $computer.name -MaxItems 1
  $rs = $result.ResourceRecordSets[0]
  if($rs.Name -eq "$($computer.name).") {
    "[$($computer.name)]`t[R53] entry found" | Out-Default
    "[$($computer.name)]`t[R53] delete entry" | Out-Default
    $action = (new-object Amazon.Route53.Model.Change).WithAction('DELETE').WithResourceRecordSet($rs)
    Edit-R53ResourceRecordSet -HostedZoneId $zoneId -ChangeBatch_Changes $action | Out-Null
  } else { "[$($computer.name)]`t[R53] entry not found (returned entry is for $($rs.Name))" | Out-Default }
  "[$($computer.name)]`t[R53] create entry" | Out-Default
  $record = (new-object Amazon.Route53.Model.ResourceRecord).WithValue($computer.CNAME)
  $rs = (new-object Amazon.Route53.Model.ResourceRecordSet).WithName($computer.name).WithType('CNAME').WithTTL('10').WithResourceRecords($record)
  $action = (new-object Amazon.Route53.Model.Change).WithAction('CREATE').WithResourceRecordSet($rs)
  Edit-R53ResourceRecordSet -HostedZoneId $zoneId -ChangeBatch_Changes $action | Out-Null
}
Advertisements

3 thoughts on “PowerShell: Register AWS EC2 instances in AWS R53

  1. Thank you for sharing this with the Community.. However, After several days I am unable to get it working. I keep getting the same error.

    Method invocation failed because [Amazon.Route53.Model.Change] does not contain a method named ‘WithAction’.

    It also does this when creating the ‘other’ object types… For example:

    Method invocation failed because [Amazon.Route53.Model.ResourceRecordSet] does not contain a method named ‘WithName’.

    Any idea why this might be happening?

    Thanks!@

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s