-
Notifications
You must be signed in to change notification settings - Fork 4k
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
feat(redshift): optionally reboot Clusters to apply parameter changes #22063
Changes from 1 commit
8b8798c
68ebe28
f262a52
6c1122b
c61f994
6fe9387
63400f6
a5d1ee5
153345a
f31b6dd
a01c334
52916e6
c445b0f
26fbbaa
b732dcf
692d1ab
762f2bf
dae7883
75ebd28
b6c323d
1c69185
80848a9
f91ffeb
a76b887
16ba85f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,18 @@ | ||
import * as path from 'path'; | ||
import * as ec2 from '@aws-cdk/aws-ec2'; | ||
import * as iam from '@aws-cdk/aws-iam'; | ||
import * as kms from '@aws-cdk/aws-kms'; | ||
import * as lambda from '@aws-cdk/aws-lambda'; | ||
import * as s3 from '@aws-cdk/aws-s3'; | ||
import * as secretsmanager from '@aws-cdk/aws-secretsmanager'; | ||
import { Duration, IResource, RemovalPolicy, Resource, SecretValue, Token } from '@aws-cdk/core'; | ||
import { Duration, IResource, RemovalPolicy, Resource, SecretValue, Token, CustomResource, Stack, ArnFormat } from '@aws-cdk/core'; | ||
import * as cr from '@aws-cdk/custom-resources'; | ||
import { Construct } from 'constructs'; | ||
import { DatabaseSecret } from './database-secret'; | ||
import { Endpoint } from './endpoint'; | ||
import { ClusterParameterGroup, IClusterParameterGroup } from './parameter-group'; | ||
import { CfnCluster } from './redshift.generated'; | ||
import { ClusterSubnetGroup, IClusterSubnetGroup } from './subnet-group'; | ||
|
||
/** | ||
* Possible Node Types to use in the cluster | ||
* used for defining {@link ClusterProps.nodeType}. | ||
|
@@ -354,6 +356,12 @@ export interface ClusterProps { | |
* @default - No Elastic IP | ||
*/ | ||
readonly elasticIp?: string | ||
|
||
/** | ||
* If this flag is set, the cluster will be rebooted when changes to the cluster's parameter group that require a restart to apply. | ||
* @default false | ||
*/ | ||
readonly rebootForParameterChanges?: boolean | ||
} | ||
|
||
/** | ||
|
@@ -452,6 +460,11 @@ export class Cluster extends ClusterBase { | |
*/ | ||
protected parameterGroup?: IClusterParameterGroup; | ||
|
||
/** | ||
* Whether the cluster will be rebooted when changes to the cluster's parameter group that require a restart to apply. | ||
*/ | ||
protected rebootForParameterChangesEnabled?: boolean; | ||
|
||
constructor(scope: Construct, id: string, props: ClusterProps) { | ||
super(scope, id); | ||
|
||
|
@@ -565,6 +578,9 @@ export class Cluster extends ClusterBase { | |
|
||
const defaultPort = ec2.Port.tcp(this.clusterEndpoint.port); | ||
this.connections = new ec2.Connections({ securityGroups, defaultPort }); | ||
if (props.rebootForParameterChanges) { | ||
this.enableRebootForParameterChanges(); | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -652,4 +668,54 @@ export class Cluster extends ClusterBase { | |
throw new Error('Cannot add a parameter to an imported parameter group.'); | ||
} | ||
} | ||
|
||
/** | ||
* Enables automatic cluster rebooting when changes to the cluster's parameter group require a restart to apply. | ||
*/ | ||
public enableRebootForParameterChanges(): void { | ||
if (!this.parameterGroup) { | ||
throw new Error('Cannot enable reboot for parameter changes when there is no associated ClusterParameterGroup.'); | ||
} | ||
if (!(this.parameterGroup instanceof ClusterParameterGroup)) { | ||
throw new Error('Cannot enable reboot for parameter changes when using an imported parameter group.'); | ||
} | ||
if (!this.rebootForParameterChangesEnabled) { | ||
this.rebootForParameterChangesEnabled = true; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The check on line 607 is all we need for this, no? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe we need it to make sure we don't make a duplicate Custom Resource. I could create class variables to make the function idempotent, but thought that may be overkill There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. making the function idempotent is definitely overkill, and guarding against these potential bugs (an error would be thrown complaining about duplicate construct IDs) is definitely the right call in general. In this case though I don't see how the function could be called twice, as it's only called in the constructor and it's internal-only. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I originally thought about making it constructor only, but It's currently not (example). I figured that exposing the method would be friendlier for situationally enabling the feature under specific conditions (ex. beta/prod stages, using the addToParameterGroup method and then using this method). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmmm okay. I don't love that we have this prop and this internal member, but it is a better user experience to be able to use a convenience method to set it, so we can leave it. Please change the format of this to a guard clause though, eg:
|
||
const rebootFunction = new lambda.SingletonFunction(this, 'RedshiftClusterRebooterFunction', { | ||
uuid: '511e207f-13df-4b8b-b632-c32b30b65ac2', | ||
runtime: lambda.Runtime.NODEJS_16_X, | ||
code: lambda.Code.fromAsset(path.join(__dirname, 'cluster-parameter-change-reboot-handler')), | ||
handler: 'index.handler', | ||
timeout: Duration.seconds(900), | ||
}); | ||
rebootFunction.addToRolePolicy(new iam.PolicyStatement({ | ||
actions: ['redshift:DescribeClusters'], | ||
resources: ['*'], | ||
})); | ||
rebootFunction.addToRolePolicy(new iam.PolicyStatement({ | ||
actions: ['reshift:RebootCluster'], | ||
resources: [ | ||
Stack.of(this).formatArn({ | ||
service: 'redshift', | ||
resource: 'cluster', | ||
resourceName: this.clusterName, | ||
arnFormat: ArnFormat.COLON_RESOURCE_NAME, | ||
}), | ||
], | ||
})); | ||
const provider = new cr.Provider(this, 'ResourceProvider', { | ||
onEventHandler: rebootFunction, | ||
}); | ||
const customResource = new CustomResource(this, 'RedshiftClusterRebooterCustomResource', { | ||
resourceType: 'Custom::RedshiftClusterRebooter', | ||
serviceToken: provider.serviceToken, | ||
properties: { | ||
ClusterId: this.cluster.getAtt('id'), | ||
ParameterGroupName: this.parameterGroup.clusterParameterGroupName, | ||
ParametersString: JSON.stringify(this.parameterGroup.parameters), | ||
}, | ||
}); | ||
customResource.node.addDependency(this, this.parameterGroup); | ||
} | ||
} | ||
} |
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 don't think we need this prop, can we remove it? It seems redundant with
rebootForParameterChanges
?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.
Related to my other comment
rebootForParameterChanges
is the class property that is needed so we don't create duplicate custom resources withenableRebootForParameterChanges()
. I thought it would be clearer to have this property versus making theProvider
andCustomResource
class propertiesThere 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.
Please add to this docstring that this is only used to guard against repeated invocations of
enableRebootForParameterChanges()