AWS CDK ec2.Instance userData.... 不持久 :(

AWS CDK ec2.Instance userData.... not persisting :(

我正在尝试使用 AWS CDK 启动一个 ec2 实例,在大多数情况下它工作得很好,但我希望 userData 能够持续存在,以便它在每次启动时运行......令人讨厌的是,这没有记录(我可以在任何地方find) 而我只是想不出 where/how 来定义它。下面是我的代码,但因为用户数据是 forWindows() 我不能只添加 xxx.addCommands('<persist>true</persist>') 因为 forWindows() 将代码放在标签中...

// Instance details
const ssmaUserData = UserData.forWindows()
ssmaUserData.addCommands('mkdir -p C:/helloworld; ');

const ec2Instance = new ec2.Instance(this, 'SdkInstance', {
  vpc,
  instanceType: ec2.InstanceType.of(ec2.InstanceClass.T3, ec2.InstanceSize.NANO),
  machineImage: awsAMI,
  securityGroup: mySecurityGroup,
  vpcSubnets: {subnetType: ec2.SubnetType.PUBLIC},
  keyName: "EC2Connect",
  userData: ssmaUserData
});

我尝试使用 ssmaUserData.addOnExitCommands("<persist>true</persist>") 及其变体,但没有成功,有人知道如何完成吗?

以下是表明这不是运行持久性的日志...

2021/03/11 12:56:51Z: Userdata execution begins
2021/03/11 12:56:51Z: Zero or more than one <persist> tag was not provided
2021/03/11 12:56:51Z: Unregistering the persist scheduled task
2021/03/11 12:56:55Z: Zero or more than one <runAsLocalSystem> tag was not provided
2021/03/11 12:56:55Z: Zero or more than one <script> tag was not provided
2021/03/11 12:56:55Z: Zero or more than one <powershellArguments> tag was not provided
2021/03/11 12:56:55Z: <powershell> tag was provided.. running powershell content
2021/03/11 13:08:34Z: Userdata execution begins
2021/03/11 13:08:34Z: Zero or more than one <persist> tag was not provided
2021/03/11 13:08:34Z: Unregistering the persist scheduled task
2021/03/11 13:08:37Z: Zero or more than one <runAsLocalSystem> tag was not provided
2021/03/11 13:08:37Z: Zero or more than one <script> tag was not provided
2021/03/11 13:08:37Z: Zero or more than one <powershellArguments> tag was not provided
2021/03/11 13:08:37Z: <powershell> tag was provided.. running powershell content
2021/03/11 13:08:42Z: Message: The output from user scripts: 

知道了!因此,每当我使用 AWS 详细记录的 UserData.forWindows() 时,它会自动添加 <PowerShell> 标签,这意味着如果我定义 <Persist> 它将包含标签......为了解决这个问题我需要改用 UserData.custom() 。我已经测试了下面的代码,它运行良好!

const script = `
<powershell>
  Start-Transcript -OutputDirectory C:/
  Write-Output HelloWorld
  Stop-Transcript
</powershell>
<persist>true</persist>
`;

const ssmaUserData = UserData.custom(script)

const ec2Instance = new ec2.Instance(this, 'SdkInstance', {
  vpc,
  instanceType: ec2.InstanceType.of(ec2.InstanceClass.T3, ec2.InstanceSize.NANO),
  machineImage: awsAMI,
  securityGroup: mySecurityGroup,
  vpcSubnets: {subnetType: ec2.SubnetType.PUBLIC},
  keyName: "EC2Connect",
  userData: ssmaUserData,
});

为避免将所有脚本都写在一个字符串中,您可以使用提供的方法,特别是如果您想执行 S3 下载等操作。

完成 UserData 后,只需获取脚本并附加标志。

示例(在 python 中,但可以在打字稿中以相同的方式完成):

    instance_userdata = ec2.UserData.for_windows()
    #... do lots os actions like: instance_userdata.add_s3_download_command(...)

    data_script = instance_userdata.render()
    #https://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/ec2-windows-user-data.html#user-data-execution
    data_script += "<persist>true</persist>"
    persistent_userdata = ec2.UserData.custom(data_script)