【问题标题】:The DB instance and EC2 security group are in different VPCs, cloudFormation error数据库实例和EC2安全组在不同的VPC,cloudFormation错误
【发布时间】:2019-12-22 17:42:23
【问题描述】:

我想自动化创建 RDS 的过程。我想创建 RDS Aurora。
部署应用程序时,堆栈 cloudFormation 被验证,我有一个错误:

发生错误:DatabaseCluster - 数据库实例和 EC2 安全组位于不同的 VPC。

你能说一下有什么问题吗?

我关注了这个帖子Issue with creating a Postgres RDS in Cloudformation Template,但这不起作用。

这是我的 serverless.yml 文件的一部分

resources:
  Resources:
    DatabaseCluster:
      Type: AWS::RDS::DBCluster
      Properties:
        DatabaseName: name${opt:stage, self:provider.stage}
        Engine: aurora
        MasterUsername: ${ssm:MasterUsername-${opt:stage, self:provider.stage}}
        MasterUserPassword: ${ssm:MasterUserPassword-${opt:stage, self:provider.stage}}
        Port: "3306"
        VpcSecurityGroupIds:
          - !Ref VpcSecurityGroup

    ServerlessRDS:
      Type: AWS::RDS::DBInstance
      Properties:
        Engine: aurora
        DBClusterIdentifier: !Ref "DatabaseCluster"
        DBInstanceIdentifier: db-name-${opt:stage, self:provider.stage}
        DBInstanceClass: db.t2.medium
        VPCSecurityGroups:
          - !Ref VpcSecurityGroup
        DBSubnetGroupName: !Ref myDBSubnetGroup


    VpcSecurityGroup:
      Type: AWS::EC2::SecurityGroup
      Properties:
        VpcId:
          Ref: ServerlessVPC
        GroupDescription: "Allow all traffic"
        SecurityGroupEgress:
          - IpProtocol: -1
            CidrIp: 0.0.0.0/0
        SecurityGroupIngress:
          - IpProtocol: -1
            CidrIp: 0.0.0.0/0

    ServerlessVPC:
      Type: AWS::EC2::VPC
      Properties:
        CidrBlock: "10.0.0.0/16"

    myDBSubnetGroup:
      Type: "AWS::RDS::DBSubnetGroup"
      Properties:
        DBSubnetGroupDescription: "description"
        SubnetIds:
          - !Ref ServerlessSubnetA
          - !Ref ServerlessSubnetB
    ServerlessSubnetA:
      Type: AWS::EC2::Subnet
      Properties:
        VpcId:
          Ref: ServerlessVPC
        AvailabilityZone: "eu-west-1b"
        CidrBlock: "10.0.0.0/24"
    ServerlessSubnetB:
      Type: AWS::EC2::Subnet
      Properties:
        VpcId:
          Ref: ServerlessVPC
        AvailabilityZone: "eu-west-1a"
        CidrBlock: "10.0.1.0/24"

【问题讨论】:

    标签: amazon-web-services amazon-ec2 amazon-cloudformation amazon-vpc amazon-aurora


    【解决方案1】:

    您需要将DBSubnetGroupName 参数添加到AWS::RDS::DBCluster 资源。

    DatabaseCluster:
      Type: AWS::RDS::DBCluster
      Properties:
        DatabaseName: name${opt:stage, self:provider.stage}
        Engine: aurora
        MasterUsername: ${ssm:MasterUsername-${opt:stage, self:provider.stage}}
        MasterUserPassword: ${ssm:MasterUserPassword-${opt:stage, self:provider.stage}}
        Port: "3306"
        VpcSecurityGroupIds:
          - !Ref VpcSecurityGroup
        DBSubnetGroupName:
          Ref: myDBSubnetGroup
    

    此外,您可能希望在 VpcSecurityGroup 资源中添加对 ServerlessSubnetAServerlessSubnetB 的显式依赖,以通过服务创建某种组资源并避免任何竞争条件。

    VpcSecurityGroup:
        Type: AWS::EC2::SecurityGroup
        DependsOn: 
        - ServerlessSubnetA
        - ServerlessSubnetB
        Properties:
          VpcId:
            Ref: ServerlessVPC
          GroupDescription: "Allow all traffic"
          SecurityGroupEgress:
            - IpProtocol: -1
              CidrIp: 0.0.0.0/0
          SecurityGroupIngress:
            - IpProtocol: -1
              CidrIp: 0.0.0.0/0
    

    【讨论】:

      【解决方案2】:

      遇到这种情况,发现有人修改了dBs VPC。

      有人拍摄了快照并将其还原到不同的 VPC,而该 VPC 显然不需要安全组。

      这可能是因为与数据库通信的网站运行在不同的 VPC 上。

      要解决它,您需要做出一个艰难的决定,因为此时您可能最终不得不将整个事情吹走,如果它是一组 CFN 模板的一部分,那么这可能会给多米诺骨牌效应带来更多问题.

      您可能值得尝试将 dB 恢复到 CFN 模板认为它所在的原始 VPC(或更改子网组/VPC)以及安全组由 CFN 模板创建的位置,并且然后重新运行 CFN 模板。

      如果失败,例如最近更新了数据库版本,您可能会遇到以下问题:

      无法将 postgres 从 9.6.22 升级到 9.6.11

      此时,我知道的唯一办法是删除 CFN 堆栈并重新运行它。

      【讨论】:

        猜你喜欢
        • 2019-12-25
        • 2017-08-08
        • 1970-01-01
        • 2016-05-12
        • 2021-04-18
        • 2021-06-28
        • 2019-06-29
        • 2019-10-27
        • 2019-02-23
        相关资源
        最近更新 更多