使用AWS CloudFormation创建DBSubnetGroup

时间:2017-01-18 21:24:44

标签: amazon-web-services amazon-cloudformation subnet amazon-ecs amazon-rds-aurora

我使用ECS-CLI(0.4.5)启动CFN模板,现在我尝试将Aurora集群放入CFN模板,并通过CFN更改堆栈的更改集SDK。

我无法弄清楚为什么它对我的子网感到不满。子网是由最初的'ecs-cli up'创建的。呼叫。它们与堆栈的其余部分位于相同的vpc中,在我尝试部署变更集之前它们已经存在,并且它们位于不同的可用区域(us-west-2b和us-west-2c)。

CFN给我的唯一信息是,某些输入子网无效'。

CFN失败: CFN Failure

子网: Subnets

我可以通过管理控制台创建一个DBSubnetGroup,它具有完全相同的子网,没有任何问题。

关于可能出错的任何想法?这是CloudFormation中的错误吗?如果需要更多信息来解决这个问题,请告诉我......老实说我真是亏本

这是我的初始模板归结为(它内置于ecs-cli中):

 "PubSubnetAz1": {
        "Type": "AWS::EC2::Subnet",
        "Properties": {
            "VpcId": {
                "Ref": "Vpc"
            },
            "CidrBlock": "10.0.0.0/24",
            "AvailabilityZone": "us-west-2b"
        }
},
"PubSubnetAz2": {
        "Type": "AWS::EC2::Subnet",
        "Properties": {
            "VpcId": {
                "Ref": "Vpc"
            },
            "CidrBlock": "10.0.1.0/24",
            "AvailabilityZone": "us-west-2c"
        }
},
"InternetGateway": {
    "Type": "AWS::EC2::InternetGateway"
},
"AttachGateway": {
    "Type": "AWS::EC2::VPCGatewayAttachment",
    "Properties": {
        "VpcId": {
            "Ref": "Vpc"
        },
        "InternetGatewayId": {
            "Ref": "InternetGateway"
        }
    }
},
"RouteViaIgw": {
    "Type": "AWS::EC2::RouteTable",
    "Properties": {
        "VpcId": {
            "Ref": "Vpc"
        }
    }
},
"PublicRouteViaIgw": {
    "DependsOn": "AttachGateway",
    "Type": "AWS::EC2::Route",
    "Properties": {
        "RouteTableId": {
            "Ref": "RouteViaIgw"
        },
        "DestinationCidrBlock": "0.0.0.0/0",
        "GatewayId": {
            "Ref": "InternetGateway"
        }
    }
},
"PubSubnet1RouteTableAssociation": {
    "Type": "AWS::EC2::SubnetRouteTableAssociation",
    "Properties": {
        "SubnetId": {
            "Ref": "PubSubnetAz1"
        },
        "RouteTableId": {
            "Ref": "RouteViaIgw"
        }
    }
},
"PubSubnet2RouteTableAssociation": {
    "Type": "AWS::EC2::SubnetRouteTableAssociation",
    "Properties": {
        "SubnetId": {
            "Ref": "PubSubnetAz2"
        },
        "RouteTableId": {
            "Ref": "RouteViaIgw"
        }
    }
},

然后当我去更新它时,我添加了这个:

"DBSubnetGroup": {
    "Type": "AWS::RDS::DBSubnetGroup",
    "Properties": {
        "DBSubnetGroupDescription": "Aurora Subnet Group using subnets from 2 AZs",
        "SubnetIds": {
             "Fn::Join": [
                    ",", [{
                            "Ref": "pubSubnetAz1"
                        },
                        {
                            "Ref": "pubSubnetAz2"
                        }
                    ]
                ]
            }]
        }
    }
}

变更集应该足够简单......

"Changes": [
    {
      "Type": "Resource",
      "ResourceChange": {
        "Action": "Add",
        "LogicalResourceId": "DBSubnetGroup",
        "ResourceType": "AWS::RDS::DBSubnetGroup",
        "Scope": [],
        "Details": []
      }
    }
]

我正在使用AWSTemplateFormatVersion 2010-09-09和JavaScript aws-sdk" ^ 2.7.21"

2 个答案:

答案 0 :(得分:3)

问题是您将子网ID连接成字符串。相反,您应该在数组中传递它们。试试这个:

  "PrivateSubnetGroup": {
    "Type": "AWS::RDS::DBSubnetGroup",
    "Properties": {
      "SubnetIds": [
        {
          "Ref": "PubSubnetAz1"
        },
        {
          "Ref": "PubSubnetAz2"
        }
      ],
      "DBSubnetGroupDescription": "Aurora Subnet Group using subnets from 2 AZs"
    }
  }

另外,我强烈建议尝试使用yaml而不是json。 Cloudformation现在支持这一功能,以及一些快捷功能,可以更轻松地使用引用,我认为从长远来看,您会发现读取和写入都更容易。

以下是如何在yaml中编写等效json的示例:

PrivateSubnetGroup:
  Type: AWS::RDS::DBSubnetGroup
  Properties:
    DBSubnetGroupDescription: Subnet group for Aurora Database
    SubnetIds:
    - !Ref PubSubnetAz1
    - !Ref PubSubnetAz2

答案 1 :(得分:2)

根据AWS::RDS::DBSubnetGroup文档,SubnetIDs参数接受字符串列表,而不是您在示例中提供的CommaDelimitedList。您应该直接在JSON数组中传递子网,而不使用Fn::Join

"SubnetIds": [
  {"Ref": "pubSubnetAz1"},
  {"Ref": "pubSubnetAz2"}
]