带参考或TopLevel Fn :: FindInMap的CloudFormation模板

时间:2016-08-04 19:46:35

标签: amazon-web-services amazon-cloudformation

我在模板中定义了以下映射

// set id on table
var dataTable = document.getElementById("dataTable");
var tableHead = dataTable.getElementsByTagName("thead")[0];
var headRow = tableHead.getElementsByTagName("tr")[0];
var tds = document.querySelectorAll('#dataTable tbody td');
var ths = document.querySelectorAll('#dataTable thead th');
for(i=0;i<tds.length;i++){
    var offset=tds[i].offsetLeft- ths[i].offsetLeft;
    var thToSet = headRow.getElementsByTagName("th")[i];
    thToSet.style.marginLeft = offset + "px";
}

然而,验证失败,在Tested Demo之后,我可以看到原因。映射函数需要三层密钥,值只能是字符串或列表。

无论如何使用"Mappings": { "TimeBasedAutoScale" : { "days" : { "weekend" : {"0":"off","1":"off","2":"off","3":"off","4":"off","5":"off","6":"off","7":"off","8":"off","9":"off","10":"off","11":"off","12":"off","13":"off","14":"off","15":"off","16":"off","17":"off","18":"off","19":"off","20":"off","21":"off","22":"off","23":"off"}, "weekday1" : {"0":"off","1":"off","2":"on","3":"on","4":"on","5":"on","6":"on","7":"on","8":"on","9":"on","10":"on","11":"on","12":"on","13":"on","14":"on","15":"on","16":"on","17":"on","18":"on","19":"on","20":"on","21":"on","22":"off","23":"off"}, "weekday2" : {"0":"off","1":"off","2":"off","3":"on","4":"on","5":"on","6":"on","7":"on","8":"on","9":"on","10":"on","11":"on","12":"on","13":"on","14":"on","15":"on","16":"on","17":"on","18":"on","19":"on","20":"on","21":"on","22":"off","23":"off"} } }, "TimeBasedSchedule": { "6AMTimeBasedSchedule" : { "schedule" : { "Monday" : { "Fn::FindInMap" : [ "TimeBasedAutoScale", "days", "weekday1"]}, "Tuesday" : { "Fn::FindInMap" : [ "TimeBasedAutoScale", "days", "weekday1"]}, "Wednesday" :{ "Fn::FindInMap" : [ "TimeBasedAutoScale", "days", "weekday1"]}, "Thursday" : { "Fn::FindInMap" : [ "TimeBasedAutoScale", "days", "weekday1"]}, "Friday" : { "Fn::FindInMap" : [ "TimeBasedAutoScale", "days", "weekday1"]}, "Saturday" : { "Fn::FindInMap" : [ "TimeBasedAutoScale", "days", "weekend"]}, "Sunday" : { "Fn::FindInMap" : [ "TimeBasedAutoScale", "days", "weekend"]} } }, "7AMTimeBasedSchedule" : { "schedule" : { "Monday" : { "Fn::FindInMap" : [ "TimeBasedAutoScale", "days", "weekday1"]}, "Tuesday" : { "Fn::FindInMap" : [ "TimeBasedAutoScale", "days", "weekday1"]}, "Wednesday" :{ "Fn::FindInMap" : [ "TimeBasedAutoScale", "days", "weekday1"]}, "Thursday" : { "Fn::FindInMap" : [ "TimeBasedAutoScale", "days", "weekday1"]}, "Friday" : { "Fn::FindInMap" : [ "TimeBasedAutoScale", "days", "weekday1"]}, "Saturday" : { "Fn::FindInMap" : [ "TimeBasedAutoScale", "days", "weekend"]}, "Sunday" : { "Fn::FindInMap" : [ "TimeBasedAutoScale", "days", "weekend"]} } }} 或更好的映射进行上述操作?

1 个答案:

答案 0 :(得分:0)

要完成多级映射,请直接在Fn::FindInMapResources部分应用嵌套的Outputs内部函数。

以下是您的示例的完整工作版本:

Launch Stack

{
  "Description": "Return 'on' or 'off' according to the provided schedule, hour of day, and day of week.",
  "Mappings": {
    "TimeBasedAutoScale": {
      "weekend" : {"0":"off","1":"off","2":"off","3":"off","4":"off","5":"off","6":"off","7":"off","8":"off","9":"off","10":"off","11":"off","12":"off","13":"off","14":"off","15":"off","16":"off","17":"off","18":"off","19":"off","20":"off","21":"off","22":"off","23":"off"},
      "weekday1" : {"0":"off","1":"off","2":"on","3":"on","4":"on","5":"on","6":"on","7":"on","8":"on","9":"on","10":"on","11":"on","12":"on","13":"on","14":"on","15":"on","16":"on","17":"on","18":"on","19":"on","20":"on","21":"on","22":"off","23":"off"},
      "weekday2" : {"0":"off","1":"off","2":"off","3":"on","4":"on","5":"on","6":"on","7":"on","8":"on","9":"on","10":"on","11":"on","12":"on","13":"on","14":"on","15":"on","16":"on","17":"on","18":"on","19":"on","20":"on","21":"on","22":"off","23":"off"}
  },
    "TimeBasedSchedule": {
      "6AMTimeBasedSchedule": {
        "Monday": "weekday1",
        "Tuesday": "weekday1",
        "Wednesday": "weekday1",
        "Thursday": "weekday1",
        "Friday": "weekday1",
        "Saturday": "weekend",
        "Sunday": "weekend"
      },
      "7AMTimeBasedSchedule": {
        "Monday": "weekday2",
        "Tuesday": "weekday2",
        "Wednesday": "weekday2",
        "Thursday": "weekday2",
        "Friday": "weekday2",
        "Saturday": "weekend",
        "Sunday": "weekend"
      }
    }
  },
  "Parameters": {
    "Schedule": {
      "Description": "Selected schedule",
      "Type": "String",
      "AllowedValues" : [ "6AMTimeBasedSchedule", "7AMTimeBasedSchedule" ],
      "Default": "6AMTimeBasedSchedule"
    },
    "DayOfWeek": {
      "Description": "Day of week",
      "Type": "String",
      "AllowedValues" : [ "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" ],
      "Default": "Monday"
    },
    "HourOfDay": {
      "Description": "Hour of Day",
      "Type": "Number",
      "MinValue": "0",
      "MaxValue": "23",
      "Default": "0"
    }
  },
  "Resources": {
    "Dummy": {
      "Type": "AWS::CloudFormation::WaitConditionHandle"
    }
  },
  "Outputs": {
    "Result": {
      "Value": {
        "Fn::FindInMap": [
          "TimeBasedAutoScale",
          {"Fn::FindInMap": ["TimeBasedSchedule", "6AMTimeBasedSchedule", {"Ref": "DayOfWeek"}]},
          {"Ref": "HourOfDay"}
        ]
      }
    }
  }
}