-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update RDS Construct #91
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,86 +1,98 @@ | ||
import { Construct } from 'constructs'; | ||
import { Aspects, RemovalPolicy } from 'aws-cdk-lib'; | ||
import { RemovalPolicy, Duration } from 'aws-cdk-lib'; | ||
import * as rds from 'aws-cdk-lib/aws-rds'; | ||
import * as ec2 from 'aws-cdk-lib/aws-ec2'; | ||
|
||
export interface DatabaseProps { | ||
/** | ||
* Props for enabling enhanced monitoring. | ||
*/ | ||
type MonitoringProps = { | ||
/** | ||
* Add cloud watch exports. | ||
*/ | ||
readonly cloudwatchLogsExports?: string[]; | ||
/** | ||
* Enable performance insights. | ||
*/ | ||
readonly enablePerformanceInsights?: boolean; | ||
/** | ||
* performance insights retention period | ||
*/ | ||
readonly performanceInsightsRetention?: rds.PerformanceInsightRetention; | ||
/** | ||
* Enable enhanced monitoring by specifying the interval | ||
*/ | ||
readonly enhancedMonitoringInterval?: Duration; | ||
}; | ||
|
||
export type DatabaseProps = MonitoringProps & { | ||
clusterIdentifier: string; | ||
defaultDatabaseName: string; | ||
parameterGroupName: string; | ||
username: string; | ||
version: rds.AuroraMysqlEngineVersion; | ||
masterSecretName: string; | ||
version: rds.AuroraPostgresEngineVersion; | ||
numberOfInstance: number; | ||
minACU: number; | ||
maxACU: number; | ||
allowDbSGIngressRule?: { | ||
peer: ec2.IPeer; | ||
description?: string; | ||
}[]; | ||
} | ||
dbPort: number; | ||
allowedInboundSG?: ec2.SecurityGroup; | ||
}; | ||
|
||
export class DatabaseConstruct extends Construct { | ||
readonly dbSecurityGroup: ec2.SecurityGroup; | ||
readonly dbCluster: rds.DatabaseCluster; | ||
|
||
constructor(scope: Construct, id: string, vpc: ec2.IVpc, props: DatabaseProps) { | ||
super(scope, id); | ||
const dbPort = 3306; | ||
|
||
const secret = new rds.DatabaseSecret(this, id + 'Secret', { | ||
const dbSecret = new rds.DatabaseSecret(this, id + 'DbSecret', { | ||
username: props.username, | ||
secretName: props.masterSecretName, | ||
}); | ||
|
||
this.dbSecurityGroup = new ec2.SecurityGroup(this, 'DbSecurityGroup', { | ||
vpc: vpc, | ||
allowAllOutbound: true, | ||
allowAllOutbound: false, | ||
allowAllIpv6Outbound: false, | ||
description: 'security group for OrcaBus RDS', | ||
}); | ||
|
||
if (props.allowDbSGIngressRule) { | ||
for (const i in props.allowDbSGIngressRule) { | ||
const sgProps = props.allowDbSGIngressRule[i]; | ||
this.dbSecurityGroup.addIngressRule( | ||
sgProps.peer, | ||
ec2.Port.tcp(dbPort), | ||
sgProps.description | ||
); | ||
} | ||
// give compute sg to access the rds | ||
if (props.allowedInboundSG) { | ||
this.dbSecurityGroup.addIngressRule( | ||
props.allowedInboundSG, | ||
ec2.Port.tcp(props.dbPort), | ||
'allow the OrcaBus compute sg to access db' | ||
); | ||
} | ||
|
||
this.dbCluster = new rds.DatabaseCluster(this, id + 'Cluster', { | ||
engine: rds.DatabaseClusterEngine.auroraMysql({ | ||
version: props.version, | ||
}), | ||
port: dbPort, | ||
instances: props.numberOfInstance, | ||
instanceProps: { | ||
vpc: vpc, | ||
vpcSubnets: { | ||
subnetType: ec2.SubnetType.PRIVATE_ISOLATED, | ||
}, | ||
securityGroups: [this.dbSecurityGroup], | ||
instanceType: new ec2.InstanceType('serverless'), | ||
parameterGroup: rds.ParameterGroup.fromParameterGroupName( | ||
this, | ||
id + 'ParameterGroup', | ||
props.parameterGroupName | ||
), | ||
}, | ||
|
||
removalPolicy: RemovalPolicy.DESTROY, | ||
credentials: rds.Credentials.fromSecret(secret), | ||
engine: rds.DatabaseClusterEngine.auroraPostgres({ version: props.version }), | ||
clusterIdentifier: props.clusterIdentifier, | ||
credentials: rds.Credentials.fromSecret(dbSecret), | ||
defaultDatabaseName: props.defaultDatabaseName, | ||
}); | ||
|
||
Aspects.of(this.dbCluster).add({ | ||
visit(node) { | ||
if (node instanceof rds.CfnDBCluster) { | ||
node.serverlessV2ScalingConfiguration = { | ||
minCapacity: props.minACU, | ||
maxCapacity: props.maxACU, | ||
}; | ||
} | ||
parameterGroup: rds.ParameterGroup.fromParameterGroupName( | ||
this, | ||
id + 'ParameterGroup', | ||
props.parameterGroupName | ||
), | ||
port: props.dbPort, | ||
removalPolicy: RemovalPolicy.DESTROY, | ||
securityGroups: [this.dbSecurityGroup], | ||
serverlessV2MaxCapacity: props.maxACU, | ||
serverlessV2MinCapacity: props.minACU, | ||
vpc: vpc, | ||
vpcSubnets: { | ||
subnetType: ec2.SubnetType.PRIVATE_ISOLATED, | ||
}, | ||
|
||
cloudwatchLogsExports: props.cloudwatchLogsExports, | ||
monitoringInterval: props.enhancedMonitoringInterval, | ||
|
||
writer: rds.ClusterInstance.serverlessV2('WriterClusterInstance', { | ||
enablePerformanceInsights: props.enablePerformanceInsights, | ||
}), | ||
}); | ||
} | ||
} |
This file contains 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
This file contains 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the idea that every microservice that needs the database will fetch this secret to connect?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am thinking this way but do you have any suggestion?
Realising this, I should have pass in the secret name so that it could be discovered by the microservice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is okay. Probably simplest to just have one secret for the whole database cluster.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ha, I was thinking similarly for the DB stack to set up databases / users for the different services and isolate them against each other... but let's start simple ;-)