引导 Amazon CloudFormation Windows 堆栈
本主题将介绍如何引导 Windows 堆栈并排除堆栈创建问题。如果您要创建自己的用于 CloudFormation 的 Windows 映像,请参阅《Amazon EC2 用户指南》中的在 EC2 旧版 Windows 操作系统实例启动期间使用 EC2ConfigService 执行任务中的信息来获得相关说明。由于 EC2ConfigService 与 Amazon CloudFormation 引导工具一起工作,因此,您必须通过 EC2ConfigService 设置 Windows 实例。
启动 Windows 堆栈的示例
出于说明的目的,我们将查看一个 Amazon CloudFormation 单实例 SharePoint 服务器模板。
通过以下 URL 可查看完整的模板:
本示例将展示如何:
-
创建访问实例的 IAM 用户和安全组。
-
配置初始化文件:
cfn-credentials
、cfn-hup.conf
和cfn-auto-reloader.conf
。 -
下载软件包,例如 SharePoint Foundation 2010,并将其安装在服务器实例上。
-
使用
WaitCondition
确定资源准备就绪。 -
通过 Amazon 弹性 IP (EIP) 检索实例的 IP。
Amazon CloudFormation 帮助程序脚本 cfn-init
用于根据 Windows Single Server Sharepoint Foundation 模板中的 AWS::CloudFormation::Init
资源中的信息来执行上述各种操作。
AWS::CloudFormation::Init
部分名称为 SharePointFoundation
,并通过标准声明开始:
"SharePointFoundation": { "Type" : "AWS::EC2::Instance", "Metadata" : { "AWS::CloudFormation::Init" : { "config" : {
在此之后,声明 AWS::CloudFormation::Init
的 files
部分:
"files" : { "c:\\cfn\\cfn-hup.conf" : { "content" : { "Fn::Join" : ["", [ "[main]\n", "stack=", { "Ref" : "AWS::StackName" }, "\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.SharePointFoundation.Metadata.AWS::CloudFormation::Init\n", "action=cfn-init.exe -v -s ", { "Ref" : "AWS::StackName" }, " -r SharePointFoundation", " --region ", { "Ref" : "AWS::Region" }, "\n" ]]} }, "C:\\SharePoint\\SharePointFoundation2010.exe" : { "source" : "http://d3adzpja92utk0.cloudfront.net/SharePointFoundation.exe" } },
将在此处创建这三个文件,并将其置于服务器实例上的 C:\cfn
目录中。这些文件为:
-
cfn-hup.conf
为cfn-hup
的配置文件。 -
cfn-auto-reloader.conf
为挂钩的配置文件,当AWS::CloudFormation::Init
中的元数据发生变化时,cfn-hup
将用其初始化更新(调用cfn-init
)。
此外,还有一个文件下载至服务器:SharePointFoundation.exe
。该文件用于将 SharePoint 安装在服务器实例上。
重要
由于 Windows 上的路径使用反斜线 (‘\’) 字符,因此,您必须记住在任何时候,当您参考 Amazon CloudFormation 模板中的 Windows 路径时,应通过预置另一反斜线而适当将所有反斜线转义。
接下来是 commands
部分,即 cmd.exe
命令。
"commands" : { "1-extract" : { "command" : "C:\\SharePoint\\SharePointFoundation2010.exe /extract:C:\\SharePoint\\SPF2010 /quiet /log:C:\\SharePoint\\SharePointFoundation2010-extract.log" }, "2-prereq" : { "command" : "C:\\SharePoint\\SPF2010\\PrerequisiteInstaller.exe /unattended" }, "3-install" : { "command" : "C:\\SharePoint\\SPF2010\\setup.exe /config C:\\SharePoint\\SPF2010\\Files\\SetupSilent\\config.xml" }
由于实例中的命令可通过按名称的字母顺序排列 的方式来处理,因此,每项命令均将预置数字,表示其所需的执行顺序。因此,我们能够确保首先提取安装软件包,随后安装所有系统必备项,最后,开始安装 SharePoint。
下面是 Properties
部分:
"Properties": { "InstanceType" : { "Ref" : "InstanceType" }, "ImageId" : { "Fn::FindInMap" : [ "AWSRegionArch2AMI", { "Ref" : "AWS::Region" }, { "Fn::FindInMap" : [ "AWSInstanceType2Arch", { "Ref" : "InstanceType" }, "Arch" ] } ] }, "SecurityGroups" : [ {"Ref" : "SharePointFoundationSecurityGroup"} ], "KeyName" : { "Ref" : "KeyPairName" }, "UserData" : { "Fn::Base64" : { "Fn::Join" : ["", [ "<script>\n", "cfn-init.exe -v -s ", { "Ref" : "AWS::StackName" }, " -r SharePointFoundation", " --region ", { "Ref" : "AWS::Region" }, "\n", "cfn-signal.exe -e %ERRORLEVEL% ", { "Fn::Base64" : { "Ref" : "SharePointFoundationWaitHandle" }}, "\n", "</script>" ]]}} }
在本部分中,UserData
属性包含一个 cmd.exe
脚本,该脚本通过将 cfn-init
用 <script>
标记括起得以执行。您可以通过将脚本用 <powershell>
标签括起,来在此处改用 Windows Powershell 脚本。对于 Windows 堆栈,您必须再次对等待条件句柄 URL 进行 base64 编码。
可参考此处的 SharePointFoundationWaitHandle,并通过 cfn-signal
运行。模板的下一部分中声明了 WaitConditionHandle
和相关 WaitCondition
:
"SharePointFoundationWaitHandle" : { "Type" : "AWS::CloudFormation::WaitConditionHandle" }, "SharePointFoundationWaitCondition" : { "Type" : "AWS::CloudFormation::WaitCondition", "DependsOn" : "SharePointFoundation", "Properties" : { "Handle" : {"Ref" : "SharePointFoundationWaitHandle"}, "Timeout" : "3600" } }
由于执行所有步骤和安装 SharePoint 需要一定的时间,但无需一个小时,因此在超时前,WaitCondition
将等待一个小时 (3600) 秒。
如果一切顺利,弹性 IP 将用于提供对 SharePoint 实例的访问权限:
"Outputs" : { "SharePointFoundationURL" : { "Value" : { "Fn::Join" : ["", ["http://", { "Ref" : "SharePointFoundationEIP" } ]] }, "Description" : "SharePoint Team Site URL. Please retrieve Administrator password of the instance and use it to access the URL" }
堆栈创建完成后,由 EIP 提供的 IP 地址将显示在 Amazon CloudFormation 控制台的输出选项卡中。但您需要先检索为实例生成的临时管理员密码,然后才能访问实例。有关更多信息,请参阅《Amazon EC2 用户指南》中的使用 RDP 连接到 Windows 实例。
如何管理 Windows 服务
除了使用 windows
密钥而不使用 sysvinit
之外,管理 Linux 服务的方式与管理 Windows 服务的方式相同。以下示例将启动 cfn-hup
服务,将其设置为“自动”,并在 cfn-init
修改 c:\cfn\cfn-hup.conf
或 c:\cfn\hooks.d\cfn-auto-reloader.conf
配置文件时重启服务。
"services" : { "windows" : { "cfn-hup" : { "enabled" : "true", "ensureRunning" : "true", "files" : ["c:\\cfn\\cfn-hup.conf", "c:\\cfn\\hooks.d\\cfn-auto-reloader.conf"] } } }
您可以使用名称而非显示名称引用服务,从而以相同方式管理其他 Windows 服务。
如何解决堆栈创建故障问题
如果您的堆栈在创建中出现问题,默认行为将为失败时回滚。正常情况下,这属于一项良好默认,因为它免除了不必要的收费,但是它会让调试堆栈创建失败的原因变得非常困难。
要关闭此行为,请在使用 Amazon CloudFormation 控制台创建堆栈时,选择 Show Advanced Options(显示高级选项),然后选择 Rollback on failure(失败时回滚)旁边的 No(否)选择器。通过此操作,您可以登录您的实例,并查看日志文件,并精确查找运行启动脚本时出现的问题。
须查看的重要日志文件包括:
-
EC2 配置日志位于:
C:\Program Files\Amazon\Ec2ConfigService\Logs\Ec2ConfigLog.txt
-
cfn-init 日志位于:
C:\cfn\log\cfn-init.log
有关更多日志,请参阅以下 EC2 指南: