Skip to content

Commit

Permalink
feat(docdb): added ability to enable performance insights (aws#24039)
Browse files Browse the repository at this point in the history
Added the ability to enable performance insights in the L2 construct for DocumentDB Cluster so it can be enabled programmatically in all instances inside the cluster.

> [CONTRIBUTING GUIDE]: https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md
> [DESIGN GUIDELINES]: https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md

Closes aws#24036.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
rodrigomata authored and homakk committed Mar 28, 2023
1 parent c8921bb commit bf2d11f
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 34 deletions.
20 changes: 20 additions & 0 deletions packages/@aws-cdk/aws-docdb/README.md
Expand Up @@ -161,3 +161,23 @@ const cluster = new docdb.DatabaseCluster(this, 'Database', {
cloudWatchLogsRetentionRole: myLogsPublishingRole, // Optional - a role will be created if not provided
});
```

## Enable Performance Insights

By enabling this feature it will be cascaded and enabled in all instances inside the cluster:

```ts
declare const vpc: ec2.Vpc;

const cluster = new docdb.DatabaseCluster(this, 'Database', {
masterUser: {
username: 'myuser',
},
instanceType: ec2.InstanceType.of(ec2.InstanceClass.MEMORY5, ec2.InstanceSize.LARGE),
vpcSubnets: {
subnetType: ec2.SubnetType.PUBLIC,
},
vpc,
enablePerformanceInsights: true, // Enable Performance Insights in all instances under this cluster
});
```
8 changes: 8 additions & 0 deletions packages/@aws-cdk/aws-docdb/lib/cluster.ts
Expand Up @@ -183,6 +183,13 @@ export interface DatabaseClusterProps {
* @default - a new role is created.
*/
readonly cloudWatchLogsRetentionRole?: IRole;

/**
* A value that indicates whether to enable Performance Insights for the instances in the DB Cluster.
*
* @default - false
*/
readonly enablePerformanceInsights?: boolean;
}

/**
Expand Down Expand Up @@ -509,6 +516,7 @@ export class DatabaseCluster extends DatabaseClusterBase {
dbInstanceIdentifier: instanceIdentifier,
// Instance properties
dbInstanceClass: databaseInstanceType(props.instanceType),
enablePerformanceInsights: props.enablePerformanceInsights,
});

instance.applyRemovalPolicy(props.removalPolicy, {
Expand Down
8 changes: 8 additions & 0 deletions packages/@aws-cdk/aws-docdb/lib/instance.ts
Expand Up @@ -165,6 +165,13 @@ export interface DatabaseInstanceProps {
* @default RemovalPolicy.Retain
*/
readonly removalPolicy?: cdk.RemovalPolicy

/**
* A value that indicates whether to enable Performance Insights for the DB Instance.
*
* @default - false
*/
readonly enablePerformanceInsights?: boolean;
}

/**
Expand Down Expand Up @@ -208,6 +215,7 @@ export class DatabaseInstance extends DatabaseInstanceBase implements IDatabaseI
availabilityZone: props.availabilityZone,
dbInstanceIdentifier: props.dbInstanceName,
preferredMaintenanceWindow: props.preferredMaintenanceWindow,
enablePerformanceInsights: props.enablePerformanceInsights,
});

this.cluster = props.cluster;
Expand Down
23 changes: 23 additions & 0 deletions packages/@aws-cdk/aws-docdb/test/cluster.test.ts
Expand Up @@ -706,6 +706,29 @@ describe('DatabaseCluster', () => {
});
});

test('can enable Performance Insights on instances', () => {
// GIVEN
const stack = testStack();
const vpc = new ec2.Vpc(stack, 'VPC');

// WHEN
new DatabaseCluster(stack, 'Database', {
masterUser: {
username: 'admin',
},
instanceType: ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.SMALL),
vpc,
enablePerformanceInsights: true,
});

// THEN
Template.fromStack(stack).hasResource('AWS::DocDB::DBInstance', {
Properties: {
EnablePerformanceInsights: true,
},
});
});

test('single user rotation', () => {
// GIVEN
const stack = testStack();
Expand Down
19 changes: 19 additions & 0 deletions packages/@aws-cdk/aws-docdb/test/instance.test.ts
Expand Up @@ -172,6 +172,25 @@ describe('DatabaseInstance', () => {
Value: `${instanceEndpointAddress}:${port}`,
});
});

test('can enable performance insights on instances', () => {
// GIVEN
const stack = testStack();

// WHEN
new DatabaseInstance(stack, 'Instance', {
cluster: stack.cluster,
instanceType: SINGLE_INSTANCE_TYPE,
enablePerformanceInsights: true,
});

// THEN
Template.fromStack(stack).hasResource('AWS::DocDB::DBInstance', {
Properties: {
EnablePerformanceInsights: true,
},
});
});
});

class TestStack extends cdk.Stack {
Expand Down
@@ -1,15 +1,15 @@
{
"version": "20.0.0",
"version": "30.1.0",
"files": {
"811c147f8ed74c3803d8df4f44e3f6a7e7779b21e729fc8cb3b66a685054a393": {
"f7cbfe0c634dda7840ec3c05f938c0badf4e3f4f4b183a058b1245d482d019bd": {
"source": {
"path": "aws-cdk-docdb-integ.template.json",
"packaging": "file"
},
"destinations": {
"current_account-current_region": {
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
"objectKey": "811c147f8ed74c3803d8df4f44e3f6a7e7779b21e729fc8cb3b66a685054a393.json",
"objectKey": "f7cbfe0c634dda7840ec3c05f938c0badf4e3f4f4b183a058b1245d482d019bd.json",
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
}
}
Expand Down
Expand Up @@ -533,7 +533,8 @@
"DBClusterIdentifier": {
"Ref": "DatabaseB269D8BB"
},
"DBInstanceClass": "db.r5.large"
"DBInstanceClass": "db.r5.large",
"EnablePerformanceInsights": true
},
"DependsOn": [
"VPCPublicSubnet1DefaultRoute91CEF279",
Expand Down
@@ -1 +1 @@
{"version":"20.0.0"}
{"version":"30.1.0"}
@@ -1,5 +1,5 @@
{
"version": "20.0.0",
"version": "30.1.0",
"testCases": {
"integ.cluster": {
"stacks": [
Expand Down
@@ -1,12 +1,6 @@
{
"version": "20.0.0",
"version": "30.1.0",
"artifacts": {
"Tree": {
"type": "cdk:tree",
"properties": {
"file": "tree.json"
}
},
"aws-cdk-docdb-integ.assets": {
"type": "cdk:asset-manifest",
"properties": {
Expand All @@ -23,7 +17,7 @@
"validateOnSynth": false,
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}",
"cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}",
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/811c147f8ed74c3803d8df4f44e3f6a7e7779b21e729fc8cb3b66a685054a393.json",
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/f7cbfe0c634dda7840ec3c05f938c0badf4e3f4f4b183a058b1245d482d019bd.json",
"requiresBootstrapStackVersion": 6,
"bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version",
"additionalDependencies": [
Expand Down Expand Up @@ -233,6 +227,12 @@
]
},
"displayName": "aws-cdk-docdb-integ"
},
"Tree": {
"type": "cdk:tree",
"properties": {
"file": "tree.json"
}
}
}
}
Expand Up @@ -4,14 +4,6 @@
"id": "App",
"path": "",
"children": {
"Tree": {
"id": "Tree",
"path": "Tree",
"constructInfo": {
"fqn": "constructs.Construct",
"version": "10.1.85"
}
},
"aws-cdk-docdb-integ": {
"id": "aws-cdk-docdb-integ",
"path": "aws-cdk-docdb-integ",
Expand Down Expand Up @@ -91,8 +83,8 @@
"id": "Acl",
"path": "aws-cdk-docdb-integ/VPC/PublicSubnet1/Acl",
"constructInfo": {
"fqn": "constructs.Construct",
"version": "10.1.85"
"fqn": "@aws-cdk/core.Resource",
"version": "0.0.0"
}
},
"RouteTable": {
Expand Down Expand Up @@ -258,8 +250,8 @@
"id": "Acl",
"path": "aws-cdk-docdb-integ/VPC/PublicSubnet2/Acl",
"constructInfo": {
"fqn": "constructs.Construct",
"version": "10.1.85"
"fqn": "@aws-cdk/core.Resource",
"version": "0.0.0"
}
},
"RouteTable": {
Expand Down Expand Up @@ -425,8 +417,8 @@
"id": "Acl",
"path": "aws-cdk-docdb-integ/VPC/PrivateSubnet1/Acl",
"constructInfo": {
"fqn": "constructs.Construct",
"version": "10.1.85"
"fqn": "@aws-cdk/core.Resource",
"version": "0.0.0"
}
},
"RouteTable": {
Expand Down Expand Up @@ -544,8 +536,8 @@
"id": "Acl",
"path": "aws-cdk-docdb-integ/VPC/PrivateSubnet2/Acl",
"constructInfo": {
"fqn": "constructs.Construct",
"version": "10.1.85"
"fqn": "@aws-cdk/core.Resource",
"version": "0.0.0"
}
},
"RouteTable": {
Expand Down Expand Up @@ -880,7 +872,8 @@
"dbClusterIdentifier": {
"Ref": "DatabaseB269D8BB"
},
"dbInstanceClass": "db.r5.large"
"dbInstanceClass": "db.r5.large",
"enablePerformanceInsights": true
}
},
"constructInfo": {
Expand All @@ -893,17 +886,41 @@
"fqn": "@aws-cdk/aws-docdb.DatabaseCluster",
"version": "0.0.0"
}
},
"BootstrapVersion": {
"id": "BootstrapVersion",
"path": "aws-cdk-docdb-integ/BootstrapVersion",
"constructInfo": {
"fqn": "@aws-cdk/core.CfnParameter",
"version": "0.0.0"
}
},
"CheckBootstrapVersion": {
"id": "CheckBootstrapVersion",
"path": "aws-cdk-docdb-integ/CheckBootstrapVersion",
"constructInfo": {
"fqn": "@aws-cdk/core.CfnRule",
"version": "0.0.0"
}
}
},
"constructInfo": {
"fqn": "@aws-cdk/core.Stack",
"version": "0.0.0"
}
},
"Tree": {
"id": "Tree",
"path": "Tree",
"constructInfo": {
"fqn": "constructs.Construct",
"version": "10.1.85"
"version": "10.1.270"
}
}
},
"constructInfo": {
"fqn": "constructs.Construct",
"version": "10.1.85"
"fqn": "@aws-cdk/core.App",
"version": "0.0.0"
}
}
}
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-docdb/test/integ.cluster.ts
Expand Up @@ -41,6 +41,7 @@ class TestStack extends cdk.Stack {
parameterGroup: params,
kmsKey,
removalPolicy: cdk.RemovalPolicy.DESTROY,
enablePerformanceInsights: true,
});

cluster.connections.allowDefaultPortFromAnyIpv4('Open to the world');
Expand Down

0 comments on commit bf2d11f

Please sign in to comment.