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
}

AWS Windows AMI: How to make a new image after installing an .net update

Recently I found that my AWS EC2 instance performance degrades after applying windows updates. The reason is that I created the new AMI right away after Windows Update complete.

Now each time new instance started, I have the high CPU usage due to the .NET Runtime Optimization Services.

NetRuntime

The best practice here is to wait till this service done its job. In my environment it takes up to 30 minutes!

More http://blogs.msdn.com/b/davidnotario/archive/2005/04/27/412838.aspx

Schedule your Amazon EC2 spot instance startup time

Here I want to explain how to start your Amazon EC2 instance recurrently by scheduling.


I have used the AWS Tools for Windows PowerShell. You can use java command line or python library. The main goal is to explain the idea:

I am going to automate the starting of my Fitnesse server.

Create launch configuration

New-ASLaunchConfiguration Fitnesse -ImageId ami-00000000 -SecurityGroups Fitnesse -InstanceType t1.micro -SpotPrice 0.011

Create Auto Scaling group with MinSize=MaxSize=0

New-ASAutoScalingGroup Fitnesse-AS -LaunchConfigurationName Fitnesse -AvailabilityZones us-east-1a -MinSize 0 -MaxSize 0

Set the Start scheduling action
The core thing is the MinSize=MaxSize=1 and the -Recurrence argument which is in the cron scheduler time format

Write-ASScheduledUpdateGroupAction -AutoScalingGroupName Fitnesse-AS -ScheduledActionName Start -MinSize 1 -MaxSize 1 -Recurrence '15 13 * * *'

Optionl (stop event)

Write-ASScheduledUpdateGroupAction -AutoScalingGroupName Fitnesse-AS -ScheduledActionName Stop -MinSize 0 -MaxSize 0 -Recurrence '59 23 * * *'