Cloudformation - 如何在Windows 2012实例上安装cfn帮手脚本
我有一个自定义的Windows 2012服务器的AMI映像。我想推出一些服务,并在该实例出现时使用PowerShell脚本做一些额外的工作。顺便说一下,我正在使用AWS cloudformation模板来调出实例。Cloudformation - 如何在Windows 2012实例上安装cfn帮手脚本
在EC2实例的AMI之前(根据Steve的建议),我在EC2Config设置中检查了“user data”选项并运行了sysops。运行syops之后,它自行关闭。我在那个阶段参加了AMI。
我从我的云形成模板调用的powershell脚本不起作用。我不知道为什么。
{
"AWSTemplateFormatVersion": "2010-09-09",
"Resources": {
"MyInstance": {
"Type": "AWS::EC2::Instance",
"Metadata" : {
"AWS::CloudFormation::Init" : {
"config" : {
"files" : {
"c:\\cfn\\cfn-hup.conf" : {
"content" : { "Fn::Join" : ["", [
"[main]\n",
"stack=", { "Ref" : "AWS::StackId" }, "\n",
"region=", { "Ref" : "AWS::Region" }, "\n"
]]}
},
"c:\\cfn\\hooks.d\\cfn-auto-reloader.conf" : {
"content": { "Fn::Join" : ["", [
"[cfn-auto-reloader-hook]\n",
"triggers=post.update\n",
"path=Resources.MyInstance.Metadata.AWS::CloudFormation::Init\n",
"action=cfn-init.exe -v -s ", { "Ref" : "AWS::StackId" },
" -r MyInstance",
" --region ", { "Ref" : "AWS::Region" }, "\n"
]]}
},
"c:\\scripts\\test.ps1" : {
"content": { "Fn::Join" : ["", [
"Write-Host Hello World!\n"
]]}
}
},
"commands" : {
"1-run-script" : {
"command" : { "Fn::Join" : [ "", [
"Powershell.exe Set-ExecutionPolicy Unrestricted -force \n",
"Powershell.exe C:\\PowershellScripts\\WindowsServiceManager.ps1;StopWindowsService Dnscache"
]]}}
},
"services": {
"windows": {
"cfn-hup": {
"enabled": "true",
"ensureRunning": "true",
"files": ["c:\\cfn\\cfn-hup.conf", "c:\\cfn\\hooks.d\\cfn-auto-reloader.conf"]
}
}
}
}
}
},
"Properties": {
"DisableApiTermination": "FALSE",
"ImageId": "ami-3723c04f",
"InstanceType": "t2.micro",
"KeyName": "EC2Instances",
"Monitoring": "false",
"UserData" : { "Fn::Base64" : { "Fn::Join" : ["", [
"<script>\n",
"cfn-init.exe -v -s ", { "Ref" : "AWS::StackName" },
" -r MyInstance",
" --region ", { "Ref" : "AWS::Region" }, "\n",
"cfn-signal.exe -e 0 ", { "Fn::Base64" : { "Ref" : "WindowsServerWaitHandle" }}, "\n",
"</script>\n"
]]}},
"Tags": [
{
"Key": "Name",
"Value": "CloudAcademy_Instance"
}
],
"NetworkInterfaces": [
{
"DeleteOnTermination": "true",
"Description": "Primary network interface",
"DeviceIndex": 0,
"AssociatePublicIpAddress": "true"
}
]
}
},
All I need is the following line to get executed:
"Powershell.exe C:\\PowershellScripts \\WindowsServiceManager.ps1;StopWindowsService Dnscache"
感谢
如果您使用的是亚马逊的Windows 2012 AMI,那么它将有cfn- *已经安装的辅助脚本。
如果没有,那么你需要安装它们。 official documentation在细节上很薄:
这些脚本默认安装在/ opt/aws/bin中的最新Amazon Linux AMI上。它们也可以在亚马逊Linux AMI yum存储库中用于以前版本的Amazon Linux AMI,也可以通过RPM用于其他Linux/Unix发行版。您也可以使用Python for Windows在Microsoft Windows(2008或更高版本)上安装脚本。
但是你可以用EC2ConfigService安装它们。需要注意的是,EC2ConfigService中的cfn脚本may be disabled:
在创建Windows AMI之前,请单击“开始”并下移到“E”。打开Ec2Config设置。启用“用户数据”执行。如果您要设置管理员密码,请选择中间的单选按钮并提供密码。然后“用sysprep关机”。您将在sysprep期间看到它已启用userdata执行。一旦机器停机 - 创建一个AMI。
谢谢史蒂夫。它现在有效。我将发布整个代码 – Jason