Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sending powershell script to Windows ec2 in user data

I'm following these instructions to bootstrap a Windows ec2 instance with a powershell script (call it "bootstrap.ps1"). However, it looks as if the script has never run when I log into the machine for the first time. I check C:\Program Files\Amazon\Ec2ConfigService\Logs\Ec2ConfigLog, and I see this:

2014-03-11 00:08:02: Ec2HandleUserData: Start running user scripts
2014-03-11 00:08:02: Ec2HandleUserData: Could not find <script> and </script>
2014-03-11 00:08:02: Ec2HandleUserData: Could not find <powershell> and </powershell>

This is what my script looks like:

<powershell>
(multiple lines of powershell script here)
</powershell>

I'm base64-encoding the script in Python and sending it through boto:

import base64
# (create connection to aws region in boto called 'conn')
conn = ...
# run the instance and provide base64-encoded bootstrap powershell script in user_data
with open("bootstrap.ps1", "r") as fd:
  conn.run_instances(..., user_data=base64.encodestring(fd.read()))

I've made sure that:

  1. The script I'm sending ("bootstrap.ps1") is using \r\n as line endings
  2. The script available at http://169.254.169.254/latest/user-data base64-decoded is the same as the raw "bootstrap.ps1" I've written (verified by downloading it, then running through base64.decodestring in Python)
  3. There are no characters before or after <powershell> and </powershell> in "boostrap.ps1", respectively.

Clearly, I'm including <powershell> and </powershell> in user_data. These are encoded base-64, yet somehow they're not being found by ec2config? Can anyone see what I'm doing wrong here?

like image 486
llovett Avatar asked Mar 11 '14 00:03

llovett


People also ask

How do I connect to an EC2 instance in Windows PowerShell?

Create new terminal connection (use Putty plugin) In “Credentials” step, under Credential tab, select “Specify username and password”; then type the username specified with the EC2 connection (in my case ubuntu) Under “Private Key File” Choose “Embed Private Key File” and select the path to the key. pem file.

How do I transfer files from Windows to EC2 instance?

Open a new command prompt and run the following command replacing the fields as needed: scp -P 2222 Source-File-Path user-fqdn @localhost: To copy the entire directory instead of a file, use scp -r before the path. This recursively copies all of the directory's contents to the destination EC2 instance.


1 Answers

The problem lies in the encoding of user_data, which is already performed by boto. According to the boto documentation, user_data should be "[t]he Base64-encoded MIME user data to be made available to the instance(s) in this reservation," which I find to be very misleading, since the encoding need not and should not be done in the call to run_instances. The following Python works for me now:

# create connection...
conn = ...
# run instance
with open("bootstrap.ps1", "r") as fd:
  # don't encode user_data
  conn.run_instances(..., user_data=fd.read())
like image 172
llovett Avatar answered Oct 02 '22 10:10

llovett