Cloudformation:在ElastiCache SubnetGroup中引用创建的子网

时间:2015-11-04 22:46:52

标签: amazon-web-services amazon-ec2 amazon-elasticache amazon-cloudformation

我在创建一个动态引用正确子网的ElastiCache SubnetGroup时遇到了挑战。我想在东部和西部地区使用相同的模板,所以我在映射中指定子网组的子网。但是,当我尝试运行更新我的堆栈时,我收到以下错误:

public class CEAppAssembly:TyphoonAssembly {

   //uiviewcontrollers' components assembly
   var mainComponentsAssembly : CEMainComponentsAssembly!


   /**
    UI Dependencies
   **/

   public dynamic func mainViewController() -> AnyObject {
      return TyphoonDefinition.withClass(MainViewController.self) {
         (definition) in

         definition.injectProperty("dataProvider", with: self.mainComponentsAssembly.dataProvider())
      }
   }

这里有一个要点,大致显示了我要做的事情:https://gist.github.com/brockhaywood/b71ed34c6a554a0a0fec

AWS论坛上这个未解决的问题似乎是一个非常类似的问题:https://forums.aws.amazon.com/message.jspa?messageID=532454

2 个答案:

答案 0 :(得分:0)

我认为SubnetIds should be an array,你有一个对象。

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

答案 1 :(得分:0)

具体问题是,您无法在Ref值中使用Mappings,如Mappings文档中所述:

  

您不能在“映射”部分中包含参数,伪参数或内部函数。

作为替代方案,您可以使用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"}
          ]}
        ]}
      }
    }
  }
}