数据库实例和EC2安全组在不同的VPC,cloudFormation错误

Luk*_*K_K 6 amazon-ec2 amazon-web-services aws-cloudformation amazon-vpc amazon-aurora

我想自动化创建 RDS 的过程。我想创建 RDS Aurora。
部署应用程序时,验证堆栈 cloudFormation 并且出现错误:

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

你能说有什么问题吗?

我关注了这篇文章在 Cloudformation 模板中创建 Postgres RDS 问题,但这不起作用。

这是我的 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"
Run Code Online (Sandbox Code Playgroud)

Asd*_*dfg 7

您需要DBSubnetGroupNameAWS::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
Run Code Online (Sandbox Code Playgroud)

此外,您可能希望在Resource上ServerlessSubnetAServerlessSubnetBVpcSecurityGroupResource 中添加显式依赖项,以通过服务创建组资源并避免任何竞争条件。

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
Run Code Online (Sandbox Code Playgroud)