Skip to content

Instantly share code, notes, and snippets.

@touzoku
Created August 2, 2022 08:53
Show Gist options
  • Save touzoku/edd81baf4b94d3fb0d9635d619beb191 to your computer and use it in GitHub Desktop.
Save touzoku/edd81baf4b94d3fb0d9635d619beb191 to your computer and use it in GitHub Desktop.
import {
Stack,
StackProps,
RemovalPolicy,
aws_rds as rds,
aws_ec2 as ec2,
custom_resources as cr,
CfnResource,
} from 'aws-cdk-lib'
import { Construct } from 'constructs'
export class DbChangeTest extends Stack {
constructor(scope: Construct, id: string, props: StackProps) {
super(scope, id, props)
const vpc = new ec2.Vpc(this, 'DBVpc', {
maxAzs: 2,
natGateways: 0,
})
const cluster = new rds.DatabaseCluster(this, 'TestDB', {
engine: rds.DatabaseClusterEngine.auroraMysql({
version: rds.AuroraMysqlEngineVersion.VER_3_02_0,
}),
defaultDatabaseName: 'test',
parameterGroup: new rds.ParameterGroup(this, 'MySQLParameterGroup', {
engine: rds.DatabaseClusterEngine.auroraMysql({
version: rds.AuroraMysqlEngineVersion.VER_3_02_0,
}),
description: 'Sets default encoding to utf8',
parameters: {
character_set_client: 'utf8mb4',
character_set_connection: 'utf8mb4',
character_set_database: 'utf8mb4',
character_set_results: 'utf8mb4',
character_set_server: 'utf8mb4',
collation_connection: 'utf8mb4_bin',
collation_server: 'utf8mb4_bin',
general_log: '1',
long_query_time: '3',
slow_query_log: '1',
server_audit_events:
'CONNECT,QUERY,QUERY_DCL,QUERY_DDL,QUERY_DML,TABLE',
server_audit_logging: '1',
server_audit_logs_upload: '1',
},
}),
instances: 1,
instanceProps: {
instanceType: 'serverless' as unknown as ec2.InstanceType,
vpc,
vpcSubnets: {
subnetType: ec2.SubnetType.PRIVATE_ISOLATED,
},
},
removalPolicy: RemovalPolicy.DESTROY,
})
const serverlessV2ScalingConfiguration = {
MinCapacity: 0.5,
MaxCapacity: 1,
}
const dbScalingConfigure = new cr.AwsCustomResource(
this,
'DbScalingConfigure',
{
onUpdate: {
service: 'RDS',
action: 'modifyDBCluster',
parameters: {
DBClusterIdentifier: cluster.clusterIdentifier,
ServerlessV2ScalingConfiguration: serverlessV2ScalingConfiguration,
},
physicalResourceId: cr.PhysicalResourceId.of(
cluster.clusterIdentifier
),
},
policy: cr.AwsCustomResourcePolicy.fromSdkCalls({
resources: cr.AwsCustomResourcePolicy.ANY_RESOURCE,
}),
}
)
const cfnDbCluster = cluster.node.defaultChild as rds.CfnDBCluster
const dbScalingConfigureTarget = dbScalingConfigure.node.findChild(
'Resource'
).node.defaultChild as CfnResource
cfnDbCluster.addPropertyOverride('EngineMode', 'provisioned')
dbScalingConfigure.node.addDependency(cfnDbCluster)
;(cluster.node.findChild(`Instance1`) as rds.CfnDBInstance).addDependsOn(
dbScalingConfigureTarget
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment