-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(redshift): optionally reboot Clusters to apply parameter changes (…
…#22063) Closes #22009 Currently waiting on #22055 and #22059 for the assertions in the integration test to successfully run ---- ### All Submissions: * [X] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) ### Adding new Unconventional Dependencies: * [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-new-unconventional-dependencies) ### New Features * [X] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)? * [X] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)? *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
- Loading branch information
Showing
34 changed files
with
9,010 additions
and
8 deletions.
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
53 changes: 53 additions & 0 deletions
53
packages/@aws-cdk/aws-redshift/lib/cluster-parameter-change-reboot-handler/index.ts
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
import { Redshift } from 'aws-sdk'; | ||
|
||
const redshift = new Redshift(); | ||
|
||
export async function handler(event: AWSLambda.CloudFormationCustomResourceEvent): Promise<void> { | ||
if (event.RequestType !== 'Delete') { | ||
return rebootClusterIfRequired(event.ResourceProperties?.ClusterId, event.ResourceProperties?.ParameterGroupName); | ||
} else { | ||
return; | ||
} | ||
} | ||
|
||
async function rebootClusterIfRequired(clusterId: string, parameterGroupName: string): Promise<void> { | ||
return executeActionForStatus(await getApplyStatus()); | ||
|
||
// https://docs.aws.amazon.com/redshift/latest/APIReference/API_ClusterParameterStatus.html | ||
async function executeActionForStatus(status: string, retryDurationMs?: number): Promise<void> { | ||
await sleep(retryDurationMs ?? 0); | ||
if (['pending-reboot', 'apply-deferred', 'apply-error'].includes(status)) { | ||
try { | ||
await redshift.rebootCluster({ ClusterIdentifier: clusterId }).promise(); | ||
} catch (err) { | ||
if ((err as any).code === 'InvalidClusterState') { | ||
return await executeActionForStatus(status, 30000); | ||
} else { | ||
throw err; | ||
} | ||
} | ||
return; | ||
} else if (['applying', 'retry'].includes(status)) { | ||
return executeActionForStatus(await getApplyStatus(), 30000); | ||
} | ||
return; | ||
} | ||
|
||
async function getApplyStatus(): Promise<string> { | ||
const clusterDetails = await redshift.describeClusters({ ClusterIdentifier: clusterId }).promise(); | ||
if (clusterDetails.Clusters?.[0].ClusterParameterGroups === undefined) { | ||
throw new Error(`Unable to find any Parameter Groups associated with ClusterId "${clusterId}".`); | ||
} | ||
for (const group of clusterDetails.Clusters?.[0].ClusterParameterGroups) { | ||
if (group.ParameterGroupName === parameterGroupName) { | ||
return group.ParameterApplyStatus ?? 'retry'; | ||
} | ||
} | ||
throw new Error(`Unable to find Parameter Group named "${parameterGroupName}" associated with ClusterId "${clusterId}".`); | ||
} | ||
} | ||
|
||
function sleep(ms: number) { | ||
return new Promise(resolve => setTimeout(resolve, ms)); | ||
} |
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
1 change: 1 addition & 0 deletions
1
...napshot/asset.1b88b7c3e3e0f8d3e27ded1bde51b7a80c75f3d8733872af7952c3a6d902147e/index.d.ts
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export declare function handler(event: AWSLambda.CloudFormationCustomResourceEvent): Promise<void>; |
56 changes: 56 additions & 0 deletions
56
....snapshot/asset.1b88b7c3e3e0f8d3e27ded1bde51b7a80c75f3d8733872af7952c3a6d902147e/index.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
53 changes: 53 additions & 0 deletions
53
....snapshot/asset.1b88b7c3e3e0f8d3e27ded1bde51b7a80c75f3d8733872af7952c3a6d902147e/index.ts
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
import { Redshift } from 'aws-sdk'; | ||
|
||
const redshift = new Redshift(); | ||
|
||
export async function handler(event: AWSLambda.CloudFormationCustomResourceEvent): Promise<void> { | ||
if (event.RequestType !== 'Delete') { | ||
return rebootClusterIfRequired(event.ResourceProperties?.ClusterId, event.ResourceProperties?.ParameterGroupName); | ||
} else { | ||
return; | ||
} | ||
} | ||
|
||
async function rebootClusterIfRequired(clusterId: string, parameterGroupName: string): Promise<void> { | ||
return executeActionForStatus(await getApplyStatus()); | ||
|
||
// https://docs.aws.amazon.com/redshift/latest/APIReference/API_ClusterParameterStatus.html | ||
async function executeActionForStatus(status: string, retryDurationMs?: number): Promise<void> { | ||
await sleep(retryDurationMs ?? 0); | ||
if (['pending-reboot', 'apply-deferred', 'apply-error', 'unknown-error'].includes(status)) { | ||
try { | ||
await redshift.rebootCluster({ ClusterIdentifier: clusterId }).promise(); | ||
} catch (err) { | ||
if ((<any>err).code === 'InvalidClusterState') { | ||
return await executeActionForStatus(status, 30000); | ||
} else { | ||
throw err; | ||
} | ||
} | ||
return; | ||
} else if (['applying', 'retry'].includes(status)) { | ||
return executeActionForStatus(await getApplyStatus(), 30000); | ||
} | ||
return; | ||
} | ||
|
||
async function getApplyStatus(): Promise<string> { | ||
const clusterDetails = await redshift.describeClusters({ ClusterIdentifier: clusterId }).promise(); | ||
if (clusterDetails.Clusters?.[0].ClusterParameterGroups === undefined) { | ||
throw new Error(`Unable to find any Parameter Groups associated with ClusterId "${clusterId}".`); | ||
} | ||
for (const group of clusterDetails.Clusters?.[0].ClusterParameterGroups) { | ||
if (group.ParameterGroupName === parameterGroupName) { | ||
return group.ParameterApplyStatus ?? 'retry'; | ||
} | ||
} | ||
throw new Error(`Unable to find Parameter Group named "${parameterGroupName}" associated with ClusterId "${clusterId}".`); | ||
} | ||
} | ||
|
||
function sleep(ms: number) { | ||
return new Promise(resolve => setTimeout(resolve, ms)); | ||
} |
Oops, something went wrong.