Monday, 3 December 2012

Adventures with Powershell and Amazon EC2 (part 1)

Managing servers isn’t fun. Setting up servers isn’t fun. I don’t want to do it any more, ever – it’s tedious, repetitive and error-prone, and, dammit, we have machines to do those kinds of jobs these days.

So, here’s what I’m trying to do. I want to be able to spin up a blank Windows instance on Amazon EC2 and deploy our apps onto it without ever logging into the box directly. This means no remote desktop sessions, no VNC, no SSH – nothing. These blog posts are as much a diary of work in progress as anything else… I’ll probably make mistakes and get things wrong along the way, but hopefully I’ll end up with something usable.

Step 1: Hello, World!

Prerequisites:

  1. An Amazon EC2 security group with firewall rules accepting all connections from your local network.
  2. A fresh Windows instance running in that security group, bound to an Amazon elastic IP
  3. The Windows Administrator username and password for that instance.

Create this script on your local workstation:

$remoteUsername = "Administrator"
$remotePassword = "your Amazon EC2 instance Administrator password"
$remoteHostname = "the elastic IP bound to your Amazon EC2 box"

$securePassword = ConvertTo-SecureString -AsPlainText -Force $remotePassword
$cred = New-Object System.Management.Automation.PSCredential $remoteUsername, $securePassword

Invoke-Command -ComputerName $remoteHostname -Credential $cred -ScriptBlock {
    Write-Host "Hello, World (from $env:COMPUTERNAME)"

}

Run it. You should get:

Invoking command on Remote host X.X.X.X
Hello, World (from AMAZONA-???????)

If that works, you’ve got Powershell remoting working against your Amazon EC2 instance. Hurrah!