Renaming an EC2 Windows instance and customizing its wallpaper with tags using PowerShell
I decided to give blogging a shot because I think I have some potentially interesting patterns in AWS automation using Ansible to share and get feedback on. If you know me, you know how much of a Linux and Mac OS X guy I am, and thus how ironic it is that one of my early posts should include PowerShell work!
Historically, I've mostly worked with Linux, but at my current position, my role is very general, and the reality is that many applications used in the life sciences industry run on Windows. At work, we're increasingly using AWS, even for Windows workloads, and I'm the "AWS guy", so I'm making an effort to bring my Windows administration and especially automation skills up to par.
We use a system naming scheme where hostnames are randomly assigned to VMware guests and AWS instances. The hostnames are explicitly not based on the service offered by the hosts. A service name is generally a DNS CNAME
pointing at the service-offering host. The hostname is just a human identifier to make it easy to talk about a host. In the AWS case, it allows my team to not have to talk about instance IDs or IP addresses, but instead a plain English word.
We use EC2 tags to add metadata to instances. These include the sort of information that some organizations embed in their hostnames. I've generally been adamant that hostnames are not storage devices (I feel that leads to servers-as-pets instead of servers-as-cattle), but are just identifiers. In our case, the important tags are
Name
: the hostnameEnvironment
:production
,staging
,test
, orhpc
Function
: generally the same as the service name, likeokta-agent
orspotfire
VPC
: the name of the VPC where the instance resides, since we have several (this is really a convenience for AWS Console display, where otherwise only the VPC ID, an identifier likevpc-deadbeef
would be available)
For some time, I've had configuration management code in place for our Linux instances that would set their hostname based on the Name
tag and put the other tags, nicely formatted, in /etc/motd
. This week, I took the time to bring our Windows provisioning process up to this level.
This is the first PowerShell I've written of any complexity, so I'm looking forward to any feedback. It is supplied to the instance as user data, so EC2Config
(which is sort of like cloud-init
for Windows) will run it after launch. What does it do?
- gets the instance ID from the local metadata web service
- rewrites the configuration file for the wallpaper customization program to include our tags (this is all the XML manipulation you see)
- renames the computer using the value of the instance's
Name
tag - restarts the computer (this is Windows -- you've got to reboot on a hostname change)
Note that Rename-Computer
works just fine if the instance is joined to an Active Directory domain. I'll write about how we use AWS' domain join functionality with their Directory Service in a future post.