无法从 Cloudformation 模板(windows 实例)调用任何 powershell 脚本
Can't call any powershell script from Cloudformation template (windows instance)
我创建了一个 Windows 2012 AMI,并使用如下所示的 CloudFormation 模板创建了该 AMI 的一个实例。
在那个 JSON 脚本中,我想调用 PowerShell 脚本来禁用服务(简单的)。创建 EC2 Windows 2012 实例。在我使用 AMI.But 之前,我确保 EC2Config 服务是 运行,PowerShell 脚本不会从 CFN 模板执行。知道为什么吗?
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "EC2 Head Node Instance ",
"Parameters": {
"VPC": {
"Description": "The default VPC",
"Type": "AWS::EC2::VPC::Id"
},
"AvailabilityZone": {
"Description": "Availablity Zone",
"Type": "String"
},
"Region":{
"Description": "Dev/Test/Prod regions",
"Type": "String"
},
"AMI": {
"Description": "AMI to start virtual server",
"Type": "String",
"Default": "ami-19273960",
"MaxLength": 12,
"MinLength": 12
},
"Subnet": {
"Description": "subnet to launch virtual server in",
"Type": "AWS::EC2::Subnet::Id"
}
},
"Resources": {
"EC2Instance": {
"Type": "AWS::EC2::Instance",
"Metadata": {
"AWS::Cloudformation::Init": {
"configSets": {
"config": [
"rename",
"bootstrapDSC"
]
},
"rename": {
"a-rename-computer" : {
"command": "powershell.exe -Command Rename-Computer -qrmawshead01 Server1 -Restart",
"waitAfterCompletion" : "forever"
}
},
"bootstrapDSC": {
"a-setpullmode" : {
"command": "powershell.exe -Command c:\cfn\scripts\SetPullMode.ps1",
"waitAfterCompletion" :"0"
}
}
}
},
"Properties": {
"ImageId" : { "Ref": "AMI"},
"SubnetId": {"Ref": "Subnet"},
"AvailabilityZone": {"Ref": "AvailabilityZone"},
"SecurityGroupIds" : [ "sg-b603b2cc" ],
"Tags": [
{
"Key": "Name",
"Value": "Head Node in DEV region"
}
]
}
}
},
"Outputs": {
"InstanceId": {
"Value": {"Ref": "EC2Instance"},
"Description": "ID of virtual server"
},
"PublicIPAddress": {
"Value": {"Fn::GetAtt": ["EC2Instance", "PublicIp"]},
"Description": "public IP address of virtual server"
}
}
}
虽然您已在模板中配置 CloudFormation::Init
,但还需要一个额外的步骤才能激活。
实例需要调用cfn-init.exe
的用户数据脚本。然后该程序从 CloudFormation 模板中检索配置并运行请求的命令。
例如:
"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>"
]]}}
信令方法还允许 cfn-init 发回 success/failure 信号给 CloudFormation。
我创建了一个 Windows 2012 AMI,并使用如下所示的 CloudFormation 模板创建了该 AMI 的一个实例。
在那个 JSON 脚本中,我想调用 PowerShell 脚本来禁用服务(简单的)。创建 EC2 Windows 2012 实例。在我使用 AMI.But 之前,我确保 EC2Config 服务是 运行,PowerShell 脚本不会从 CFN 模板执行。知道为什么吗?
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "EC2 Head Node Instance ",
"Parameters": {
"VPC": {
"Description": "The default VPC",
"Type": "AWS::EC2::VPC::Id"
},
"AvailabilityZone": {
"Description": "Availablity Zone",
"Type": "String"
},
"Region":{
"Description": "Dev/Test/Prod regions",
"Type": "String"
},
"AMI": {
"Description": "AMI to start virtual server",
"Type": "String",
"Default": "ami-19273960",
"MaxLength": 12,
"MinLength": 12
},
"Subnet": {
"Description": "subnet to launch virtual server in",
"Type": "AWS::EC2::Subnet::Id"
}
},
"Resources": {
"EC2Instance": {
"Type": "AWS::EC2::Instance",
"Metadata": {
"AWS::Cloudformation::Init": {
"configSets": {
"config": [
"rename",
"bootstrapDSC"
]
},
"rename": {
"a-rename-computer" : {
"command": "powershell.exe -Command Rename-Computer -qrmawshead01 Server1 -Restart",
"waitAfterCompletion" : "forever"
}
},
"bootstrapDSC": {
"a-setpullmode" : {
"command": "powershell.exe -Command c:\cfn\scripts\SetPullMode.ps1",
"waitAfterCompletion" :"0"
}
}
}
},
"Properties": {
"ImageId" : { "Ref": "AMI"},
"SubnetId": {"Ref": "Subnet"},
"AvailabilityZone": {"Ref": "AvailabilityZone"},
"SecurityGroupIds" : [ "sg-b603b2cc" ],
"Tags": [
{
"Key": "Name",
"Value": "Head Node in DEV region"
}
]
}
}
},
"Outputs": {
"InstanceId": {
"Value": {"Ref": "EC2Instance"},
"Description": "ID of virtual server"
},
"PublicIPAddress": {
"Value": {"Fn::GetAtt": ["EC2Instance", "PublicIp"]},
"Description": "public IP address of virtual server"
}
}
}
虽然您已在模板中配置 CloudFormation::Init
,但还需要一个额外的步骤才能激活。
实例需要调用cfn-init.exe
的用户数据脚本。然后该程序从 CloudFormation 模板中检索配置并运行请求的命令。
例如:
"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>"
]]}}
信令方法还允许 cfn-init 发回 success/failure 信号给 CloudFormation。