【问题标题】:How to add a RDS instance to a VPC using aws cloudformation如何使用 aws cloudformation 将 RDS 实例添加到 VPC
【发布时间】:2016-08-30 12:38:22
【问题描述】:

当我手动启动 RDS 实例时,我可以分配我希望它加入的 VPC。我正在尝试使用 AWS cloudformation 创建一个堆栈,但是我没有看到能够做到这一点的 API。我可以在堆栈中创建我的 VPC,然后为 EC2 和 DB 安全组的安全组引用它,它们最终都是 VPC 的一部分,但 RDS 实例本身不是。有没有办法将 VPC 分配给 RDS 实例?

下面是我的模板:

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Metadata": {
    "AWS::CloudFormation::Designer": {
      "30e03bfc-b61a-4d6c-89db-1b62b258a305": {
        "size": {
          "width": 80,
          "height": 80
        },
        "position": {
          "x": 700,
          "y": 170
        },
        "z": 0,
        "embeds": []
      }
    }
  },

  "Parameters": {

    "DBPreferredBkupWindow": {
      "Description"                 : "The daily time range (in UTC) during which automated backups are created, ideally off peak-hours.",
      "Type"                        : "String",
      "MinLength"                   : "1",
      "MaxLength"                   : "11",
      "AllowedPattern"              : "\\d[0-23]:\\d[0-59]-\\d[0-23]:\\d[0-59]",
      "Default"                     : "01:00-02:00"
    }
  },

  "Resources": {

    "VPC": {
      "Type": "AWS::EC2::VPC",
      "Properties": {
        "CidrBlock"                 : "172.16.0.0/16",
        "EnableDnsSupport"          : true
      }
    },

    "DB": {
      "Type": "AWS::RDS::DBInstance",
      "Properties": {
        "DBName"                    : "ems",
        "Engine"                    : "postgres",
        "EngineVersion"             : "9.4.7",
        "DBInstanceClass"           : "db.t1.micro",
        "DBInstanceIdentifier"      : "rltdb",
        "MasterUsername"            : "pgadmin",
        "MasterUserPassword"        : "pgadmin1",
        "AllocatedStorage"          : "100",
        "Iops"                      : "1000",
        "BackupRetentionPeriod"     : "7",
        "PreferredBackupWindow"     : { "Ref" : "DBPreferredBkupWindow" },
        "MultiAZ"                   : true,
        "PubliclyAccessible"        : false,
        "AutoMinorVersionUpgrade"   : false,
        "VPCSecurityGroups"         : [{ "Ref" : "SecurityGroup" } ]
      },

      "Metadata": {
        "AWS::CloudFormation::Designer": {
          "id": "30e03bfc-b61a-4d6c-89db-1b62b258a305"
        }
      }
    },

    "DBSecurityGroup": {
      "Type": "AWS::RDS::DBSecurityGroup",
      "Properties": {
        "EC2VpcId"                  : { "Ref" : "VPC" },
        "DBSecurityGroupIngress"    : { "EC2SecurityGroupName": { "Ref": "SecurityGroup"} },
        "GroupDescription"          : "Database Access"
      }
    },

    "SecurityGroup" : {
      "Type" : "AWS::EC2::SecurityGroup",
      "Properties" : {
        "VpcId"                     : { "Ref" : "VPC" },
        "GroupDescription"          : "Enable database access for application",
        "SecurityGroupIngress"      : [
          {"IpProtocol" : "tcp", "FromPort" : "5432", "ToPort" : "5432", "CidrIp" : "0.0.0.0/0"}
        ]
      }
    }
  }
}

【问题讨论】:

    标签: amazon-web-services amazon-cloudformation


    【解决方案1】:

    您必须在 CloudFormation 模板中创建一个DBSubnetGroup 和至少两个子网。

    "subnet-1" : {
       "Type" : "AWS::EC2::Subnet",
       "Properties" : {
          "CidrBlock" : "172.16.1.0/24",
          "VpcId" : { "Ref" : "VPC" }
       }
    },     
    
    "subnet-2" : {
       "Type" : "AWS::EC2::Subnet",
       "Properties" : {
          "CidrBlock" : "172.16.2.0/24",
          "VpcId" : { "Ref" : "VPC" }
       }
    },     
    
    "DBSubnetGroup" : {
       "Type" : "AWS::RDS::DBSubnetGroup",
       "Properties" : {
          "SubnetIds" : [
              { "Ref" : "subnet-1" },
              { "Ref" : "subnet-2" }
          ],
       }
    },
    

    最后,您必须在 "DB" 对象中包含 DBSubnetGroup。

    "DBSubnetGroupName": { "Ref": "DBSubnetGroup" }
    

    【讨论】:

      【解决方案2】:

      您需要包含DBSubnetGroupName:

      与数据库实例关联的数据库子网组。

      如果没有数据库子网组,则为非 VPC 数据库实例。

      使用您的 VPC 中的子网创建 DBSubnetGroup resource,然后将其绑定到您的 DBInstance:

      "DBSubnetGroupName": { "Ref": "MySubnetGroup" }
      

      【讨论】:

        猜你喜欢
        • 2020-03-27
        • 2017-08-06
        • 2015-09-08
        • 1970-01-01
        • 2015-08-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多