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,则只需要引用子网组的名称。

相关内容

  • 没有找到相关文章

最新更新