我创建了一个Windows 2012 AMI并使用如下所示的CloudFormation模板创建了该AMI的实例。
在该JSON脚本中,我想调用PowerShell脚本来禁用服务(简单的一个)。将创建EC2 Windows 2012实例。在我服用AMI之前,我确保EC2Config服务正在运行。它现在有效。以下是运行良好的代码。但问题是,我不清楚cfn-hup,cfn-signal和cfn-init之间的相互作用。老实说,我读到了所有4个帮助程序脚本。但我并没有围绕这些助手脚本。
是否有关于这4个助手脚本如何协同工作的博客或文档?
{
"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;Unblock-File C:\\PowershellScripts\\WindowsServiceManager.ps1;. C:\\PowershellScripts\\WindowsServiceManager.ps1;SetWindowsServiceStartupType Dnscache Manual;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"
]]}}
}
},
"WindowsServerWaitHandle": {
"Type": "AWS::CloudFormation::WaitConditionHandle"
},
"WindowsServerWaitCondition": {
"Type": "AWS::CloudFormation::WaitCondition",
"DependsOn": "MyInstance",
"Properties": {
"Handle": { "Ref": "WindowsServerWaitHandle" },
"Timeout": "1800"
}
}
}
}
答案 0 :(得分:0)
在这里找到一个不错的解释:
AWS :: CloudFormation :: Init如何工作的顺序:
*您可以为Linux和Windows下载CloudFormation帮助程序脚本。这些预安装在亚马逊提供的Linux和Windows AMI上。您需要在EC2用户数据脚本中指定触发cfn-init和cfn-signal的命令。一旦实例启动并运行,EC2用户数据脚本将自动为大多数Linux发行版和Windows执行。
一旦您的应用程序堆栈启动并运行,您可能会在堆栈的生命周期中更新应用程序,应用操作系统修补程序或执行其他一些配置更新。您只需更新模板中的AWS :: CloudFormation :: Init部分(例如,指定较新版本的应用程序包),然后调用UpdateStack。执行此操作时,CloudFormation将根据更新的模板更新实例元数据。然后,在实例上运行的cfn-hup守护程序检测更新的元数据并重新运行cfn-init以根据更新的配置更新实例。 cfn-hup是Linux和Windows上可用的CloudFormation帮助程序脚本之一。