Created
July 31, 2022 05:31
-
-
Save leegilmorecode/53a71d2b3de46c3a8ce570587799fe29 to your computer and use it in GitHub Desktop.
Workaround of using Serverless Aurora V2 with the AWS CDK
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://github.com/aws/aws-cdk/issues/20197 | |
enum ServerlessInstanceType { | |
SERVERLESS = "serverless", | |
} | |
type CustomInstanceType = ServerlessInstanceType | ec2.InstanceType; | |
const CustomInstanceType = { | |
...ServerlessInstanceType, | |
...ec2.InstanceType, | |
}; | |
const dbClusterInstanceCount: number = 1; | |
const vpc: ec2.Vpc = new ec2.Vpc(this, "InfraVPC", { | |
maxAzs: 2, | |
natGateways: 1, | |
vpcName: "infra-vpc", | |
subnetConfiguration: [ | |
{ | |
cidrMask: 24, | |
name: "private-subnet", | |
subnetType: ec2.SubnetType.PRIVATE_ISOLATED, | |
}, | |
{ | |
cidrMask: 24, | |
name: "public-subnet", | |
subnetType: ec2.SubnetType.PUBLIC, | |
}, | |
], | |
}); | |
const dbCluster: rds.DatabaseCluster = new rds.DatabaseCluster( | |
this, | |
"ServerlessAuroraDbCluster", | |
{ | |
engine: rds.DatabaseClusterEngine.auroraPostgres({ | |
version: rds.AuroraPostgresEngineVersion.VER_13_7, | |
}), | |
clusterIdentifier: "ServerlessAuroraDbCluster", | |
removalPolicy: RemovalPolicy.DESTROY, | |
defaultDatabaseName: "orders", | |
instances: dbClusterInstanceCount, | |
instanceProps: { | |
vpc: vpc, | |
deleteAutomatedBackups: true, | |
instanceType: | |
CustomInstanceType.SERVERLESS as unknown as ec2.InstanceType, | |
autoMinorVersionUpgrade: false, | |
publiclyAccessible: false, | |
vpcSubnets: { | |
subnetType: ec2.SubnetType.PRIVATE_ISOLATED, | |
}, | |
}, | |
backup: { | |
retention: Duration.days(1), | |
preferredWindow: "08:00-09:00", | |
}, | |
port: 5432, | |
cloudwatchLogsExports: ["postgresql"], | |
cloudwatchLogsRetention: logs.RetentionDays.ONE_DAY, | |
storageEncrypted: true, | |
} | |
); | |
const serverlessV2ScalingConfiguration = { | |
MinCapacity: 0.5, | |
MaxCapacity: 1, | |
}; | |
const dbConnectionGroup: ec2.SecurityGroup = new ec2.SecurityGroup( | |
this, | |
"RdsProxyDBConnection", | |
{ | |
vpc, | |
securityGroupName: "rds-proxy-sg", | |
} | |
); | |
dbConnectionGroup.addIngressRule( | |
dbConnectionGroup, | |
ec2.Port.tcp(5432), | |
"allow db connection" | |
); | |
const cfnDbCluster: rds.CfnDBCluster = dbCluster.node | |
.defaultChild as rds.CfnDBCluster; | |
const dbScalingConfigureTarget = dbScalingConfigure.node.findChild( | |
"Resource" | |
).node.defaultChild as CfnResource; | |
cfnDbCluster.addPropertyOverride("EngineMode", "provisioned"); | |
dbScalingConfigure.node.addDependency(cfnDbCluster); | |
for (let i = 1; i <= dbClusterInstanceCount; i++) { | |
( | |
dbCluster.node.findChild(`Instance${i}`) as rds.CfnDBInstance | |
).addDependsOn(dbScalingConfigureTarget); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment