Cloudformation - 如何在 windows 2012 个实例上安装 cfn 帮助程序脚本
Cloudformation - How does the cfn helper scripts get installed on windows 2012 instances
我有一个 windows 2012 服务器的自定义 AMI 映像。当该实例出现时,我想启动一些服务并使用 powershell 脚本做一些额外的事情。顺便说一句,我正在使用 AWS cloudformation 模板来启动实例。
在对 EC2 实例进行 AMI 之前(根据下面史蒂夫的建议),我检查了 EC2Config 设置和 运行 系统操作中的 "user data" 选项。 运行 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"
谢谢
如果您使用的是 Amazon Windows 2012 AMI,那么它将已经安装了 cfn-* 帮助程序脚本。
如果没有,那么您需要安装它们。 official documentation 细节不足:
These scripts are installed by default on the latest Amazon Linux AMI in /opt/aws/bin. They are also available in the Amazon Linux AMI yum repository for previous versions of the Amazon Linux AMI as well as via RPM for other Linux/Unix distributions. You can also install the scripts on Microsoft Windows (2008 or later) by using Python for Windows.
但是您可以使用 EC2ConfigService 安装它们。需要注意的是 EC2ConfigService:
中的 cfn 脚本 may be disabled
Before creating your Windows AMI, click start and go down to "E". Open Ec2Config Settings. Enable "Userdata" execution. If you want set Administrator password, select the middle radio button and supply password. Then "shutdown with sysprep". You will see during sysprep that it has enabled userdata execution. Once the machine is down - create an AMI.
我有一个 windows 2012 服务器的自定义 AMI 映像。当该实例出现时,我想启动一些服务并使用 powershell 脚本做一些额外的事情。顺便说一句,我正在使用 AWS cloudformation 模板来启动实例。
在对 EC2 实例进行 AMI 之前(根据下面史蒂夫的建议),我检查了 EC2Config 设置和 运行 系统操作中的 "user data" 选项。 运行 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"
谢谢
如果您使用的是 Amazon Windows 2012 AMI,那么它将已经安装了 cfn-* 帮助程序脚本。
如果没有,那么您需要安装它们。 official documentation 细节不足:
These scripts are installed by default on the latest Amazon Linux AMI in /opt/aws/bin. They are also available in the Amazon Linux AMI yum repository for previous versions of the Amazon Linux AMI as well as via RPM for other Linux/Unix distributions. You can also install the scripts on Microsoft Windows (2008 or later) by using Python for Windows.
但是您可以使用 EC2ConfigService 安装它们。需要注意的是 EC2ConfigService:
中的 cfn 脚本 may be disabledBefore creating your Windows AMI, click start and go down to "E". Open Ec2Config Settings. Enable "Userdata" execution. If you want set Administrator password, select the middle radio button and supply password. Then "shutdown with sysprep". You will see during sysprep that it has enabled userdata execution. Once the machine is down - create an AMI.