Cloudformation - 如何在Windows 2012实例上安装cfn帮助程序脚本

时间:2017-08-10 23:41:09

标签: amazon-ec2 amazon-cloudformation

我有一个Windows 2012服务器的自定义AMI映像。我希望在该实例出现时使用PowerShell脚本启动一些服务并执行一些额外的操作。顺便说一下,我正在使用AWS云计算模板来调出实例。

在接受EC2实例的AMI之前(根据Steve的建议),我检查了用户数据" EC2Config设置中的选项并运行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"

由于

1 个答案:

答案 0 :(得分:2)

如果您使用的是亚马逊Windows 2012 AMI,那么它将安装cfn- *帮助程序脚本。

如果没有,则需要安装它们。 official documentation细节很薄:

  

这些脚本默认安装在/ opt / aws / bin中的最新Amazon Linux AMI上。它们也可用于以前版本的Amazon Linux AMI的Amazon Linux AMI yum存储库以及其他Linux / Unix发行版的RPM。您还可以使用Python for Windows在Microsoft Windows(2008或更高版本)上安装脚本。

但您可以使用EC2ConfigService安装它们。需要注意的是EC2ConfigService中的cfn脚本may be disabled

  

在创建Windows AMI之前,单击开始并转到“E”。打开Ec2Config设置。启用“Userdata”执行。如果要设置管理员密码,请选择中间单选按钮并提供密码。然后“使用sysprep关闭”。您将在sysprep期间看到它已启用userdata执行。机器关闭后 - 创建AMI。