Cloudformation模板错误 - 模板验证错误:模板格式错误:每个Mappings成员Type必须是一个映射

时间:2017-10-19 19:24:09

标签: json amazon-ec2 amazon-cloudformation

我有一个cloudformation模板。

应创建EC2实例,更改管理员密码并重命名服务器。

我将几个参数传递给堆栈模板。当我运行它时,它给出“模板格式错误:每个映射成员类型必须是一个地图”。

我确保我在模板中引用的内容都在“映射”部分中。不知道为什么我收到此错误。

任何建议都非常有用。

{
   "AWSTemplateFormatVersion": "2010-09-09",

   "Parameters": {

"LocalAdminPassword" :
{
  "Type": "String",
  "NoEcho" : "true",
  "Description": "Password for the local server administrator account."
   }   
},

 "Mappings": {  

"EnvironmentTypeName" :
{
  "PlatformName" : {"Dev" : "D", "Test" : "T", "Prod" : "P"}
},
"QRMEnvironmentType" : 
{
  "Description"   :   "QRM Dev, test, or Prod Platform",
  "Type"          :   "String",
  "AllowedValues" :  ["Dev", "Test", "Prod"],
  "Default" : "Dev",
  "ConstraintDescription" : "must be either Dev, test, or Prod"
},
"QRMAvailabilityZoneIndex" :
{
  "Description"   :   "QRM Platform AZ letter A,B, or C for Dev, Test, or Prod",
  "Type"          :   "String",
  "AllowedValues" :  ["A", "B", "C"],
  "Default" : "A",
  "ConstraintDescription" : "Must be a letter of the AZ in the specified Region"
  }   
},

 "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  ([adsi]\\\"WinNT://$env:computername/Administrator\\\").SetPassword('",{ "Ref": "LocalAdminPassword"},"')"  
                        ] 
                      ]
                    }
            },
            "02-rename-server" :
                  {
                    "command" :
                    {
                      "Fn::Join" : 
                      [ 
                        "", 
                        [ 
                          "powershell.exe Rename-Computer -NewName ",  {"Fn::Join"  : [ "",[ "AW", {"Fn::FindInMap" : [ "EnvironmentTypeName", "PlatformName", {"Ref" : "QRMEnvironmentType"} ]},"W",{"Ref": "QRMAvailabilityZoneIndex"},"QRMHEAD"]] }    ," -force -restart"
                        ]
                      ]
                    },
                    "WaitAfterCompletion" : "forever"
                  },                
              "3-run-script" : {
                    "command" : { "Fn::Join" : [ "", [
                        "Powershell.exe Set-ExecutionPolicy Unrestricted -force \n",
                        "Powershell.exe C:\\scripts\\test.ps1 \n",
                        "Powershell.exe Start-Sleep -s 60; . C:\\PowershellScripts\\WindowsServiceManager.ps1;StopWindowsService Dnscache" , "\n"
                        ]]}}
                },
            "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": "MyEC2Instance"
     }
   ],

     }
  ,
    "WindowsServerWaitHandle": {
         "Type": "AWS::CloudFormation::WaitConditionHandle"
    },
     "WindowsServerWaitCondition": {
          "Type": "AWS::CloudFormation::WaitCondition",
          "DependsOn": "MyInstance",
      "Properties": {
     "Handle": { "Ref": "WindowsServerWaitHandle" },
     "Timeout": "1800"
   }
   }        
 }
}

1 个答案:

答案 0 :(得分:1)

我对yaml模板有同样的问题。 看起来模板中只支持2级映射,因此您需要在相关位置添加另一个级别,例如

...
"QRMEnvironmentType" : 
{
  "Description"   :   {"Value": "QRM Dev, test, or Prod Platform"},
  "Type"          :   {"Value": "String"},
  ...
}
...