【问题标题】:sending powershell script to Windows ec2 in user data在用户数据中向 Windows ec2 发送 powershell 脚本
【发布时间】:2014-04-14 09:17:42
【问题描述】:

我正在关注 these instructions 以使用 powershell 脚本(称为“bootstrap.ps1”)引导 Windows ec2 实例。但是,当我第一次登录机器时,脚本似乎从未运行过。我检查了 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-database64-decoded 提供的脚本与我编写的原始“bootstrap.ps1”相同(通过下载验证,然后在 Python 中通过base64.decodestring 运行)
  3. “boostrap.ps1”中的&lt;powershell&gt;&lt;/powershell&gt; 之前或之后分别没有字符。

显然,我在 user_data 中包含了 &lt;powershell&gt;&lt;/powershell&gt;。这些是 base-64 编码的,但不知何故,ec2config 找不到它们?谁能看到我在这里做错了什么?

【问题讨论】:

    标签: python windows powershell amazon-web-services amazon-ec2


    【解决方案1】:

    问题出在user_data的编码上,boto已经完成了。根据boto documentationuser_data 应该是“[t]Base64 编码的 MIME 用户数据可供此预留中的实例使用,”我发现这是非常具有误导性,因为编码不需要并且不应在调用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())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-14
      • 1970-01-01
      • 2017-12-18
      • 2015-10-28
      • 1970-01-01
      • 1970-01-01
      • 2013-12-24
      相关资源
      最近更新 更多