How to Make Aurora Serverless database in CDK

Viewed 2259

I am trying to make an aurora serverless database. In the console its straightforward I just use the default subnet group and everything works very well.

When I am using cdk however I get the error "Aurora Serverless doesn't support DB subnet groups with subnets in the same Availability Zone. Choose a DB subnet group with subnets in different Availability Zones.". I have tried to troubleshoot but i really dont understand why its failing, i guess its using the same default subnet as when i do it manually (and it works)? So why is it suddenly all wrong when using cdk...

const auroraDatabaseCluster = new rds.ServerlessCluster(this, 'Database', {
    engine: rds.DatabaseClusterEngine.AURORA_POSTGRESQL,
    credentials: rds.Credentials.fromSecret(masterSecret),
    parameterGroup: clusterParameterGroup,
    defaultDatabaseName: databaseName,
    vpc: vpc,
    securityGroups: [databaseSecurityGroup],
    storageEncryptionKey: databaseKey,
    deletionProtection: false
  });

Anyone knows what is wrong?

I can also get it to work using a regional aurora database...

const auroraDatabaseCluster = new rds.DatabaseCluster(this, 'Database', {
    engine: rds.DatabaseClusterEngine.auroraPostgres({version: rds.AuroraPostgresEngineVersion.VER_11_8}),
    instances: 2, // TODO should be 2
    credentials: rds.Credentials.fromSecret(masterSecret),
    defaultDatabaseName: databaseName,
    port: endpointPort,
    storageEncrypted: true,
    storageEncryptionKey: databaseKey,
    deletionProtection: false, // TODO enable in prod
    parameterGroup: clusterParameterGroup,
    instanceProps: {
      instanceType: ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE3, ec2.InstanceSize.MEDIUM),
      securityGroups: [databaseSecurityGroup],
      vpcSubnets: {
        subnetType: ec2.SubnetType.PRIVATE,
      },
      vpc,
    },
  });
1 Answers

I solved the problem by manually adding a DB securityGroup with onePerAZ=true

const subnetGroup = new rds.SubnetGroup(this, "subnetGroup", {
        description: `Subnetgroup for serverless postgres aurora databasa`,
        vpc: vpc,
        vpcSubnets: {onePerAz: true},
      })

  const auroraDatabaseCluster = new rds.ServerlessCluster(this, 'Database', {
    engine: rds.DatabaseClusterEngine.AURORA_POSTGRESQL,
    credentials: rds.Credentials.fromSecret(masterSecret),
    parameterGroup: clusterParameterGroup,
    defaultDatabaseName: databaseName,
    vpc: vpc,
    subnetGroup: subnetGroup,
    securityGroups: [databaseSecurityGroup],
    storageEncryptionKey: databaseKey,
    deletionProtection: false
  });
Related