如何构建一个物理子网ID字符串,以便在cloudformation脚本中动态创建db子网组?

时间:2013-06-04 02:46:55

标签: amazon-web-services amazon-rds amazon-cloudformation vpc

我正在尝试构建一个CLoudFormation脚本,该脚本同时将一个实例和一个db启动到一个vpc中。问题是数据库需要两个AZ,所以我创建了第二个子网,现在我只需要在'MyDBSubnetGroup'var中引用两个子网物理ID。我可以获取我创建的子网的逻辑ID,但不知道如何引用这些物理ID。有人知道吗?谢谢!

继承我的代码:

"MyDBSubnetGroup" : {
      "Type" : "AWS::RDS::DBSubnetGroup",
      "Properties" : {
        "DBSubnetGroupDescription" : "Subnets available for the RDS DB Instance",
        "SubnetIds" : { "Fn::Join" : [ " ", [{"Ref" : "PublicSubnetAZ1"}, ", ", {"Ref" : "PublicSubnetAZ2"}, " " ]]}
      }
    },

2 个答案:

答案 0 :(得分:4)

我遇到同样的问题,在使用AWS支持后,我理解字符串列表并不意味着我们最初的想法。此外,如果要将数据库放在VPC中,则不得使用AWS::RDS::DBSecurityGroup个对象。

这是一个完整的示例,我需要一段时间才能让它工作:

"dbSubnetGroup" : {
        "Type" : "AWS::RDS::DBSubnetGroup",
        "Properties" : {
            "DBSubnetGroupDescription" : "Availability Zones for RDS DB",
            "SubnetIds" : [ { "Ref" : "subnetPrivate1" },
                            { "Ref" : "subnetPrivate2" } ]
        }
},
"dbInstance" : {
        "Type" : "AWS::RDS::DBInstance",
        "Properties" : {
            "DBInstanceIdentifier" : { "Fn::Join" : [ "",
                                                    [ { "Ref" : "AWS::StackName" },
                                                      "DB" ] ] },
            "DBName" : "dbname",
            "DBSubnetGroupName" : { "Ref" : "dbSubnetGroup" },
            "MultiAZ" : "true",
            "AllocatedStorage" : "8",
            "BackupRetentionPeriod" : "0",
            "DBInstanceClass" : "db.m1.medium",
            "Engine" : "postgres",
            "MasterUserPassword" : "masteruserpassword",
            "MasterUsername" : "masterusername",
            "VPCSecurityGroups" : [ { "Ref" : "sgVpc" }, { "Ref" : "sgDB" } ]
     }
},

答案 1 :(得分:2)

如果您映射子网ID,您可以使用类似的方式访问它们。

        "AWSRegionSubnet":{
        "us-east-1":{
            "RDSSubnets":[
                "subnet-aaaaaaaa",
                "subnet-bbbbbbbb"
            ]

        },
        "us-west-2":{
            "RDSSubnets":[
                "subnet-cccccccc",
                "subnet-dddddddd"
            ]
        }
    }

        "RDSSubnet":{
        "Type":"AWS::RDS::DBSubnetGroup",
        "Properties":{
            "DBSubnetGroupDescription":"Some cool notes here",
            "SubnetIds":{
                "Fn::FindInMap":[
                    "AWSRegionSubnet",
                    {
                        "Ref":"AWS::Region"
                    },
                    "RDSSubnets"
                ]
            }
        }
    }