3

我正在創建一個動態引用正確子網的ElastiCache SubnetGroup。我想在東部和西部地區使用相同的模板,因此我在映射中指定了子網組的子網。然而,當我嘗試運行更新我的堆棧,我得到以下錯誤:Cloudformation:ElastiCache SubnetGroup中創建子網的引用

Value of property SubnetIds must be of type List of String 

這裏的顯示大概什麼,我試圖做一個要點:https://gist.github.com/brockhaywood/b71ed34c6a554a0a0fec

在AWS論壇這個懸而未決的問題出現是一個非常類似的問題:https://forums.aws.amazon.com/message.jspa?messageID=532454

回答

0

我認爲SubnetIds should be an array,你有一個單一的對象。

"ElastiCacheSubnetGroup": { 
    "Type": "AWS::ElastiCache::SubnetGroup", 
    "Properties": { 
    "SubnetIds": [ 
     { 
      "Fn::FindInMap":["RegionMap", { "Ref":"AWS::Region" }, AppSubnets" ] 
     } 
    ] 
    } 
} 
+0

哦,真的? 'AppSubnets'引用是一個Refs數組,是否不滿足這個數組的需要? –

+0

一個合理的人可能會這樣想,但它會給你一個抱怨數據類型的錯誤信息:)。 – James

+0

我已嘗試,以及,提供以下對SubnetIds但它產生了同樣的錯誤: 「SubnetIds」:[{ 「FN :: FindInMap」:[ 「RegionMap」, { 「參考」:」 AWS :: Region「 }, 」AppSubnets「 ] }] –

0

的具體問題是,你不能在一個Mappings值內使用Ref,作爲Mappings文檔中指出:

You cannot include parameters, pseudo parameters, or intrinsic functions in the Mappings section.

作爲替代方案,可以使用Conditions來完成你的模板正在嘗試。下面是一個完整的工作示例:

Launch Stack

{ 
    "Description": "Create an ElastiCache SubnetGroup with different subnet depending on the current AWS region." 
    "Conditions": { 
    "us-east-1": {"Fn::Equals": [{"Ref":"AWS::Region"}, "us-east-1"]}, 
    "us-west-2": {"Fn::Equals": [{"Ref":"AWS::Region"}, "us-west-2"]} 
    }, 
    "Resources": { 
    "VPC": { 
     "Type": "AWS::EC2::VPC", 
     "Properties": { 
     "CidrBlock": "10.0.0.0/16" 
     } 
    }, 
    "AppSubnetA": { 
     "Type": "AWS::EC2::Subnet", 
     "Properties": { 
     "VpcId": {"Ref": "VPC"}, 
     "CidrBlock": "10.0.0.0/24", 
     "AvailabilityZone": {"Fn::Select": [1, {"Fn::GetAZs": ""}]} 
     } 
    }, 
    "AppSubnetB": { 
     "Type": "AWS::EC2::Subnet", 
     "Properties": { 
     "VpcId": {"Ref": "VPC"}, 
     "CidrBlock": "10.0.1.0/24", 
     "AvailabilityZone": {"Fn::Select": [1, {"Fn::GetAZs": ""}]} 
     } 
    }, 
    "ElastiCacheSubnetGroup": { 
     "Type": "AWS::ElastiCache::SubnetGroup", 
     "Properties": { 
     "Description": "SubnetGroup", 
     "SubnetIds": {"Fn::If": ["us-east-1", [ 
      {"Ref": "AppSubnetA"} 
      ], 
      {"Fn::If": ["us-west-2", 
      [ 
       {"Ref": "AppSubnetB"} 
      ], 
      {"Ref":"AWS::NoValue"} 
      ]} 
     ]} 
     } 
    } 
    } 
} 
+0

這應該是公認的答案。在CloudFormation模板的Mappings部分中使用動態值是不可能的。 – Aditya