在用户数据中发送PowerShell脚本到Windows ec2

我按照这些说明使用powershell脚本引导Windows ec2实例(称之为“bootstrap.ps1”)。 但是,当我第一次login机器时,看起来脚本从未运行过。 我检查C:\ Program Files \ Amazon \ Ec2ConfigService \ Logs \ Ec2ConfigLog,我看到这个:

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> 

这是我的脚本看起来像:

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

我是用Python编写脚本的base64编码,并通过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())) 

我确信:

  1. 我发送的脚本(“bootstrap.ps1”)使用\ r \ n作为行尾
  2. http://169.254.169.254/latest/user-data base64解码的脚本与我编写的原始“bootstrap.ps1”相同(通过下载validation,然后通过Python中的base64.decodestring运行)
  3. “boostrap.ps1”中的<powershell></powershell>之前或之后没有字符。

显然,我在user_data中包含<powershell></powershell> 。 这些是编码的base-64,但不知何故,他们不被ec2config发现? 任何人都可以看到我在这里做错了吗?

问题在于已经由boto执行的user_data的编码。 根据boto文档 , user_data应该是“ Base64编码的MIME用户数据,以供该预留中的实例使用 ”,我发现这是非常具有误导性的,因为编码不需要也不应该不要run_instances的调用中run_instances 。 下面的Python现在适用于我:

 # create connection... conn = ... # run instance with open("bootstrap.ps1", "r") as fd: # don't encode user_data conn.run_instances(..., user_data=fd.read())