Using AWS SDKs
The AWS SDKs provide programmatic access to Aurora DSQL in your preferred programming language. The following sections show how to perform common cluster operations using different programming languages.
Create cluster
The following examples show how to create a multi-Region cluster using different programming languages.
- Python
-
To create a multi-Region cluster, use the following example. Creating a multi-Region cluster might take some time.
import boto3 def create_multi_region_clusters(region_1, region_2, witness_region): try: client_1 = boto3.client("dsql", region_name=region_1) client_2 = boto3.client("dsql", region_name=region_2) # We can only set the witness region for the first cluster cluster_1 = client_1.create_cluster( deletionProtectionEnabled=True, multiRegionProperties={"witnessRegion": witness_region}, tags={"Name": "Python multi region cluster"} ) print(f"Created {cluster_1["arn"]}") # For the second cluster we can set witness region and designate cluster_1 as a peer cluster_2 = client_2.create_cluster( deletionProtectionEnabled=True, multiRegionProperties={"witnessRegion": witness_region, "clusters": [cluster_1["arn"]]}, tags={"Name": "Python multi region cluster"} ) print(f"Created {cluster_2["arn"]}") # Now that we know the cluster_2 arn we can set it as a peer of cluster_1 client_1.update_cluster( identifier=cluster_1["identifier"], multiRegionProperties={"witnessRegion": witness_region, "clusters": [cluster_2["arn"]]} ) print(f"Added {cluster_2["arn"]} as a peer of {cluster_1["arn"]}") # Now that multiRegionProperties is fully defined for both clusters # they'll begin the transition to ACTIVE print(f"Waiting for {cluster_1["arn"]} to become ACTIVE") client_1.get_waiter("cluster_active").wait( identifier=cluster_1["identifier"], WaiterConfig={ 'Delay': 10, 'MaxAttempts': 30 } ) print(f"Waiting for {cluster_2["arn"]} to become ACTIVE") client_2.get_waiter("cluster_active").wait( identifier=cluster_2["identifier"], WaiterConfig={ 'Delay': 10, 'MaxAttempts': 30 } ) return (cluster_1, cluster_2) except: print("Unable to create cluster") raise def main(): region_1 = "us-east-1" region_2 = "us-east-2" witness_region = "us-west-2" (cluster_1, cluster_2) = create_multi_region_clusters(region_1, region_2, witness_region) print("Created multi region clusters:") print("Cluster id: " + cluster_1['arn']) print("Cluster id: " + cluster_2['arn']) if __name__ == "__main__": main()
- C++
-
To create a multi-Region cluster, use the following example. Creating a multi-Region cluster might take some time.
#include <aws/core/Aws.h> #include <aws/core/utils/Outcome.h> #include <aws/dsql/DSQLClient.h> #include <aws/dsql/model/CreateClusterRequest.h> #include <aws/dsql/model/UpdateClusterRequest.h> #include <aws/dsql/model/MultiRegionProperties.h> #include <aws/dsql/model/GetClusterRequest.h> #include <iostream> #include <thread> #include <chrono> using namespace Aws; using namespace Aws::DSQL; using namespace Aws::DSQL::Model; /** * Creates multi-region clusters in Amazon Aurora DSQL */ std::pair<CreateClusterResult, CreateClusterResult> CreateMultiRegionClusters( const Aws::String& region1, const Aws::String& region2, const Aws::String& witnessRegion) { // Create clients for each region DSQL::DSQLClientConfiguration clientConfig1; clientConfig1.region = region1; DSQL::DSQLClient client1(clientConfig1); DSQL::DSQLClientConfiguration clientConfig2; clientConfig2.region = region2; DSQL::DSQLClient client2(clientConfig2); std::cout << "Creating cluster in " << region1 << std::endl; CreateClusterRequest createClusterRequest1; createClusterRequest1.SetDeletionProtectionEnabled(true); // Set multi-region properties with witness region MultiRegionProperties multiRegionProps1; multiRegionProps1.SetWitnessRegion(witnessRegion); createClusterRequest1.SetMultiRegionProperties(multiRegionProps1); // Add tags Aws::Map<Aws::String, Aws::String> tags; tags["Name"] = "cpp multi region cluster 1"; createClusterRequest1.SetTags(tags); createClusterRequest1.SetClientToken(Aws::Utils::UUID::RandomUUID()); auto createOutcome1 = client1.CreateCluster(createClusterRequest1); if (!createOutcome1.IsSuccess()) { std::cerr << "Failed to create cluster in " << region1 << ": " << createOutcome1.GetError().GetMessage() << std::endl; throw std::runtime_error("Failed to create multi-region clusters"); } auto cluster1 = createOutcome1.GetResult(); std::cout << "Created " << cluster1.GetArn() << std::endl; // Create second cluster std::cout << "Creating cluster in " << region2 << std::endl; CreateClusterRequest createClusterRequest2; createClusterRequest2.SetDeletionProtectionEnabled(true); // Set multi-region properties with witness region and cluster1 as peer MultiRegionProperties multiRegionProps2; multiRegionProps2.SetWitnessRegion(witnessRegion); Aws::Vector<Aws::String> clusters; clusters.push_back(cluster1.GetArn()); multiRegionProps2.SetClusters(clusters); tags["Name"] = "cpp multi region cluster 2"; createClusterRequest2.SetMultiRegionProperties(multiRegionProps2); createClusterRequest2.SetTags(tags); createClusterRequest2.SetClientToken(Aws::Utils::UUID::RandomUUID()); auto createOutcome2 = client2.CreateCluster(createClusterRequest2); if (!createOutcome2.IsSuccess()) { std::cerr << "Failed to create cluster in " << region2 << ": " << createOutcome2.GetError().GetMessage() << std::endl; throw std::runtime_error("Failed to create multi-region clusters"); } auto cluster2 = createOutcome2.GetResult(); std::cout << "Created " << cluster2.GetArn() << std::endl; // Now that we know the cluster2 arn we can set it as a peer of cluster1 UpdateClusterRequest updateClusterRequest; updateClusterRequest.SetIdentifier(cluster1.GetIdentifier()); MultiRegionProperties updatedProps; updatedProps.SetWitnessRegion(witnessRegion); Aws::Vector<Aws::String> updatedClusters; updatedClusters.push_back(cluster2.GetArn()); updatedProps.SetClusters(updatedClusters); updateClusterRequest.SetMultiRegionProperties(updatedProps); updateClusterRequest.SetClientToken(Aws::Utils::UUID::RandomUUID()); auto updateOutcome = client1.UpdateCluster(updateClusterRequest); if (!updateOutcome.IsSuccess()) { std::cerr << "Failed to update cluster in " << region1 << ": " << updateOutcome.GetError().GetMessage() << std::endl; throw std::runtime_error("Failed to update multi-region clusters"); } std::cout << "Added " << cluster2.GetArn() << " as a peer of " << cluster1.GetArn() << std::endl; return std::make_pair(cluster1, cluster2); } int main() { Aws::SDKOptions options; Aws::InitAPI(options); { try { // Define regions for the multi-region setup Aws::String region1 = "us-east-1"; Aws::String region2 = "us-east-2"; Aws::String witnessRegion = "us-west-2"; auto [cluster1, cluster2] = CreateMultiRegionClusters(region1, region2, witnessRegion); std::cout << "Created multi region clusters:" << std::endl; std::cout << "Cluster 1 ARN: " << cluster1.GetArn() << std::endl; std::cout << "Cluster 2 ARN: " << cluster2.GetArn() << std::endl; } catch (const std::exception& e) { std::cerr << "Error: " << e.what() << std::endl; } } Aws::ShutdownAPI(options); return 0; }
- JavaScript
-
To create a multi-Region cluster, use the following example. Creating a multi-Region cluster might take some time.
import { DSQLClient, CreateClusterCommand, UpdateClusterCommand, waitUntilClusterActive } from "@aws-sdk/client-dsql"; async function createMultiRegionCluster(region1, region2, witnessRegion) { const client1 = new DSQLClient({ region: region1 }); const client2 = new DSQLClient({ region: region2 }); try { // We can only set the witness region for the first cluster console.log(`Creating cluster in ${region1}`); const createClusterCommand1 = new CreateClusterCommand({ deletionProtectionEnabled: true, tags: { Name: "javascript multi region cluster 1" }, multiRegionProperties: { witnessRegion: witnessRegion } }); const response1 = await client1.send(createClusterCommand1); console.log(`Created ${response1.arn}`); // For the second cluster we can set witness region and designate the first cluster as a peer console.log(`Creating cluster in ${region2}`); const createClusterCommand2 = new CreateClusterCommand({ deletionProtectionEnabled: true, tags: { Name: "javascript multi region cluster 2" }, multiRegionProperties: { witnessRegion: witnessRegion, clusters: [response1.arn] } }); const response2 = await client2.send(createClusterCommand2); console.log(`Created ${response2.arn}`); // Now that we know the second cluster arn we can set it as a peer of the first cluster const updateClusterCommand = new UpdateClusterCommand({ identifier: response1.identifier, multiRegionProperties: { witnessRegion: witnessRegion, clusters: [response2.arn] } }); await client1.send(updateClusterCommand); console.log(`Added ${response2.arn} as a peer of ${response1.arn}`); // Now that multiRegionProperties is fully defined for both clusters they'll begin the transition to ACTIVE console.log(`Waiting for cluster ${response1.identifier} to become ACTIVE`); await waitUntilClusterActive( { client: client1, maxWaitTime: 300 // Wait for 5 minutes }, { identifier: response1.identifier } ); console.log(`Cluster 1 is now active`); console.log(`Waiting for cluster ${response2.identifier} to become ACTIVE`); await waitUntilClusterActive( { client: client2, maxWaitTime: 300 // Wait for 5 minutes }, { identifier: response2.identifier } ); console.log(`Cluster 2 is now active`); console.log("The multi region clusters are now active"); return; } catch (error) { console.error("Failed to create cluster: ", error.message); throw error; } } async function main() { const region1 = "us-east-1"; const region2 = "us-east-2"; const witnessRegion = "us-west-2"; await createMultiRegionCluster(region1, region2, witnessRegion); } main();
- Java
-
To create a multi-Region cluster, use the following example. Creating a multi-Region cluster might take some time.
package org.example; import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.retries.api.BackoffStrategy; import software.amazon.awssdk.services.dsql.DsqlClient; import software.amazon.awssdk.services.dsql.DsqlClientBuilder; import software.amazon.awssdk.services.dsql.model.CreateClusterRequest; import software.amazon.awssdk.services.dsql.model.CreateClusterResponse; import software.amazon.awssdk.services.dsql.model.GetClusterResponse; import software.amazon.awssdk.services.dsql.model.UpdateClusterRequest; import java.time.Duration; import java.util.Map; public class CreateMultiRegionCluster { public static void main(String[] args) { Region region1 = Region.US_EAST_1; Region region2 = Region.US_EAST_2; Region witnessRegion = Region.US_WEST_2; DsqlClientBuilder clientBuilder = DsqlClient.builder() .credentialsProvider(DefaultCredentialsProvider.create()); try ( DsqlClient client1 = clientBuilder.region(region1).build(); DsqlClient client2 = clientBuilder.region(region2).build() ) { // We can only set the witness region for the first cluster System.out.println("Creating cluster in " + region1); CreateClusterRequest request1 = CreateClusterRequest.builder() .deletionProtectionEnabled(true) .multiRegionProperties(mrp -> mrp.witnessRegion(witnessRegion.toString())) .tags(Map.of("Name", "java multi region cluster")) .build(); CreateClusterResponse cluster1 = client1.createCluster(request1); System.out.println("Created " + cluster1.arn()); // For the second cluster we can set the witness region and designate // cluster1 as a peer. System.out.println("Creating cluster in " + region2); CreateClusterRequest request2 = CreateClusterRequest.builder() .deletionProtectionEnabled(true) .multiRegionProperties(mrp -> mrp.witnessRegion(witnessRegion.toString()).clusters(cluster1.arn()) ) .tags(Map.of("Name", "java multi region cluster")) .build(); CreateClusterResponse cluster2 = client2.createCluster(request2); System.out.println("Created " + cluster2.arn()); // Now that we know the cluster2 ARN we can set it as a peer of cluster1 UpdateClusterRequest updateReq = UpdateClusterRequest.builder() .identifier(cluster1.identifier()) .multiRegionProperties(mrp -> mrp.witnessRegion(witnessRegion.toString()).clusters(cluster2.arn()) ) .build(); client1.updateCluster(updateReq); System.out.printf("Added %s as a peer of %s%n", cluster2.arn(), cluster1.arn()); // Now that MultiRegionProperties is fully defined for both clusters they'll begin // the transition to ACTIVE. System.out.printf("Waiting for cluster %s to become ACTIVE%n", cluster1.arn()); GetClusterResponse activeCluster1 = client1.waiter().waitUntilClusterActive( getCluster -> getCluster.identifier(cluster1.identifier()), config -> config.backoffStrategyV2( BackoffStrategy.fixedDelayWithoutJitter(Duration.ofSeconds(10)) ).waitTimeout(Duration.ofMinutes(5)) ).matched().response().orElseThrow(); System.out.printf("Waiting for cluster %s to become ACTIVE%n", cluster2.arn()); GetClusterResponse activeCluster2 = client2.waiter().waitUntilClusterActive( getCluster -> getCluster.identifier(cluster2.identifier()), config -> config.backoffStrategyV2( BackoffStrategy.fixedDelayWithoutJitter(Duration.ofSeconds(10)) ).waitTimeout(Duration.ofMinutes(5)) ).matched().response().orElseThrow(); System.out.println("Created multi region clusters:"); System.out.println(activeCluster1); System.out.println(activeCluster2); } } }
- Rust
-
To create a multi-Region cluster, use the following example. Creating a multi-Region cluster might take some time.
use aws_config::{BehaviorVersion, Region, load_defaults}; use aws_sdk_dsql::client::Waiters; use aws_sdk_dsql::operation::get_cluster::GetClusterOutput; use aws_sdk_dsql::types::MultiRegionProperties; use aws_sdk_dsql::{Client, Config}; use std::collections::HashMap; /// Create a client. We will use this later for performing operations on the cluster. async fn dsql_client(region: &'static str) -> Client { // Load default SDK configuration let sdk_defaults = load_defaults(BehaviorVersion::latest()).await; // You can set your own credentials by following this guide // https://docs.aws.amazon.com/sdk-for-rust/latest/dg/credproviders.html let credentials = sdk_defaults.credentials_provider().unwrap(); let config = Config::builder() .behavior_version(BehaviorVersion::latest()) .credentials_provider(credentials) .region(Region::new(region)) .build(); Client::from_conf(config) } /// Create a cluster without delete protection and a name pub async fn create_multi_region_clusters( region_1: &'static str, region_2: &'static str, witness_region: &'static str, ) -> (GetClusterOutput, GetClusterOutput) { let client_1 = dsql_client(region_1).await; let client_2 = dsql_client(region_2).await; let tags = HashMap::from([( String::from("Name"), String::from("rust multi region cluster"), )]); // We can only set the witness region for the first cluster println!("Creating cluster in {region_1}"); let cluster_1 = client_1 .create_cluster() .set_tags(Some(tags.clone())) .deletion_protection_enabled(true) .multi_region_properties( MultiRegionProperties::builder() .witness_region(witness_region) .build(), ) .send() .await .unwrap(); let cluster_1_arn = &cluster_1.arn; println!("Created {cluster_1_arn}"); // For the second cluster we can set witness region and designate cluster_1 as a peer println!("Creating cluster in {region_2}"); let cluster_2 = client_2 .create_cluster() .set_tags(Some(tags)) .deletion_protection_enabled(true) .multi_region_properties( MultiRegionProperties::builder() .witness_region(witness_region) .clusters(&cluster_1.arn) .build(), ) .send() .await .unwrap(); let cluster_2_arn = &cluster_2.arn; println!("Created {cluster_2_arn}"); // Now that we know the cluster_2 arn we can set it as a peer of cluster_1 client_1 .update_cluster() .identifier(&cluster_1.identifier) .multi_region_properties( MultiRegionProperties::builder() .witness_region(witness_region) .clusters(&cluster_2.arn) .build(), ) .send() .await .unwrap(); println!("Added {cluster_2_arn} as a peer of {cluster_1_arn}"); // Now that the multi-region properties are fully defined for both clusters // they'll begin the transition to ACTIVE println!("Waiting for {cluster_1_arn} to become ACTIVE"); let cluster_1_output = client_1 .wait_until_cluster_active() .identifier(&cluster_1.identifier) .wait(std::time::Duration::from_secs(300)) // Wait up to 5 minutes .await .unwrap() .into_result() .unwrap(); println!("Waiting for {cluster_2_arn} to become ACTIVE"); let cluster_2_output = client_2 .wait_until_cluster_active() .identifier(&cluster_2.identifier) .wait(std::time::Duration::from_secs(300)) // Wait up to 5 minutes .await .unwrap() .into_result() .unwrap(); (cluster_1_output, cluster_2_output) } #[tokio::main(flavor = "current_thread")] pub async fn main() -> anyhow::Result<()> { let region_1 = "us-east-1"; let region_2 = "us-east-2"; let witness_region = "us-west-2"; let (cluster_1, cluster_2) = create_multi_region_clusters(region_1, region_2, witness_region).await; println!("Created multi region clusters:"); println!("{:#?}", cluster_1); println!("{:#?}", cluster_2); Ok(()) }
- Ruby
-
To create a multi-Region cluster, use the following example. Creating a multi-Region cluster might take some time.
require "aws-sdk-dsql" require "pp" def create_multi_region_clusters(region_1, region_2, witness_region) client_1 = Aws::DSQL::Client.new(region: region_1) client_2 = Aws::DSQL::Client.new(region: region_2) # We can only set the witness region for the first cluster puts "Creating cluster in #{region_1}" cluster_1 = client_1.create_cluster( deletion_protection_enabled: true, multi_region_properties: { witness_region: witness_region }, tags: { Name: "ruby multi region cluster" } ) puts "Created #{cluster_1.arn}" # For the second cluster we can set witness region and designate cluster_1 as a peer puts "Creating cluster in #{region_2}" cluster_2 = client_2.create_cluster( deletion_protection_enabled: true, multi_region_properties: { witness_region: witness_region, clusters: [ cluster_1.arn ] }, tags: { Name: "ruby multi region cluster" } ) puts "Created #{cluster_2.arn}" # Now that we know the cluster_2 arn we can set it as a peer of cluster_1 client_1.update_cluster( identifier: cluster_1.identifier, multi_region_properties: { witness_region: witness_region, clusters: [ cluster_2.arn ] } ) puts "Added #{cluster_2.arn} as a peer of #{cluster_1.arn}" # Now that multi_region_properties is fully defined for both clusters # they'll begin the transition to ACTIVE puts "Waiting for #{cluster_1.arn} to become ACTIVE" cluster_1 = client_1.wait_until(:cluster_active, identifier: cluster_1.identifier) do |w| # Wait for 5 minutes w.max_attempts = 30 w.delay = 10 end puts "Waiting for #{cluster_2.arn} to become ACTIVE" cluster_2 = client_2.wait_until(:cluster_active, identifier: cluster_2.identifier) do |w| w.max_attempts = 30 w.delay = 10 end [ cluster_1, cluster_2 ] rescue Aws::Errors::ServiceError => e abort "Failed to create multi-region clusters: #{e.message}" end def main region_1 = "us-east-1" region_2 = "us-east-2" witness_region = "us-west-2" cluster_1, cluster_2 = create_multi_region_clusters(region_1, region_2, witness_region) puts "Created multi region clusters:" pp cluster_1 pp cluster_2 end main if $PROGRAM_NAME == __FILE__
- Golang
-
To create a multi-Region cluster, use the following example. Creating a multi-Region cluster might take some time.
package main import ( "context" "fmt" "log" "time" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/config" "github.com/aws/aws-sdk-go-v2/service/dsql" dtypes "github.com/aws/aws-sdk-go-v2/service/dsql/types" ) func CreateMultiRegionClusters(ctx context.Context, witness, region1, region2 string) error { cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(region1)) if err != nil { log.Fatalf("Failed to load AWS configuration: %v", err) } // Create a DSQL region 1 client client := dsql.NewFromConfig(cfg) cfg2, err := config.LoadDefaultConfig(ctx, config.WithRegion(region2)) if err != nil { log.Fatalf("Failed to load AWS configuration: %v", err) } // Create a DSQL region 2 client client2 := dsql.NewFromConfig(cfg2, func(o *dsql.Options) { o.Region = region2 }) // Create cluster deleteProtect := true // We can only set the witness region for the first cluster input := &dsql.CreateClusterInput{ DeletionProtectionEnabled: &deleteProtect, MultiRegionProperties: &dtypes.MultiRegionProperties{ WitnessRegion: aws.String(witness), }, Tags: map[string]string{ "Name": "go multi-region cluster", }, } clusterProperties, err := client.CreateCluster(context.Background(), input) if err != nil { return fmt.Errorf("failed to create first cluster: %v", err) } // create second cluster cluster2Arns := []string{*clusterProperties.Arn} // For the second cluster we can set witness region and designate the first cluster as a peer input2 := &dsql.CreateClusterInput{ DeletionProtectionEnabled: &deleteProtect, MultiRegionProperties: &dtypes.MultiRegionProperties{ WitnessRegion: aws.String("us-west-2"), Clusters: cluster2Arns, }, Tags: map[string]string{ "Name": "go multi-region cluster", }, } clusterProperties2, err := client2.CreateCluster(context.Background(), input2) if err != nil { return fmt.Errorf("failed to create second cluster: %v", err) } // link initial cluster to second cluster cluster1Arns := []string{*clusterProperties2.Arn} // Now that we know the second cluster arn we can set it as a peer of the first cluster input3 := dsql.UpdateClusterInput{ Identifier: clusterProperties.Identifier, MultiRegionProperties: &dtypes.MultiRegionProperties{ WitnessRegion: aws.String("us-west-2"), Clusters: cluster1Arns, }} _, err = client.UpdateCluster(context.Background(), &input3) if err != nil { return fmt.Errorf("failed to update cluster to associate with first cluster. %v", err) } // Create the waiter with our custom options for first cluster waiter := dsql.NewClusterActiveWaiter(client, func(o *dsql.ClusterActiveWaiterOptions) { o.MaxDelay = 30 * time.Second // Creating a multi-region cluster can take a few minutes o.MinDelay = 10 * time.Second o.LogWaitAttempts = true }) // Now that multiRegionProperties is fully defined for both clusters // they'll begin the transition to ACTIVE // Create the input for the clusterProperties to monitor for first cluster getInput := &dsql.GetClusterInput{ Identifier: clusterProperties.Identifier, } // Wait for the first cluster to become active fmt.Printf("Waiting for first cluster %s to become active...\n", *clusterProperties.Identifier) err = waiter.Wait(ctx, getInput, 5*time.Minute) if err != nil { return fmt.Errorf("error waiting for first cluster to become active: %w", err) } // Create the waiter with our custom options waiter2 := dsql.NewClusterActiveWaiter(client2, func(o *dsql.ClusterActiveWaiterOptions) { o.MaxDelay = 30 * time.Second // Creating a multi-region cluster can take a few minutes o.MinDelay = 10 * time.Second o.LogWaitAttempts = true }) // Create the input for the clusterProperties to monitor for second getInput2 := &dsql.GetClusterInput{ Identifier: clusterProperties2.Identifier, } // Wait for the second cluster to become active fmt.Printf("Waiting for second cluster %s to become active...\n", *clusterProperties2.Identifier) err = waiter2.Wait(ctx, getInput2, 5*time.Minute) if err != nil { return fmt.Errorf("error waiting for second cluster to become active: %w", err) } fmt.Printf("Cluster %s is now active\n", *clusterProperties.Identifier) fmt.Printf("Cluster %s is now active\n", *clusterProperties2.Identifier) return nil } // Example usage in main function func main() { // Set up context with timeout ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute) defer cancel() err := CreateMultiRegionClusters(ctx, "us-west-2", "us-east-1", "us-east-2") if err != nil { fmt.Printf("failed to create multi-region clusters: %v", err) panic(err) } }
- .NET
-
To create a multi-Region cluster, use the following example. Creating a multi-Region cluster might take some time.
using System; using System.Collections.Generic; using System.Threading.Tasks; using Amazon; using Amazon.DSQL; using Amazon.DSQL.Model; using Amazon.Runtime.Credentials; using Amazon.Runtime.Endpoints; namespace DSQLExamples.examples { public class CreateMultiRegionClusters { /// <summary> /// Create a client. We will use this later for performing operations on the cluster. /// </summary> private static async Task<AmazonDSQLClient> CreateDSQLClient(RegionEndpoint region) { var awsCredentials = await DefaultAWSCredentialsIdentityResolver.GetCredentialsAsync(); var clientConfig = new AmazonDSQLConfig { RegionEndpoint = region, }; return new AmazonDSQLClient(awsCredentials, clientConfig); } /// <summary> /// Create multi-region clusters with a witness region. /// </summary> public static async Task<(CreateClusterResponse, CreateClusterResponse)> Create( RegionEndpoint region1, RegionEndpoint region2, RegionEndpoint witnessRegion) { using (var client1 = await CreateDSQLClient(region1)) using (var client2 = await CreateDSQLClient(region2)) { var tags = new Dictionary<string, string> { { "Name", "csharp multi region cluster" } }; // We can only set the witness region for the first cluster var createClusterRequest1 = new CreateClusterRequest { DeletionProtectionEnabled = true, Tags = tags, MultiRegionProperties = new MultiRegionProperties { WitnessRegion = witnessRegion.SystemName } }; var cluster1 = await client1.CreateClusterAsync(createClusterRequest1); var cluster1Arn = cluster1.Arn; Console.WriteLine($"Initiated creation of {cluster1Arn}"); // For the second cluster we can set witness region and designate cluster1 as a peer var createClusterRequest2 = new CreateClusterRequest { DeletionProtectionEnabled = true, Tags = tags, MultiRegionProperties = new MultiRegionProperties { WitnessRegion = witnessRegion.SystemName, Clusters = new List<string> { cluster1.Arn } } }; var cluster2 = await client2.CreateClusterAsync(createClusterRequest2); var cluster2Arn = cluster2.Arn; Console.WriteLine($"Initiated creation of {cluster2Arn}"); // Now that we know the cluster2 arn we can set it as a peer of cluster1 var updateClusterRequest = new UpdateClusterRequest { Identifier = cluster1.Identifier, MultiRegionProperties = new MultiRegionProperties { WitnessRegion = witnessRegion.SystemName, Clusters = new List<string> { cluster2.Arn } } }; await client1.UpdateClusterAsync(updateClusterRequest); Console.WriteLine($"Added {cluster2Arn} as a peer of {cluster1Arn}"); return (cluster1, cluster2); } } private static async Task Main() { var region1 = RegionEndpoint.USEast1; var region2 = RegionEndpoint.USEast2; var witnessRegion = RegionEndpoint.USWest2; var (cluster1, cluster2) = await Create(region1, region2, witnessRegion); Console.WriteLine("Created multi region clusters:"); Console.WriteLine($"Cluster 1: {cluster1.Arn}"); Console.WriteLine($"Cluster 2: {cluster2.Arn}"); } } }
Get cluster
The following examples show how to get information about a multi-Region cluster using different programming languages.
- Python
-
To get information about a multi-Region cluster, use the following example.
import boto3 from datetime import datetime import json def get_cluster(region, identifier): try: client = boto3.client("dsql", region_name=region) return client.get_cluster(identifier=identifier) except: print(f"Unable to get cluster {identifier} in region {region}") raise def main(): region = "us-east-1" cluster_id = "<your cluster id>" response = get_cluster(region, cluster_id) print(json.dumps(response, indent=2, default=lambda obj: obj.isoformat() if isinstance(obj, datetime) else None)) if __name__ == "__main__": main()
- C++
-
Use the following example to get information about a multi-Region cluster.
#include <aws/core/Aws.h> #include <aws/core/utils/Outcome.h> #include <aws/dsql/DSQLClient.h> #include <aws/dsql/model/GetClusterRequest.h> #include <iostream> using namespace Aws; using namespace Aws::DSQL; using namespace Aws::DSQL::Model; /** * Retrieves information about a cluster in Amazon Aurora DSQL */ GetClusterResult GetCluster(const Aws::String& region, const Aws::String& identifier) { // Create client for the specified region DSQL::DSQLClientConfiguration clientConfig; clientConfig.region = region; DSQL::DSQLClient client(clientConfig); // Get the cluster GetClusterRequest getClusterRequest; getClusterRequest.SetIdentifier(identifier); auto getOutcome = client.GetCluster(getClusterRequest); if (!getOutcome.IsSuccess()) { std::cerr << "Failed to retrieve cluster " << identifier << " in " << region << ": " << getOutcome.GetError().GetMessage() << std::endl; throw std::runtime_error("Unable to retrieve cluster " + identifier + " in region " + region); } return getOutcome.GetResult(); } int main() { Aws::SDKOptions options; Aws::InitAPI(options); { try { // Define region and cluster ID Aws::String region = "us-east-1"; Aws::String clusterId = "<your cluster id>"; auto cluster = GetCluster(region, clusterId); // Print cluster details std::cout << "Cluster Details:" << std::endl; std::cout << "ARN: " << cluster.GetArn() << std::endl; std::cout << "Status: " << ClusterStatusMapper::GetNameForClusterStatus(cluster.GetStatus()) << std::endl; } catch (const std::exception& e) { std::cerr << "Error: " << e.what() << std::endl; } } Aws::ShutdownAPI(options); return 0; }
- JavaScript
-
To get information about a multi-Region cluster, use the following example.
import { DSQLClient, GetClusterCommand } from "@aws-sdk/client-dsql"; async function getCluster(region, clusterId) { const client = new DSQLClient({ region }); const getClusterCommand = new GetClusterCommand({ identifier: clusterId, }); try { return await client.send(getClusterCommand); } catch (error) { if (error.name === "ResourceNotFoundException") { console.log("Cluster ID not found or deleted"); } throw error; } } async function main() { const region = "us-east-1"; const clusterId = "<CLUSTER_ID>"; const response = await getCluster(region, clusterId); console.log("Cluster: ", response); } main();
- Java
-
The following example lets you get information about a multi-Region cluster.
package org.example; import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.dsql.DsqlClient; import software.amazon.awssdk.services.dsql.model.GetClusterResponse; import software.amazon.awssdk.services.dsql.model.ResourceNotFoundException; public class GetCluster { public static void main(String[] args) { Region region = Region.US_EAST_1; String clusterId = "<your cluster id>"; try ( DsqlClient client = DsqlClient.builder() .region(region) .credentialsProvider(DefaultCredentialsProvider.create()) .build() ) { GetClusterResponse cluster = client.getCluster(r -> r.identifier(clusterId)); System.out.println(cluster); } catch (ResourceNotFoundException e) { System.out.printf("Cluster %s not found in %s%n", clusterId, region); } } }
- Rust
-
The following example lets you get information about a multi-Region cluster.
use aws_config::load_defaults; use aws_sdk_dsql::operation::get_cluster::GetClusterOutput; use aws_sdk_dsql::{ Client, Config, config::{BehaviorVersion, Region}, }; /// Create a client. We will use this later for performing operations on the cluster. async fn dsql_client(region: &'static str) -> Client { // Load default SDK configuration let sdk_defaults = load_defaults(BehaviorVersion::latest()).await; // You can set your own credentials by following this guide // https://docs.aws.amazon.com/sdk-for-rust/latest/dg/credproviders.html let credentials = sdk_defaults.credentials_provider().unwrap(); let config = Config::builder() .behavior_version(BehaviorVersion::latest()) .credentials_provider(credentials) .region(Region::new(region)) .build(); Client::from_conf(config) } /// Get a ClusterResource from DSQL cluster identifier pub async fn get_cluster(region: &'static str, identifier: &'static str) -> GetClusterOutput { let client = dsql_client(region).await; client .get_cluster() .identifier(identifier) .send() .await .unwrap() } #[tokio::main(flavor = "current_thread")] pub async fn main() -> anyhow::Result<()> { let region = "us-east-1"; let cluster = get_cluster(region, "<your cluster id>").await; println!("{:#?}", cluster); Ok(()) }
- Ruby
-
The following example lets you get information about a multi-Region cluster.
require "aws-sdk-dsql" require "pp" def get_cluster(region, identifier) client = Aws::DSQL::Client.new(region: region) client.get_cluster(identifier: identifier) rescue Aws::Errors::ServiceError => e abort "Unable to retrieve cluster #{identifier} in region #{region}: #{e.message}" end def main region = "us-east-1" cluster_id = "<your cluster id>" cluster = get_cluster(region, cluster_id) pp cluster end main if $PROGRAM_NAME == __FILE__
- .NET
-
The following example lets you get information about a multi-Region cluster.
using System; using System.Threading.Tasks; using Amazon; using Amazon.DSQL; using Amazon.DSQL.Model; using Amazon.Runtime.Credentials; namespace DSQLExamples.examples { public class GetCluster { /// <summary> /// Create a client. We will use this later for performing operations on the cluster. /// </summary> private static async Task<AmazonDSQLClient> CreateDSQLClient(RegionEndpoint region) { var awsCredentials = await DefaultAWSCredentialsIdentityResolver.GetCredentialsAsync(); var clientConfig = new AmazonDSQLConfig { RegionEndpoint = region }; return new AmazonDSQLClient(awsCredentials, clientConfig); } /// <summary> /// Get information about a DSQL cluster. /// </summary> public static async Task<GetClusterResponse> Get(RegionEndpoint region, string identifier) { using (var client = await CreateDSQLClient(region)) { var getClusterRequest = new GetClusterRequest { Identifier = identifier }; return await client.GetClusterAsync(getClusterRequest); } } private static async Task Main() { var region = RegionEndpoint.USEast1; var clusterId = "<your cluster id>"; var response = await Get(region, clusterId); Console.WriteLine($"Cluster ARN: {response.Arn}"); } } }
- Golang
-
The following example lets you get information about a multi-Region cluster.
package main import ( "context" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/config" "log" "time" "github.com/aws/aws-sdk-go-v2/service/dsql" ) func GetCluster(ctx context.Context, region, identifier string) (clusterStatus *dsql.GetClusterOutput, err error) { cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(region)) if err != nil { log.Fatalf("Failed to load AWS configuration: %v", err) } // Initialize the DSQL client client := dsql.NewFromConfig(cfg) input := &dsql.GetClusterInput{ Identifier: aws.String(identifier), } clusterStatus, err = client.GetCluster(context.Background(), input) if err != nil { log.Fatalf("Failed to get cluster: %v", err) } log.Printf("Cluster ARN: %s", *clusterStatus.Arn) return clusterStatus, nil } func main() { ctx, cancel := context.WithTimeout(context.Background(), 6*time.Minute) defer cancel() // Example cluster identifier identifier := "<CLUSTER_ID>" region := "us-east-1" _, err := GetCluster(ctx, region, identifier) if err != nil { log.Fatalf("Failed to get cluster: %v", err) } }
Update cluster
The following examples show how to update a multi-Region cluster using different programming languages.
- Python
-
To update a multi-Region cluster, use the following example.
import boto3 def update_cluster(region, cluster_id, deletion_protection_enabled): try: client = boto3.client("dsql", region_name=region) return client.update_cluster(identifier=cluster_id, deletionProtectionEnabled=deletion_protection_enabled) except: print("Unable to update cluster") raise def main(): region = "us-east-1" cluster_id = "<your cluster id>" deletion_protection_enabled = False response = update_cluster(region, cluster_id, deletion_protection_enabled) print(f"Updated {response["arn"]} with deletion_protection_enabled: {deletion_protection_enabled}") if __name__ == "__main__": main()
- C++
-
Use the following example to update a multi-Region cluster.
#include <aws/core/Aws.h> #include <aws/core/utils/Outcome.h> #include <aws/dsql/DSQLClient.h> #include <aws/dsql/model/UpdateClusterRequest.h> #include <iostream> using namespace Aws; using namespace Aws::DSQL; using namespace Aws::DSQL::Model; /** * Updates a cluster in Amazon Aurora DSQL */ UpdateClusterResult UpdateCluster(const Aws::String& region, const Aws::Map<Aws::String, Aws::String>& updateParams) { // Create client for the specified region DSQL::DSQLClientConfiguration clientConfig; clientConfig.region = region; DSQL::DSQLClient client(clientConfig); // Create update request UpdateClusterRequest updateRequest; updateRequest.SetClientToken(Aws::Utils::UUID::RandomUUID()); // Set identifier (required) if (updateParams.find("identifier") != updateParams.end()) { updateRequest.SetIdentifier(updateParams.at("identifier")); } else { throw std::runtime_error("Cluster identifier is required for update operation"); } // Set deletion protection if specified if (updateParams.find("deletion_protection_enabled") != updateParams.end()) { bool deletionProtection = (updateParams.at("deletion_protection_enabled") == "true"); updateRequest.SetDeletionProtectionEnabled(deletionProtection); } // Execute the update auto updateOutcome = client.UpdateCluster(updateRequest); if (!updateOutcome.IsSuccess()) { std::cerr << "Failed to update cluster: " << updateOutcome.GetError().GetMessage() << std::endl; throw std::runtime_error("Unable to update cluster"); } return updateOutcome.GetResult(); } int main() { Aws::SDKOptions options; Aws::InitAPI(options); { try { // Define region and update parameters Aws::String region = "us-east-1"; Aws::String clusterId = "<your cluster id>"; // Create parameter map Aws::Map<Aws::String, Aws::String> updateParams; updateParams["identifier"] = clusterId; updateParams["deletion_protection_enabled"] = "false"; auto updatedCluster = UpdateCluster(region, updateParams); std::cout << "Updated " << updatedCluster.GetArn() << std::endl; } catch (const std::exception& e) { std::cerr << "Error: " << e.what() << std::endl; } } Aws::ShutdownAPI(options); return 0; }
- JavaScript
-
To update a multi-Region cluster, use the following example.
import { DSQLClient, UpdateClusterCommand } from "@aws-sdk/client-dsql"; export async function updateCluster(region, clusterId, deletionProtectionEnabled) { const client = new DSQLClient({ region }); const updateClusterCommand = new UpdateClusterCommand({ identifier: clusterId, deletionProtectionEnabled: deletionProtectionEnabled }); try { return await client.send(updateClusterCommand); } catch (error) { console.error("Unable to update cluster", error.message); throw error; } } async function main() { const region = "us-east-1"; const clusterId = "<CLUSTER_ID>"; const deletionProtectionEnabled = false; const response = await updateCluster(region, clusterId, deletionProtectionEnabled); console.log(`Updated ${response.arn}`); } main();
- Java
-
Use the following example to update a multi-Region cluster.
package org.example; import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.dsql.DsqlClient; import software.amazon.awssdk.services.dsql.model.UpdateClusterRequest; import software.amazon.awssdk.services.dsql.model.UpdateClusterResponse; public class UpdateCluster { public static void main(String[] args) { Region region = Region.US_EAST_1; String clusterId = "<your cluster id>"; try ( DsqlClient client = DsqlClient.builder() .region(region) .credentialsProvider(DefaultCredentialsProvider.create()) .build() ) { UpdateClusterRequest request = UpdateClusterRequest.builder() .identifier(clusterId) .deletionProtectionEnabled(false) .build(); UpdateClusterResponse cluster = client.updateCluster(request); System.out.println("Updated " + cluster.arn()); } } }
- Rust
-
Use the following example to update a multi-Region cluster.
use aws_config::load_defaults; use aws_sdk_dsql::operation::update_cluster::UpdateClusterOutput; use aws_sdk_dsql::{ Client, Config, config::{BehaviorVersion, Region}, }; /// Create a client. We will use this later for performing operations on the cluster. async fn dsql_client(region: &'static str) -> Client { // Load default SDK configuration let sdk_defaults = load_defaults(BehaviorVersion::latest()).await; // You can set your own credentials by following this guide // https://docs.aws.amazon.com/sdk-for-rust/latest/dg/credproviders.html let credentials = sdk_defaults.credentials_provider().unwrap(); let config = Config::builder() .behavior_version(BehaviorVersion::latest()) .credentials_provider(credentials) .region(Region::new(region)) .build(); Client::from_conf(config) } /// Update a DSQL cluster and set delete protection to false. Also add new tags. pub async fn update_cluster(region: &'static str, identifier: &'static str) -> UpdateClusterOutput { let client = dsql_client(region).await; // Update delete protection let update_response = client .update_cluster() .identifier(identifier) .deletion_protection_enabled(false) .send() .await .unwrap(); update_response } #[tokio::main(flavor = "current_thread")] pub async fn main() -> anyhow::Result<()> { let region = "us-east-1"; let cluster = update_cluster(region, "<your cluster id>").await; println!("{:#?}", cluster); Ok(()) }
- Ruby
-
Use the following example to update a multi-Region cluster.
require "aws-sdk-dsql" def update_cluster(region, update_params) client = Aws::DSQL::Client.new(region: region) client.update_cluster(update_params) rescue Aws::Errors::ServiceError => e abort "Unable to update cluster: #{e.message}" end def main region = "us-east-1" cluster_id = "<your cluster id>" updated_cluster = update_cluster(region, { identifier: cluster_id, deletion_protection_enabled: false }) puts "Updated #{updated_cluster.arn}" end main if $PROGRAM_NAME == __FILE__
- .NET
-
Use the following example to update a multi-Region cluster.
using System; using System.Threading.Tasks; using Amazon; using Amazon.DSQL; using Amazon.DSQL.Model; using Amazon.Runtime.Credentials; namespace DSQLExamples.examples { public class UpdateCluster { /// <summary> /// Create a client. We will use this later for performing operations on the cluster. /// </summary> private static async Task<AmazonDSQLClient> CreateDSQLClient(RegionEndpoint region) { var awsCredentials = await DefaultAWSCredentialsIdentityResolver.GetCredentialsAsync(); var clientConfig = new AmazonDSQLConfig { RegionEndpoint = region }; return new AmazonDSQLClient(awsCredentials, clientConfig); } /// <summary> /// Update a DSQL cluster and set delete protection to false. /// </summary> public static async Task<UpdateClusterResponse> Update(RegionEndpoint region, string identifier) { using (var client = await CreateDSQLClient(region)) { var updateClusterRequest = new UpdateClusterRequest { Identifier = identifier, DeletionProtectionEnabled = false }; UpdateClusterResponse response = await client.UpdateClusterAsync(updateClusterRequest); Console.WriteLine($"Updated {response.Arn}"); return response; } } private static async Task Main() { var region = RegionEndpoint.USEast1; var clusterId = "<your cluster id>"; await Update(region, clusterId); } } }
- Golang
-
Use the following example to update a multi-Region cluster.
package main import ( "context" "github.com/aws/aws-sdk-go-v2/config" "log" "time" "github.com/aws/aws-sdk-go-v2/service/dsql" ) func UpdateCluster(ctx context.Context, region, id string, deleteProtection bool) (clusterStatus *dsql.UpdateClusterOutput, err error) { cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(region)) if err != nil { log.Fatalf("Failed to load AWS configuration: %v", err) } // Initialize the DSQL client client := dsql.NewFromConfig(cfg) input := dsql.UpdateClusterInput{ Identifier: &id, DeletionProtectionEnabled: &deleteProtection, } clusterStatus, err = client.UpdateCluster(context.Background(), &input) if err != nil { log.Fatalf("Failed to update cluster: %v", err) } log.Printf("Cluster updated successfully: %v", clusterStatus.Status) return clusterStatus, nil } func main() { ctx, cancel := context.WithTimeout(context.Background(), 6*time.Minute) defer cancel() // Example cluster identifier identifier := "<CLUSTER_ID>" region := "us-east-1" deleteProtection := false _, err := UpdateCluster(ctx, region, identifier, deleteProtection) if err != nil { log.Fatalf("Failed to update cluster: %v", err) } }
Delete cluster
The following examples show how to delete a multi-Region cluster using different programming languages.
- Python
-
To delete a multi-Region cluster, use the following example. Deleting a multi-Region cluster might take some time.
import boto3 def delete_multi_region_clusters(region_1, cluster_id_1, region_2, cluster_id_2): try: client_1 = boto3.client("dsql", region_name=region_1) client_2 = boto3.client("dsql", region_name=region_2) client_1.delete_cluster(identifier=cluster_id_1) print(f"Deleting cluster {cluster_id_1} in {region_1}") # cluster_1 will stay in PENDING_DELETE state until cluster_2 is deleted client_2.delete_cluster(identifier=cluster_id_2) print(f"Deleting cluster {cluster_id_2} in {region_2}") # Now that both clusters have been marked for deletion they will transition # to DELETING state and finalize deletion print(f"Waiting for {cluster_id_1} to finish deletion") client_1.get_waiter("cluster_not_exists").wait( identifier=cluster_id_1, WaiterConfig={ 'Delay': 10, 'MaxAttempts': 30 } ) print(f"Waiting for {cluster_id_2} to finish deletion") client_2.get_waiter("cluster_not_exists").wait( identifier=cluster_id_2, WaiterConfig={ 'Delay': 10, 'MaxAttempts': 30 } ) except: print("Unable to delete cluster") raise def main(): region_1 = "us-east-1" cluster_id_1 = "<cluster 1 id>" region_2 = "us-east-2" cluster_id_2 = "<cluster 2 id>" delete_multi_region_clusters(region_1, cluster_id_1, region_2, cluster_id_2) print(f"Deleted {cluster_id_1} in {region_1} and {cluster_id_2} in {region_2}") if __name__ == "__main__": main()
- C++
-
To delete a multi-Region cluster, use the following example. Deleting a multi-Region cluster might take some time.
#include <aws/core/Aws.h> #include <aws/core/utils/Outcome.h> #include <aws/dsql/DSQLClient.h> #include <aws/dsql/model/DeleteClusterRequest.h> #include <aws/dsql/model/GetClusterRequest.h> #include <iostream> #include <thread> #include <chrono> using namespace Aws; using namespace Aws::DSQL; using namespace Aws::DSQL::Model; /** * Deletes multi-region clusters in Amazon Aurora DSQL */ void DeleteMultiRegionClusters( const Aws::String& region1, const Aws::String& clusterId1, const Aws::String& region2, const Aws::String& clusterId2) { // Create clients for each region DSQL::DSQLClientConfiguration clientConfig1; clientConfig1.region = region1; DSQL::DSQLClient client1(clientConfig1); DSQL::DSQLClientConfiguration clientConfig2; clientConfig2.region = region2; DSQL::DSQLClient client2(clientConfig2); // Delete the first cluster std::cout << "Deleting cluster " << clusterId1 << " in " << region1 << std::endl; DeleteClusterRequest deleteRequest1; deleteRequest1.SetIdentifier(clusterId1); deleteRequest1.SetClientToken(Aws::Utils::UUID::RandomUUID()); auto deleteOutcome1 = client1.DeleteCluster(deleteRequest1); if (!deleteOutcome1.IsSuccess()) { std::cerr << "Failed to delete cluster " << clusterId1 << " in " << region1 << ": " << deleteOutcome1.GetError().GetMessage() << std::endl; throw std::runtime_error("Failed to delete multi-region clusters"); } // cluster1 will stay in PENDING_DELETE state until cluster2 is deleted std::cout << "Deleting cluster " << clusterId2 << " in " << region2 << std::endl; DeleteClusterRequest deleteRequest2; deleteRequest2.SetIdentifier(clusterId2); deleteRequest2.SetClientToken(Aws::Utils::UUID::RandomUUID()); auto deleteOutcome2 = client2.DeleteCluster(deleteRequest2); if (!deleteOutcome2.IsSuccess()) { std::cerr << "Failed to delete cluster " << clusterId2 << " in " << region2 << ": " << deleteOutcome2.GetError().GetMessage() << std::endl; throw std::runtime_error("Failed to delete multi-region clusters"); } } int main() { Aws::SDKOptions options; Aws::InitAPI(options); { try { Aws::String region1 = "us-east-1"; Aws::String clusterId1 = "<your cluster id 1>"; Aws::String region2 = "us-east-2"; Aws::String clusterId2 = "<your cluster id 2>"; DeleteMultiRegionClusters(region1, clusterId1, region2, clusterId2); std::cout << "Deleted " << clusterId1 << " in " << region1 << " and " << clusterId2 << " in " << region2 << std::endl; } catch (const std::exception& e) { std::cerr << "Error: " << e.what() << std::endl; } } Aws::ShutdownAPI(options); return 0; }
- JavaScript
-
To delete a multi-Region cluster, use the following example. Deleting a multi-Region cluster might take some time.
import { DSQLClient, DeleteClusterCommand, waitUntilClusterNotExists } from "@aws-sdk/client-dsql"; async function deleteMultiRegionClusters(region1, cluster1_id, region2, cluster2_id) { const client1 = new DSQLClient({ region: region1 }); const client2 = new DSQLClient({ region: region2 }); try { const deleteClusterCommand1 = new DeleteClusterCommand({ identifier: cluster1_id, }); const response1 = await client1.send(deleteClusterCommand1); const deleteClusterCommand2 = new DeleteClusterCommand({ identifier: cluster2_id, }); const response2 = await client2.send(deleteClusterCommand2); console.log(`Waiting for cluster1 ${response1.identifier} to finish deletion`); await waitUntilClusterNotExists( { client: client1, maxWaitTime: 300 // Wait for 5 minutes }, { identifier: response1.identifier } ); console.log(`Cluster1 Id ${response1.identifier} is now deleted`); console.log(`Waiting for cluster2 ${response2.identifier} to finish deletion`); await waitUntilClusterNotExists( { client: client2, maxWaitTime: 300 // Wait for 5 minutes }, { identifier: response2.identifier } ); console.log(`Cluster2 Id ${response2.identifier} is now deleted`); return; } catch (error) { if (error.name === "ResourceNotFoundException") { console.log("Some or all Cluster ARNs not found or already deleted"); } else { console.error("Unable to delete multi-region clusters: ", error.message); } throw error; } } async function main() { const region1 = "us-east-1"; const cluster1_id = "<CLUSTER_ID_1>"; const region2 = "us-east-2"; const cluster2_id = "<CLUSTER_ID_2>"; const response = await deleteMultiRegionClusters(region1, cluster1_id, region2, cluster2_id); console.log(`Deleted ${cluster1_id} in ${region1} and ${cluster2_id} in ${region2}`); } main();
- Java
-
To delete a multi-Region cluster, use the following example. Deleting a multi-Region cluster might take some time.
package org.example; import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.retries.api.BackoffStrategy; import software.amazon.awssdk.services.dsql.DsqlClient; import software.amazon.awssdk.services.dsql.DsqlClientBuilder; import software.amazon.awssdk.services.dsql.model.DeleteClusterRequest; import java.time.Duration; public class DeleteMultiRegionClusters { public static void main(String[] args) { Region region1 = Region.US_EAST_1; String clusterId1 = "<your cluster id 1>"; Region region2 = Region.US_EAST_2; String clusterId2 = "<your cluster id 2>"; DsqlClientBuilder clientBuilder = DsqlClient.builder() .credentialsProvider(DefaultCredentialsProvider.create()); try ( DsqlClient client1 = clientBuilder.region(region1).build(); DsqlClient client2 = clientBuilder.region(region2).build() ) { System.out.printf("Deleting cluster %s in %s%n", clusterId1, region1); DeleteClusterRequest request1 = DeleteClusterRequest.builder() .identifier(clusterId1) .build(); client1.deleteCluster(request1); // cluster1 will stay in PENDING_DELETE until cluster2 is deleted System.out.printf("Deleting cluster %s in %s%n", clusterId2, region2); DeleteClusterRequest request2 = DeleteClusterRequest.builder() .identifier(clusterId2) .build(); client2.deleteCluster(request2); // Now that both clusters have been marked for deletion they will transition // to DELETING state and finalize deletion. System.out.printf("Waiting for cluster %s to finish deletion%n", clusterId1); client1.waiter().waitUntilClusterNotExists( getCluster -> getCluster.identifier(clusterId1), config -> config.backoffStrategyV2( BackoffStrategy.fixedDelayWithoutJitter(Duration.ofSeconds(10)) ).waitTimeout(Duration.ofMinutes(5)) ); System.out.printf("Waiting for cluster %s to finish deletion%n", clusterId2); client2.waiter().waitUntilClusterNotExists( getCluster -> getCluster.identifier(clusterId2), config -> config.backoffStrategyV2( BackoffStrategy.fixedDelayWithoutJitter(Duration.ofSeconds(10)) ).waitTimeout(Duration.ofMinutes(5)) ); System.out.printf("Deleted %s in %s and %s in %s%n", clusterId1, region1, clusterId2, region2); } } }
- Rust
-
To delete a multi-Region cluster, use the following example. Deleting a multi-Region cluster might take some time.
use aws_config::{BehaviorVersion, Region, load_defaults}; use aws_sdk_dsql::client::Waiters; use aws_sdk_dsql::{Client, Config}; /// Create a client. We will use this later for performing operations on the cluster. async fn dsql_client(region: &'static str) -> Client { // Load default SDK configuration let sdk_defaults = load_defaults(BehaviorVersion::latest()).await; // You can set your own credentials by following this guide // https://docs.aws.amazon.com/sdk-for-rust/latest/dg/credproviders.html let credentials = sdk_defaults.credentials_provider().unwrap(); let config = Config::builder() .behavior_version(BehaviorVersion::latest()) .credentials_provider(credentials) .region(Region::new(region)) .build(); Client::from_conf(config) } /// Create a cluster without delete protection and a name pub async fn delete_multi_region_clusters( region_1: &'static str, cluster_id_1: &'static str, region_2: &'static str, cluster_id_2: &'static str, ) { let client_1 = dsql_client(region_1).await; let client_2 = dsql_client(region_2).await; println!("Deleting cluster {cluster_id_1} in {region_1}"); client_1 .delete_cluster() .identifier(cluster_id_1) .send() .await .unwrap(); // cluster_1 will stay in PENDING_DELETE state until cluster_2 is deleted println!("Deleting cluster {cluster_id_2} in {region_2}"); client_2 .delete_cluster() .identifier(cluster_id_2) .send() .await .unwrap(); // Now that both clusters have been marked for deletion they will transition // to DELETING state and finalize deletion println!("Waiting for {cluster_id_1} to finish deletion"); client_1 .wait_until_cluster_not_exists() .identifier(cluster_id_1) .wait(std::time::Duration::from_secs(300)) // Wait up to 5 minutes .await .unwrap(); println!("Waiting for {cluster_id_2} to finish deletion"); client_2 .wait_until_cluster_not_exists() .identifier(cluster_id_2) .wait(std::time::Duration::from_secs(300)) // Wait up to 5 minutes .await .unwrap(); } #[tokio::main(flavor = "current_thread")] pub async fn main() -> anyhow::Result<()> { let region_1 = "us-east-1"; let cluster_id_1 = "<cluster 1 to be deleted>"; let region_2 = "us-east-2"; let cluster_id_2 = "<cluster 2 to be deleted>"; delete_multi_region_clusters(region_1, cluster_id_1, region_2, cluster_id_2).await; println!("Deleted {cluster_id_1} in {region_1} and {cluster_id_2} in {region_2}"); Ok(()) }
- Ruby
-
To delete a multi-Region cluster, use the following example. Deleting a multi-Region cluster might take some time.
require "aws-sdk-dsql" def delete_multi_region_clusters(region_1, cluster_id_1, region_2, cluster_id_2) client_1 = Aws::DSQL::Client.new(region: region_1) client_2 = Aws::DSQL::Client.new(region: region_2) puts "Deleting cluster #{cluster_id_1} in #{region_1}" client_1.delete_cluster(identifier: cluster_id_1) # cluster_1 will stay in PENDING_DELETE state until cluster_2 is deleted puts "Deleting #{cluster_id_2} in #{region_2}" client_2.delete_cluster(identifier: cluster_id_2) # Now that both clusters have been marked for deletion they will transition # to DELETING state and finalize deletion puts "Waiting for #{cluster_id_1} to finish deletion" client_1.wait_until(:cluster_not_exists, identifier: cluster_id_1) do |w| # Wait for 5 minutes w.max_attempts = 30 w.delay = 10 end puts "Waiting for #{cluster_id_2} to finish deletion" client_2.wait_until(:cluster_not_exists, identifier: cluster_id_2) do |w| # Wait for 5 minutes w.max_attempts = 30 w.delay = 10 end rescue Aws::Errors::ServiceError => e abort "Failed to delete multi-region clusters: #{e.message}" end def main region_1 = "us-east-1" cluster_id_1 = "<your cluster id 1>" region_2 = "us-east-2" cluster_id_2 = "<your cluster id 2>" delete_multi_region_clusters(region_1, cluster_id_1, region_2, cluster_id_2) puts "Deleted #{cluster_id_1} in #{region_1} and #{cluster_id_2} in #{region_2}" end main if $PROGRAM_NAME == __FILE__
- .NET
-
To delete a multi-Region cluster, use the following example. Deleting a multi-Region cluster might take some time.
using System; using System.Threading.Tasks; using Amazon; using Amazon.DSQL; using Amazon.DSQL.Model; using Amazon.Runtime.Credentials; using Amazon.Runtime.Endpoints; namespace DSQLExamples.examples { public class DeleteMultiRegionClusters { /// <summary> /// Create a client. We will use this later for performing operations on the cluster. /// </summary> private static async Task<AmazonDSQLClient> CreateDSQLClient(RegionEndpoint region) { var awsCredentials = await DefaultAWSCredentialsIdentityResolver.GetCredentialsAsync(); var clientConfig = new AmazonDSQLConfig { RegionEndpoint = region, }; return new AmazonDSQLClient(awsCredentials, clientConfig); } /// <summary> /// Delete multi-region clusters. /// </summary> public static async Task Delete( RegionEndpoint region1, string clusterId1, RegionEndpoint region2, string clusterId2) { using (var client1 = await CreateDSQLClient(region1)) using (var client2 = await CreateDSQLClient(region2)) { var deleteRequest1 = new DeleteClusterRequest { Identifier = clusterId1 }; var deleteResponse1 = await client1.DeleteClusterAsync(deleteRequest1); Console.WriteLine($"Initiated deletion of {deleteResponse1.Arn}"); // cluster 1 will stay in PENDING_DELETE state until cluster 2 is deleted var deleteRequest2 = new DeleteClusterRequest { Identifier = clusterId2 }; var deleteResponse2 = await client2.DeleteClusterAsync(deleteRequest2); Console.WriteLine($"Initiated deletion of {deleteResponse2.Arn}"); } } private static async Task Main() { var region1 = RegionEndpoint.USEast1; var cluster1 = "<cluster 1 to be deleted>"; var region2 = RegionEndpoint.USEast2; var cluster2 = "<cluster 2 to be deleted>"; await Delete(region1, cluster1, region2, cluster2); } } }
- Golang
-
To delete a multi-Region cluster, use the following example. Deleting a multi-Region cluster might take some time.
package main import ( "context" "fmt" "log" "time" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/config" "github.com/aws/aws-sdk-go-v2/service/dsql" ) func DeleteMultiRegionClusters(ctx context.Context, region1, clusterId1, region2, clusterId2 string) error { // Load the AWS configuration for region 1 cfg1, err := config.LoadDefaultConfig(ctx, config.WithRegion(region1)) if err != nil { return fmt.Errorf("unable to load SDK config for region %s: %w", region1, err) } // Load the AWS configuration for region 2 cfg2, err := config.LoadDefaultConfig(ctx, config.WithRegion(region2)) if err != nil { return fmt.Errorf("unable to load SDK config for region %s: %w", region2, err) } // Create DSQL clients for both regions client1 := dsql.NewFromConfig(cfg1) client2 := dsql.NewFromConfig(cfg2) // Delete cluster in region 1 fmt.Printf("Deleting cluster %s in %s\n", clusterId1, region1) _, err = client1.DeleteCluster(ctx, &dsql.DeleteClusterInput{ Identifier: aws.String(clusterId1), }) if err != nil { return fmt.Errorf("failed to delete cluster in region %s: %w", region1, err) } // Delete cluster in region 2 fmt.Printf("Deleting cluster %s in %s\n", clusterId2, region2) _, err = client2.DeleteCluster(ctx, &dsql.DeleteClusterInput{ Identifier: aws.String(clusterId2), }) if err != nil { return fmt.Errorf("failed to delete cluster in region %s: %w", region2, err) } // Create waiters for both regions waiter1 := dsql.NewClusterNotExistsWaiter(client1, func(options *dsql.ClusterNotExistsWaiterOptions) { options.MinDelay = 10 * time.Second options.MaxDelay = 30 * time.Second options.LogWaitAttempts = true }) waiter2 := dsql.NewClusterNotExistsWaiter(client2, func(options *dsql.ClusterNotExistsWaiterOptions) { options.MinDelay = 10 * time.Second options.MaxDelay = 30 * time.Second options.LogWaitAttempts = true }) // Wait for cluster in region 1 to be deleted fmt.Printf("Waiting for cluster %s to finish deletion\n", clusterId1) err = waiter1.Wait(ctx, &dsql.GetClusterInput{ Identifier: aws.String(clusterId1), }, 5*time.Minute) if err != nil { return fmt.Errorf("error waiting for cluster deletion in region %s: %w", region1, err) } // Wait for cluster in region 2 to be deleted fmt.Printf("Waiting for cluster %s to finish deletion\n", clusterId2) err = waiter2.Wait(ctx, &dsql.GetClusterInput{ Identifier: aws.String(clusterId2), }, 5*time.Minute) if err != nil { return fmt.Errorf("error waiting for cluster deletion in region %s: %w", region2, err) } fmt.Printf("Successfully deleted clusters %s in %s and %s in %s\n", clusterId1, region1, clusterId2, region2) return nil } // Example usage in main function func main() { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute) defer cancel() err := DeleteMultiRegionClusters( ctx, "us-east-1", // region1 "<CLUSTER_ID_1>", // clusterId1 "us-east-2", // region2 "<CLUSTER_ID_2>", // clusterId2 ) if err != nil { log.Fatalf("Failed to delete multi-region clusters: %v", err) } }
For more code samples and examples, visit the Aurora DSQL Samples GitHub repository