gpt4 book ai didi

powershell - 在 CloudFormation 模板中运行 PowerShell 命令

转载 作者:行者123 更新时间:2023-12-02 09:32:04 24 4
gpt4 key购买 nike

我有一个 CloudFormation 模板,它启动 Windows 实例,并将 Java 和 Tomcat 作为服务运行。我必须在注册表中设置 Tomcat 的最大内存池大小的值。我准备了一个 PowerShell 命令,它在命令行中运行得很好。然后我尝试了 3 种不同的方法来将它们作为 CloudFormation 模板的一部分来运行,但这些方法都不起作用。

Windows EventLog 和 C:\Program Files\Amazon\Ec2ConfigService\Logs 中的文件都没有向我提供任何关于它不起作用的原因的线索。

1

"LaunchConfig" : {
"Type" : "AWS::AutoScaling::LaunchConfiguration",
"Properties" : {
--- some props removed ---,
"UserData" : {
"Fn::Base64" : {
"Fn::Join" : [
"",
[
"<powershell>\n",
"Set-ItemProperty -Path \"HKLM:\\Software\\Wow6432Node\\Apache Software Foundation\\Procrun 2.0\\Tomcat8\\Parameters\\Java\" -Name JvmMx -Value 5000 \n",
"Restart-Service Tomcat8 \n",
"</powershell>"
]
]
}
}
}
}

2

"LaunchConfig" : {
"Type" : "AWS::AutoScaling::LaunchConfiguration",
"Properties" : {
--- some props removed ---,
"UserData" : {
"Fn::Base64" : {
"Fn::Join" : [
"",
[
"<script>\n",
"PowerShell -Command \"& {Set-ItemProperty -Path 'HKLM:\\Software\\Wow6432Node\\Apache Software Foundation\\Procrun 2.0\\Tomcat8\\Parameters\\Java' -Name JvmMx -Value 5000}\" \n",
"PowerShell -Command \"& {Restart-Service Tomcat8}\" \n",
"</script>"
]
]
}
}
}
}

3

"LaunchConfig" : {
"Type" : "AWS::AutoScaling::LaunchConfiguration",
"Properties" : {
--- some props removed ---,
"UserData" : {
"Fn::Base64" : {
"Fn::Join" : [
"",
[
"<script>\n",
"cfn-init.exe -v -s ", {"Ref" : "AWS::StackId"}, " -r LaunchConfig --region ", {"Ref" : "AWS::Region"}, "\n",
"</script>"
]
]
}
}
},
"Metadata" : {
"AWS::CloudFormation::Init" : {
"config" : {
"commands" : {
"1-heapsize" : {
"command" : "PowerShell -Command \"& {Set-ItemProperty -Path 'HKLM:\\Software\\Wow6432Node\\Apache Software Foundation\\Procrun 2.0\\Tomcat8\\Parameters\\Java' -Name JvmMx -Value 5000}\""
},
"2-restart" : {
"command" : "PowerShell -Command \"& {Restart-Service Tomcat8}\""
}
}
}
}
}
}

最佳答案

必须启用 EC2 实例中的用户数据才能应用/运行 cfn-init 和其他用户数据注入(inject)脚本。

您需要打开 EC2ConfigurationService - 检查启用用户数据,停止实例并获取 AMI,并请使用新创建的 AMI。

enter image description here

详细信息可以在亚马逊文档链接中找到 - http://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/UsingConfig_WinAMI.html

关于powershell - 在 CloudFormation 模板中运行 PowerShell 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31939327/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com