|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/aws/aws-sdk-go-v2/aws" |
| 10 | + "github.com/aws/aws-sdk-go-v2/config" |
| 11 | + "github.com/aws/aws-sdk-go-v2/service/dsql" |
| 12 | + dtypes "github.com/aws/aws-sdk-go-v2/service/dsql/types" |
| 13 | +) |
| 14 | + |
| 15 | +func CreateMultiRegionClusters(ctx context.Context, witness, region1, region2 string) error { |
| 16 | + |
| 17 | + cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(region1)) |
| 18 | + if err != nil { |
| 19 | + log.Fatalf("Failed to load AWS configuration: %v", err) |
| 20 | + } |
| 21 | + |
| 22 | + // Create a DSQL region 1 client |
| 23 | + client := dsql.NewFromConfig(cfg, func(o *dsql.Options) { |
| 24 | + o.Region = region1 |
| 25 | + }) |
| 26 | + |
| 27 | + cfg2, err := config.LoadDefaultConfig(ctx, config.WithRegion(region2)) |
| 28 | + if err != nil { |
| 29 | + log.Fatalf("Failed to load AWS configuration: %v", err) |
| 30 | + } |
| 31 | + |
| 32 | + // Create a DSQL region 2 client |
| 33 | + client2 := dsql.NewFromConfig(cfg2, func(o *dsql.Options) { |
| 34 | + o.Region = region2 |
| 35 | + }) |
| 36 | + |
| 37 | + // Create cluster |
| 38 | + deleteProtect := true |
| 39 | + |
| 40 | + // We can only set the witness region for the first cluster |
| 41 | + input := &dsql.CreateClusterInput{ |
| 42 | + DeletionProtectionEnabled: &deleteProtect, |
| 43 | + MultiRegionProperties: &dtypes.MultiRegionProperties{ |
| 44 | + WitnessRegion: aws.String(witness), |
| 45 | + }, |
| 46 | + Tags: map[string]string{ |
| 47 | + "Name": "go multi-region cluster", |
| 48 | + }, |
| 49 | + } |
| 50 | + |
| 51 | + clusterProperties, err := client.CreateCluster(context.Background(), input) |
| 52 | + |
| 53 | + if err != nil { |
| 54 | + return fmt.Errorf("failed to create first cluster: %v", err) |
| 55 | + } |
| 56 | + |
| 57 | + // create second cluster |
| 58 | + cluster2Arns := []string{*clusterProperties.Arn} |
| 59 | + |
| 60 | + // For the second cluster we can set witness region and designate the first cluster as a peer |
| 61 | + input2 := &dsql.CreateClusterInput{ |
| 62 | + DeletionProtectionEnabled: &deleteProtect, |
| 63 | + MultiRegionProperties: &dtypes.MultiRegionProperties{ |
| 64 | + WitnessRegion: aws.String("us-west-2"), |
| 65 | + Clusters: cluster2Arns, |
| 66 | + }, |
| 67 | + Tags: map[string]string{ |
| 68 | + "Name": "go multi-region cluster", |
| 69 | + }, |
| 70 | + } |
| 71 | + |
| 72 | + clusterProperties2, err := client2.CreateCluster(context.Background(), input2) |
| 73 | + |
| 74 | + if err != nil { |
| 75 | + return fmt.Errorf("failed to create second cluster: %v", err) |
| 76 | + } |
| 77 | + |
| 78 | + // link initial cluster to second cluster |
| 79 | + cluster1Arns := []string{*clusterProperties2.Arn} |
| 80 | + |
| 81 | + // Now that we know the second cluster arn we can set it as a peer of the first cluster |
| 82 | + input3 := dsql.UpdateClusterInput{ |
| 83 | + Identifier: clusterProperties.Identifier, |
| 84 | + MultiRegionProperties: &dtypes.MultiRegionProperties{ |
| 85 | + WitnessRegion: aws.String("us-west-2"), |
| 86 | + Clusters: cluster1Arns, |
| 87 | + }} |
| 88 | + |
| 89 | + _, err = client.UpdateCluster(context.Background(), &input3) |
| 90 | + |
| 91 | + if err != nil { |
| 92 | + return fmt.Errorf("failed to update cluster to associate with first cluster. %v", err) |
| 93 | + } |
| 94 | + |
| 95 | + // Create the waiter with our custom options for first cluster |
| 96 | + waiter := dsql.NewClusterActiveWaiter(client, func(o *dsql.ClusterActiveWaiterOptions) { |
| 97 | + o.MaxDelay = 30 * time.Second // Creating a multi-region cluster can take a few minutes |
| 98 | + o.MinDelay = 10 * time.Second |
| 99 | + o.LogWaitAttempts = true |
| 100 | + }) |
| 101 | + |
| 102 | + // Now that multiRegionProperties is fully defined for both clusters |
| 103 | + // they'll begin the transition to ACTIVE |
| 104 | + |
| 105 | + // Create the input for the clusterProperties to monitor for first cluster |
| 106 | + getInput := &dsql.GetClusterInput{ |
| 107 | + Identifier: clusterProperties.Identifier, |
| 108 | + } |
| 109 | + |
| 110 | + // Wait for the first cluster to become active |
| 111 | + fmt.Printf("Waiting for first cluster %s to become active...\n", *clusterProperties.Identifier) |
| 112 | + err = waiter.Wait(ctx, getInput, 5*time.Minute) |
| 113 | + if err != nil { |
| 114 | + return fmt.Errorf("error waiting for first cluster to become active: %w", err) |
| 115 | + } |
| 116 | + |
| 117 | + // Create the waiter with our custom options |
| 118 | + waiter2 := dsql.NewClusterActiveWaiter(client2, func(o *dsql.ClusterActiveWaiterOptions) { |
| 119 | + o.MaxDelay = 30 * time.Second // Creating a multi-region cluster can take a few minutes |
| 120 | + o.MinDelay = 10 * time.Second |
| 121 | + o.LogWaitAttempts = true |
| 122 | + }) |
| 123 | + |
| 124 | + // Create the input for the clusterProperties to monitor for second |
| 125 | + getInput2 := &dsql.GetClusterInput{ |
| 126 | + Identifier: clusterProperties2.Identifier, |
| 127 | + } |
| 128 | + |
| 129 | + // Wait for the second cluster to become active |
| 130 | + fmt.Printf("Waiting for second cluster %s to become active...\n", *clusterProperties2.Identifier) |
| 131 | + err = waiter2.Wait(ctx, getInput2, 5*time.Minute) |
| 132 | + if err != nil { |
| 133 | + return fmt.Errorf("error waiting for second cluster to become active: %w", err) |
| 134 | + } |
| 135 | + |
| 136 | + fmt.Printf("Cluster %s is now active\n", *clusterProperties.Identifier) |
| 137 | + fmt.Printf("Cluster %s is now active\n", *clusterProperties2.Identifier) |
| 138 | + return nil |
| 139 | +} |
| 140 | + |
| 141 | +// Example usage in main function |
| 142 | +func main() { |
| 143 | + // Set up context with timeout |
| 144 | + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute) |
| 145 | + defer cancel() |
| 146 | + |
| 147 | + err := CreateMultiRegionClusters(ctx, "us-west-2", "us-east-1", "us-east-2") |
| 148 | + if err != nil { |
| 149 | + fmt.Printf("failed to create multi-region clusters: %v", err) |
| 150 | + panic(err) |
| 151 | + } |
| 152 | + |
| 153 | +} |
0 commit comments