Skip to content

Commit 3a77c01

Browse files
mitchell-elholmMitchell Elholm
andauthored
Ruby SR/MR cluster management (#142)
* Python SR/MR cluster management * Uncomment test * Minor changes - PR review comments * Update README.md * ENV variable consistency --------- Co-authored-by: Mitchell Elholm <elholmit@amazon.com>
1 parent 29fb073 commit 3a77c01

18 files changed

Lines changed: 336 additions & 240 deletions

.github/workflows/ruby-cm-integ-tests.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ jobs:
3434
- name: Checkout code
3535
uses: actions/checkout@v4
3636

37-
- name: Set up Ruby 2.5
37+
- name: Set up Ruby 3.3
3838
uses: ruby/setup-ruby@v1
3939
with:
40-
ruby-version: '2.5'
40+
ruby-version: '3.3'
4141
bundler-cache: true
4242

4343
- name: Configure AWS Credentials
@@ -48,6 +48,8 @@ jobs:
4848

4949
- name: Configure and run integration for cluster management
5050
working-directory: ./ruby/cluster_management
51+
env:
52+
IS_CI: "TRUE"
5153
run: |
5254
bundle install
5355
rspec

ruby/cluster_management/Gemfile

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
source "https://rubygems.org"
22

3-
gem "aws-sdk-dsql", "~> 1"
4-
5-
gem 'rspec', '3.13.0'
6-
gem 'aws-sdk-core', '3.211.0'
3+
gem "aws-sdk-dsql", "~> 1.8"
4+
gem 'rspec', '3.13.0'

ruby/cluster_management/README.md

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,74 @@
1-
# Aurora DSQL Ruby SDK code examples
1+
# Aurora DSQL Ruby code examples
22

33
## Overview
44

5-
The code examples in this topic show you how to use the AWS Ruby SDK with DSQL to create, read, update, and delete clusters.
5+
The code examples in this topic show you how to use the AWS Ruby SDK with DSQL
6+
to create, update, get, and delete single- and multi-Region clusters.
7+
8+
Each file in the [/lib](lib) directory demonstrates a minimal
9+
working example for each operation. The example function for each operation is invoked
10+
in [`dsql_cluster_management_spec.rb`](spec/dsql_cluster_management_spec.rb).
611

712
## Run the examples
813

14+
### ⚠️ Important
15+
16+
* Running this code might result in charges to your AWS account.
17+
* We recommend that you grant your code least privilege. At most, grant only the
18+
minimum permissions required to perform the task. For more information, see
19+
[Grant least privilege](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege).
20+
* This code is not tested in every AWS Region. For more information, see
21+
[AWS Regional Services](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services).
22+
923
### Prerequisites
1024

11-
* Ruby version >=3.3 is needed
25+
- Ruby version >= 3.3 is installed.
26+
- **MacOS Optional:** Use rbenv to manage Ruby version
1227

13-
### Setup test running environment
28+
```bash
29+
# Optional use rbenv
30+
rbenv install 3.3.5
31+
rbenv local 3.3.5
1432

15-
Ensure you are authenticated with AWS credentials. No other setup is needed besides having Ruby installed.
33+
ruby --version
34+
```
35+
36+
- Valid AWS credentials can be discovered by
37+
the [default provider chain](https://docs.aws.amazon.com/sdk-for-ruby/v3/developer-guide/credential-providers.html).
1638

17-
### Run the example tests
39+
### Execute tests to create and delete clusters
1840

19-
In a terminal run the following command from the crud_example folder:
2041
```sh
42+
# Optional: Single-Region examples will execute in CLUSTER_REGION. Defaults to 'us-east-1'.
43+
export CLUSTER_REGION="us-east-1"
44+
45+
# Optional: Multi-Region examples will create clusters in CLUSTER_1_REGION and CLUSTER_2_REGION
46+
# with WITNESS_REGION as witness for both. Defaults to 'us-east-1' for CLUSTER_1_REGION, 'us-east-2'
47+
# for CLUSTER_2_REGION and 'us-west-2' for WITNESS_REGION.
48+
export CLUSTER_1_REGION="us-east-1"
49+
export CLUSTER_2_REGION="us-east-2"
50+
export WITNESS_REGION="us-west-2"
51+
2152
bundle install
2253

54+
# Will create, update, read, then delete clusters
2355
rspec
2456
```
2557

58+
Test execution will take around five minutes as it waits for clusters to complete activation and deletion.
59+
60+
### Executing single operations
61+
62+
Files in [lib/](lib/) each have a `main()` function that let you exercise single operations.
63+
64+
```shell
65+
# Check each operation for its expected environment variables
66+
CLUSTER_REGION="us-east-1" CLUSTER_ID="<your cluster id>" \
67+
ruby lib/create_single_region_cluster.rb
68+
```
69+
2670
---
2771

28-
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
72+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2973

30-
SPDX-License-Identifier: MIT-0
74+
SPDX-License-Identifier: MIT-0

ruby/cluster_management/lib/create_multi_region.rb

Lines changed: 0 additions & 41 deletions
This file was deleted.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
require "aws-sdk-dsql"
2+
require "pp"
3+
4+
def create_multi_region_clusters(region_1, region_2, witness_region)
5+
client_1 = Aws::DSQL::Client.new(region: region_1)
6+
client_2 = Aws::DSQL::Client.new(region: region_2)
7+
8+
# We can only set the witness region for the first cluster
9+
puts "Creating cluster in #{region_1}"
10+
cluster_1 = client_1.create_cluster(
11+
deletion_protection_enabled: true,
12+
multi_region_properties: {
13+
witness_region: witness_region
14+
},
15+
tags: {
16+
Name: "Ruby-CM-Example-Multi-Region",
17+
Repo: "aws-samples/aurora-dsql-samples"
18+
}
19+
)
20+
puts "Created #{cluster_1.arn}"
21+
22+
# For the second cluster we can set witness region and designate cluster_1 as a peer
23+
puts "Creating cluster in #{region_2}"
24+
cluster_2 = client_2.create_cluster(
25+
deletion_protection_enabled: true,
26+
multi_region_properties: {
27+
witness_region: witness_region,
28+
clusters: [ cluster_1.arn ]
29+
},
30+
tags: {
31+
Name: "Ruby-CM-Example-Multi-Region",
32+
Repo: "aws-samples/aurora-dsql-samples"
33+
}
34+
)
35+
puts "Created #{cluster_2.arn}"
36+
37+
# Now that we know the cluster_2 arn we can set it as a peer of cluster_1
38+
client_1.update_cluster(
39+
identifier: cluster_1.identifier,
40+
multi_region_properties: {
41+
witness_region: witness_region,
42+
clusters: [ cluster_2.arn ]
43+
}
44+
)
45+
puts "Added #{cluster_2.arn} as a peer of #{cluster_1.arn}"
46+
47+
# Now that multi_region_properties is fully defined for both clusters
48+
# they'll begin the transition to ACTIVE
49+
puts "Waiting for #{cluster_1.arn} to become ACTIVE"
50+
cluster_1 = client_1.wait_until(:cluster_active, identifier: cluster_1.identifier) do |w|
51+
# Wait for 5 minutes
52+
w.max_attempts = 30
53+
w.delay = 10
54+
end
55+
56+
puts "Waiting for #{cluster_2.arn} to become ACTIVE"
57+
cluster_2 = client_2.wait_until(:cluster_active, identifier: cluster_2.identifier) do |w|
58+
w.max_attempts = 30
59+
w.delay = 10
60+
end
61+
62+
[ cluster_1, cluster_2 ]
63+
rescue Aws::Errors::ServiceError => e
64+
abort "Failed to create multi-region clusters: #{e.message}"
65+
end
66+
67+
def main
68+
region_1 = ENV.fetch("CLUSTER_1_REGION", "us-east-1")
69+
region_2 = ENV.fetch("CLUSTER_2_REGION", "us-east-2")
70+
witness_region = ENV.fetch("WITNESS_REGION", "us-west-2")
71+
72+
cluster_1, cluster_2 = create_multi_region_clusters(region_1, region_2, witness_region)
73+
74+
puts "Created multi region clusters:"
75+
pp cluster_1
76+
pp cluster_2
77+
end
78+
79+
main if $PROGRAM_NAME == __FILE__

ruby/cluster_management/lib/create_single_region.rb

Lines changed: 0 additions & 27 deletions
This file was deleted.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
require "aws-sdk-dsql"
2+
require "pp"
3+
4+
def create_cluster(region)
5+
client = Aws::DSQL::Client.new(region: region)
6+
cluster = client.create_cluster(
7+
deletion_protection_enabled: true,
8+
tags: {
9+
Name: "Ruby-CM-Example-Single-Region",
10+
Repo: "aws-samples/aurora-dsql-samples"
11+
}
12+
)
13+
puts "Created #{cluster.arn}"
14+
15+
# The DSQL SDK offers built-in waiters to poll for a cluster's
16+
# transition to ACTIVE.
17+
puts "Waiting for cluster to become ACTIVE"
18+
client.wait_until(:cluster_active, identifier: cluster.identifier) do |w|
19+
# Wait for 5 minutes
20+
w.max_attempts = 30
21+
w.delay = 10
22+
end
23+
rescue Aws::Errors::ServiceError => e
24+
abort "Unable to create cluster in #{region}: #{e.message}"
25+
end
26+
27+
def main
28+
region = ENV.fetch("CLUSTER_1_REGION", "us-east-1")
29+
cluster = create_cluster(region)
30+
pp cluster
31+
end
32+
33+
main if $PROGRAM_NAME == __FILE__

ruby/cluster_management/lib/delete_multi_region.rb

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
require "aws-sdk-dsql"
2+
3+
def delete_multi_region_clusters(region_1, cluster_id_1, region_2, cluster_id_2)
4+
client_1 = Aws::DSQL::Client.new(region: region_1)
5+
client_2 = Aws::DSQL::Client.new(region: region_2)
6+
7+
puts "Deleting cluster #{cluster_id_1} in #{region_1}"
8+
client_1.delete_cluster(identifier: cluster_id_1)
9+
10+
# cluster_1 will stay in PENDING_DELETE state until cluster_2 is deleted
11+
puts "Deleting #{cluster_id_2} in #{region_2}"
12+
client_2.delete_cluster(identifier: cluster_id_2)
13+
14+
# Now that both clusters have been marked for deletion they will transition
15+
# to DELETING state and finalize deletion
16+
puts "Waiting for #{cluster_id_1} to finish deletion"
17+
client_1.wait_until(:cluster_not_exists, identifier: cluster_id_1) do |w|
18+
# Wait for 5 minutes
19+
w.max_attempts = 30
20+
w.delay = 10
21+
end
22+
23+
puts "Waiting for #{cluster_id_2} to finish deletion"
24+
client_2.wait_until(:cluster_not_exists, identifier: cluster_id_2) do |w|
25+
# Wait for 5 minutes
26+
w.max_attempts = 30
27+
w.delay = 10
28+
end
29+
rescue Aws::Errors::ServiceError => e
30+
abort "Failed to delete multi-region clusters: #{e.message}"
31+
end
32+
33+
def main
34+
region_1 = ENV.fetch("CLUSTER_1_REGION", "us-east-1")
35+
cluster_id_1 = ENV.fetch("CLUSTER_1_ID")
36+
region_2 = ENV.fetch("CLUSTER_2_REGION", "us-east-2")
37+
cluster_id_2 = ENV.fetch("CLUSTER_2_ID")
38+
39+
delete_multi_region_clusters(region_1, cluster_id_1, region_2, cluster_id_2)
40+
puts "Deleted #{cluster_id_1} in #{region_1} and #{cluster_id_2} in #{region_2}"
41+
end
42+
43+
main if $PROGRAM_NAME == __FILE__

ruby/cluster_management/lib/delete_single_region.rb

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)