问题描述
我正在尝试使用 AWS CDK 启动一个 ec2 实例,在大多数情况下它运行良好,但我希望 userData 保持不变,以便它在每次启动时运行......令人讨厌的是,这没有记录(我可以找到的任何地方)我就是不知道在哪里/如何定义它。下面是我的代码有效,但因为用户数据是由 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:
解决方法
// Instance details
const ssmaUserData = UserData.forWindows()
const userDataScript = '<script>' + '\n' + 'mkdir -p C:/helloworld' + '\n' + '</script>'+ '\n' +'<persist>true</persist>';
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: Fn.base64(userDataScript)
});
要在每次重启或启动实例时运行用户数据脚本,请将 true 添加到用户数据中。
<script>
echo Current date and time >> %SystemRoot%\Temp\test.log
echo %DATE% %TIME% >> %SystemRoot%\Temp\test.log
</script>
<persist>true</persist>
和Poweshell
<powershell>
$file = $env:SystemRoot + "\Temp\" + (Get-Date).ToString("MM-dd-yy-hh-mm")
New-Item $file -ItemType file
</powershell>
<persist>true</persist>
将一个或多个命令添加到脚本退出时将运行的用户数据中。
而且我认为 <persist>true</persist>
是一个标签而不是一个命令。
标签已提供:true – 如果找到了持久标签 在每次启动时运行 userdata – 如果找到了持久标签
,那个没有骰子:(
当我将其更改为以下内容时出现打字稿错误
const userDataScript = '<script>' + '\n' + 'mkdir -p C:/helloworld' + '\n' + '</script>'+ '\n' +'<persist>true</persist>';
const ec2Instance = new ec2.Instance(this,{
vpc,//,onePerAz: true},userData: cdk.Fn.base64(userDataScript)
});
错误:
Type 'string' is not assignable to type 'UserData | undefined'.ts(2322)
instance.d.ts(143,14): The expected type comes from property 'userData' which is declared here on type 'InstanceProps'
,
明白了!因此,每当我使用 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,userData: ssmaUserData,});