With Linux instances, the public key of the key pair you specify when creating the instance is placed in the .ssh/authorized_keys directory. When you SSH in it encrypts the initial communication details with your public key so that only someone with the corresponding private key can decrypt the details and complete the connection.
Windows instances do not work in the same way. Instead when the instance is created a random password is created. This password is then encrypted with the public key. You can request this encrypted password but you then need the private key to decrypt it. This can be done through the AWS console but if you are going to use boto to automate AWS then you really want a Python solution.
I have seen a couple of solutions using the PyCrypto module but I wanted a pure Python solution. Luckily there is an rsa module (pip install rsa) which is written in pure Python. With that and the boto module you can decrypt the password with the following code.
import rsa, boto, base64 instance_id = 'i-0123456789abcdef' key_path = r'C:\path\to\private.pem' ec2 = boto.connect_ec2() #access_key,secret_key passwd = base64.b64decode(ec2.get_password_data(instance_id)) if (passwd): with open (key_path,'r') as privkeyfile: priv = rsa.PrivateKey.load_pkcs1(privkeyfile.read()) key = rsa.decrypt(passwd,priv) else: key = 'Wait at least 4 minutes after creation before the admin password is available' print(key)
You just need to specify the instance_id and the private key file location (2nd and 3rd lines). The connect_ec2 method will use the credentials in the .aws file in your home directory if it exists. Alternatively you can specify the access key and secret key as parameters to the method. Assuming you haven’t jumped the gun this should print out the admin password.