SubnetGroup.fromSomething() 用于 DocDB?

SubnetGroup.fromSomething() for DocDB?

我正在尝试为内部应用程序添加带有 cdk 的 DocDB 堆栈。在我的组织中,我们无法创建新的子网组。以下是未能 运行 的代码。我一直在浏览文档,但我没有看到从 ARN/Name/ID 等获取现有子网组的选项...此功能是否存在,或者我正在处理这个问题?

// We are unauthorized to create subnet groups
const subnetGroup = new docdb.CfnDBSubnetGroup(this, `${ProjectSettings.PROJECT_NAME}-Subnet-Group`, {
    subnetIds: [subnet1.subnetId, subnet2.subnetId],
    dbSubnetGroupName: `${ProjectSettings.PROJECT_NAME}-Subnet-Group`,
    dbSubnetGroupDescription: `${ProjectSettings.PROJECT_NAME} Subnet Group for DocumentDB`
});

const dbCluster = new docdb.CfnDBCluster(this, `${ProjectSettings.PROJECT_NAME}-Db-Cluster`, {
    storageEncrypted: true,
    availabilityZones: [ProjectSettings.AZ1_SLUG, ProjectSettings.AZ2_SLUG],
    dbClusterIdentifier: `${ProjectSettings.PROJECT_NAME}Docdb`,
    masterUsername: `${ProjectSettings.PROJECT_NAME}dbuser`,
    masterUserPassword: ProjectSettings.DB_MASTER_PASS,
    vpcSecurityGroupIds: [sg.securityGroupName],
    dbSubnetGroupName: subnetGroup.dbSubnetGroupName, //How do I query for an existing subnetgroup?
    port
});

dbCluster.addDependsOn(subnetGroup) //How do I query for an existing subnetgroup?

const dbInstance = new docdb.CfnDBInstance(this, `${ProjectSettings.PROJECT_NAME}-Db-Instance`, {
    dbClusterIdentifier: dbCluster.ref,
    autoMinorVersionUpgrade: true,
    dbInstanceClass: "db.t3.medium",
    //TODO: Change me to something else
    dbInstanceIdentifier: "development",
});
dbInstance.addDependsOn(dbCluster);

我最终使用 SDK 查询现有的 SubnetGroup 名称,并在回调中使用 CDK 包装集群创建。

docDbSDK.describeDBSubnetGroups({}, (err, resp) => {
            if (err) throw err;
            if (resp.DBSubnetGroups) {
                let subnetGroup = resp.DBSubnetGroups[0]
                const dbCluster = new docdb.CfnDBCluster(this, `${ProjectSettings.PROJECT_NAME}-Db-Cluster`, {
                    storageEncrypted: true,
                    availabilityZones: [ProjectSettings.AZ1_SLUG, ProjectSettings.AZ2_SLUG],
                    dbClusterIdentifier: `${ProjectSettings.PROJECT_NAME}Docdb`,
                    masterUsername: `${ProjectSettings.PROJECT_NAME}dbuser`,
                    masterUserPassword: ProjectSettings.DB_MASTER_PASS,
                    vpcSecurityGroupIds: [sg.securityGroupName],
                    dbSubnetGroupName: subnetGroup.DBSubnetGroupName,
                    port
                });

                const dbInstance = new docdb.CfnDBInstance(this, `${ProjectSettings.PROJECT_NAME}-Db-Instance`, {
                    dbClusterIdentifier: dbCluster.ref,
                    autoMinorVersionUpgrade: true,
                    dbInstanceClass: "db.t3.medium",
                    //TODO: Change me to something else
                    dbInstanceIdentifier: "development",
                });
                dbInstance.addDependsOn(dbCluster);

                // Add a ingress rule for the VPC CIDR
                // TODO: Might not need to give access to the WHOLE VPC for the account...
                sg.addIngressRule(ec2.Peer.ipv4(defaultVpc.vpcCidrBlock), ec2.Port.tcp(port));

                // Found this in a how-to, testing output
                new cdk.CfnOutput(this, `${ProjectSettings.PROJECT_NAME}-Db-URL`, {
                    value: `mongodb://${dbCluster.masterUsername}:${dbCluster.masterUserPassword}@${dbCluster.attrEndpoint}:${dbCluster.attrPort}`
                });
            }
        })

如果您使用 CDK 创建 DBCluster,您似乎只需要引用子网组的名称。