AWS Cloudformation Userdata Powershell脚本未在自定义Windows Server AMI上运行

时间:2018-08-02 09:19:17

标签: amazon-web-services powershell amazon-cloudformation

我已经尝试了this question中的所有故障排除方法。

  <div className={list} onMouseOverCapture={e => this.catchMouseOver(e)}>
    //... some other divs that does not play any role in this example
    <div className={status1} title={'100%'}>
      <div className={security}>
        <i className={sprite} />
      </div>
    </div>
    <div className={status2} title={'20%'}>
      //... A lot of such divs with child divs like the first one `status1` div
    <div className={statusN} title={'0%'}>
  </div>

test.txt文件未在C:\ Temp文件夹中创建。

  

C:\ ProgramData \ Amazon \ EC2-Windows \ Launch \ Log \ UserdataExecution.log:

const getTooltipContainer = ({ target }) => {
  const aim = target.hasAttribute('title') && target
  console.log('target', aim ) // but it does not return div with `title` attribute, 
                              // only last max depper children div instead
}

但是我可以在"UserData": { "Fn::Base64": { "Fn::Join": [ "", [ "<powershell>\n", "New-Item C:\\Temp\\test.txt -ItemType file \n", "</powershell>\n", "<persist>true</persist>" ] ] } }

看到脚本
2018/01/19 02:26:55Z: Userdata execution begins
2018/01/19 02:26:55Z: Zero or more than one <persist> tag was not provided
2018/01/19 02:26:55Z: Unregistering the persist scheduled task
2018/01/19 02:27:18Z: Zero or more than one <runAsLocalSystem> tag was not provided
2018/01/19 02:27:18Z: Zero or more than one <script> tag was not provided
2018/01/19 02:27:19Z: Zero or more than one <powershellArguments> tag was not provided
2018/01/19 02:27:19Z: <powershell> tag was provided.. running powershell content
2018/01/19 07:49:57Z: Userdata execution begins
2018/01/19 07:49:57Z: Failed to get metadata: The result from http://169.254.169.254/latest/user-data was empty
2018/01/19 07:50:00Z: Unable to execute userdata: Userdata was not provided
2018/01/19 07:50:00Z: Userdata execution done

3 个答案:

答案 0 :(得分:0)

  
    

TLDR;通过在用于创建自定义AMI的实例上运行SysPrep,我的userdata脚本现在在首次启动时由以下用户执行:     云的形成。

  
     

使用EC2Launch设置应用程序运行Sysprep:

     
    
        
  1. 在Amazon EC2控制台中,找到或创建Windows Server 2016 AMI。
  2.     
  3. 从AMI启动Windows实例。
  4.     
  5. 连接到Windows实例并对其进行自定义。
  6.     
  7. 搜索并运行EC2LaunchSettings应用程序。它位于以下     目录默认为:C:\ProgramData\Amazon\EC2-Windows\Launch\Settings
  8.     
  9. 根据需要选择或清除选项。这些设置存储在LaunchConfig.json中     文件。     6.使用Sysprep选择关机。
  10.     
  
     

为Windows Server 2016配置Sysprep之后   可以安全地创建AMI的自定义映像。添加新创建的   AMI到CFN模板后执行成功。

     

参考文献:

     
    
        
  1. Microsoft System Preparation Tool configuration
  2.     
  3. More information on SysPrep
  4.     
  

答案 1 :(得分:0)

对于自定义AMI的用户数据,已经禁用以使其能够在powershell中按照以下命令运行。

C:\ ProgramData \ Amazon \ EC2-Windows \ Launch \ Scripts \ InitializeInstance.ps1 –计划

然后userdata脚本应该起作用。

答案 2 :(得分:0)

运行命令并获取 AMI

C:\ProgramData\Amazon\EC2-Windows\Launch\Scripts\InitializeInstance.ps1 –Schedule