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

问题描述 投票:0回答:3

我正在尝试使用 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: 
typescript amazon-web-services aws-cdk infrastructure
3个回答
4
投票

明白了!因此,每当我使用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,
});

0
投票

为了避免将所有脚本写入字符串中,您可以使用提供的方法,特别是如果您想做 S3 下载之类的事情。

完成用户数据后,只需获取脚本并附加标志即可。

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

    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)

0
投票

您是否尝试过添加 userDataCausesReplacement: true 我之前也遇到过同样的问题,添加 userDataCausesReplacement 就可以了。

// 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,
  userDataCausesReplacement: true
});
© www.soinside.com 2019 - 2024. All rights reserved.