简体   繁体   中英

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?

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())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM