Use ModifyCluster with an Amazon SDK or CLI - Amazon Redshift
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Use ModifyCluster with an Amazon SDK or CLI

The following code examples show how to use ModifyCluster.

Action examples are code excerpts from larger programs and must be run in context. You can see this action in context in the following code example:

CLI
Amazon CLI

Associate a Security Group with a ClusterThis example shows how to associate a cluster security group with the specified cluster.Command:

aws redshift modify-cluster --cluster-identifier mycluster --cluster-security-groups mysecuritygroup

Modify the Maintenance Window for a ClusterThis shows how to change the weekly preferred maintenance window for a cluster to be the minimum four hour window starting Sundays at 11:15 PM, and ending Mondays at 3:15 AM.Command:

aws redshift modify-cluster --cluster-identifier mycluster --preferred-maintenance-window Sun:23:15-Mon:03:15

Change the Master Password for the ClusterThis example shows how to change the master password for a cluster.Command:

aws redshift modify-cluster --cluster-identifier mycluster --master-user-password A1b2c3d4
  • For API details, see ModifyCluster in Amazon CLI Command Reference.

Go
SDK for Go V2
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

import ( "context" "errors" "log" "time" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/redshift" "github.com/aws/aws-sdk-go-v2/service/redshift/types" ) // RedshiftActions wraps Redshift service actions. type RedshiftActions struct { RedshiftClient *redshift.Client } // ModifyCluster sets the preferred maintenance window for the given cluster. func (actor RedshiftActions) ModifyCluster(ctx context.Context, clusterId string, maintenanceWindow string) *redshift.ModifyClusterOutput { // Modify the cluster's maintenance window input := &redshift.ModifyClusterInput{ ClusterIdentifier: aws.String(clusterId), PreferredMaintenanceWindow: aws.String(maintenanceWindow), } var opErr *types.InvalidClusterStateFault output, err := actor.RedshiftClient.ModifyCluster(ctx, input) if err != nil && errors.As(err, &opErr) { log.Println("Cluster is in an invalid state.") panic(err) } else if err != nil { log.Printf("Failed to modify Redshift cluster: %v\n", err) panic(err) } log.Printf("The cluster was successfully modified and now has %s as the maintenance window\n", *output.Cluster.PreferredMaintenanceWindow) return output }
  • For API details, see ModifyCluster in Amazon SDK for Go API Reference.

Java
SDK for Java 2.x
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

Modify a cluster.

/** * Modifies an Amazon Redshift cluster asynchronously. * * @param clusterId the identifier of the cluster to be modified * @return a {@link CompletableFuture} that completes when the cluster modification is complete */ public CompletableFuture<ModifyClusterResponse> modifyClusterAsync(String clusterId) { ModifyClusterRequest modifyClusterRequest = ModifyClusterRequest.builder() .clusterIdentifier(clusterId) .preferredMaintenanceWindow("wed:07:30-wed:08:00") .build(); return getAsyncClient().modifyCluster(modifyClusterRequest) .whenComplete((clusterResponse, exception) -> { if (exception != null) { if (exception.getCause() instanceof RedshiftException) { logger.info("Error: {} ", exception.getMessage()); } else { logger.info("Unexpected error: {} ", exception.getMessage()); } } else { logger.info("The modified cluster was successfully modified and has " + clusterResponse.cluster().preferredMaintenanceWindow() + " as the maintenance window"); } }); }
  • For API details, see ModifyCluster in Amazon SDK for Java 2.x API Reference.

JavaScript
SDK for JavaScript (v3)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

Create the client.

import { RedshiftClient } from "@aws-sdk/client-redshift"; // Set the AWS Region. const REGION = "REGION"; //Set the Redshift Service Object const redshiftClient = new RedshiftClient({ region: REGION }); export { redshiftClient };

Modify a cluster.

// Import required AWS SDK clients and commands for Node.js import { ModifyClusterCommand } from "@aws-sdk/client-redshift"; import { redshiftClient } from "./libs/redshiftClient.js"; // Set the parameters const params = { ClusterIdentifier: "CLUSTER_NAME", MasterUserPassword: "NEW_MASTER_USER_PASSWORD", }; const run = async () => { try { const data = await redshiftClient.send(new ModifyClusterCommand(params)); console.log("Success was modified.", data); return data; // For unit tests. } catch (err) { console.log("Error", err); } }; run();
  • For API details, see ModifyCluster in Amazon SDK for JavaScript API Reference.

Kotlin
SDK for Kotlin
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

Modify a cluster.

suspend fun modifyCluster(clusterId: String?) { val modifyClusterRequest = ModifyClusterRequest { clusterIdentifier = clusterId preferredMaintenanceWindow = "wed:07:30-wed:08:00" } RedshiftClient { region = "us-west-2" }.use { redshiftClient -> val clusterResponse = redshiftClient.modifyCluster(modifyClusterRequest) println( "The modified cluster was successfully modified and has ${clusterResponse.cluster?.preferredMaintenanceWindow} as the maintenance window", ) } }
  • For API details, see ModifyCluster in Amazon SDK for Kotlin API reference.

Python
SDK for Python (Boto3)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

class RedshiftWrapper: """ Encapsulates Amazon Redshift cluster operations. """ def __init__(self, redshift_client): """ :param redshift_client: A Boto3 Redshift client. """ self.client = redshift_client def modify_cluster(self, cluster_identifier, preferred_maintenance_window): """ Modifies a cluster. :param cluster_identifier: The cluster identifier. :param preferred_maintenance_window: The preferred maintenance window. """ try: self.client.modify_cluster( ClusterIdentifier=cluster_identifier, PreferredMaintenanceWindow=preferred_maintenance_window, ) except ClientError as err: logging.error( "Couldn't modify a cluster. Here's why: %s: %s", err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise

The following code instantiates the RedshiftWrapper object.

client = boto3.client("redshift") redhift_wrapper = RedshiftWrapper(client)
  • For API details, see ModifyCluster in Amazon SDK for Python (Boto3) API Reference.

For a complete list of Amazon SDK developer guides and code examples, see Using this service with an Amazon SDK. This topic also includes information about getting started and details about previous SDK versions.