|
1 | 1 | import boto3 |
| 2 | +import os |
2 | 3 |
|
3 | | -def create_multi_region_clusters(client, linkedRegionList, witnessRegion, clusterProperties): |
| 4 | + |
| 5 | +def create_multi_region_clusters(region_1, region_2, witness_region): |
4 | 6 | try: |
5 | | - response = client.create_multi_region_clusters( |
6 | | - linkedRegionList=linkedRegionList, |
7 | | - witnessRegion=witnessRegion, |
8 | | - clusterProperties=clusterProperties, |
| 7 | + client_1 = boto3.client("dsql", region_name=region_1) |
| 8 | + client_2 = boto3.client("dsql", region_name=region_2) |
| 9 | + |
| 10 | + # We can only set the witness region for the first cluster |
| 11 | + cluster_1 = client_1.create_cluster( |
| 12 | + deletionProtectionEnabled=True, |
| 13 | + multiRegionProperties={"witnessRegion": witness_region}, |
| 14 | + tags={"Name": "Python-CM-Example-Multi-Region", "Repo": "aws-samples/aurora-dsql-samples"} |
| 15 | + ) |
| 16 | + print(f"Created {cluster_1["arn"]}") |
| 17 | + |
| 18 | + # For the second cluster we can set witness region and designate cluster_1 as a peer |
| 19 | + cluster_2 = client_2.create_cluster( |
| 20 | + deletionProtectionEnabled=True, |
| 21 | + multiRegionProperties={"witnessRegion": witness_region, "clusters": [cluster_1["arn"]]}, |
| 22 | + tags={"Name": "Python-CM-Example-Multi-Region", "Repo": "aws-samples/aurora-dsql-samples"} |
9 | 23 | ) |
10 | | - return response |
| 24 | + |
| 25 | + print(f"Created {cluster_2["arn"]}") |
| 26 | + # Now that we know the cluster_2 arn we can set it as a peer of cluster_1 |
| 27 | + client_1.update_cluster( |
| 28 | + identifier=cluster_1["identifier"], |
| 29 | + multiRegionProperties={"witnessRegion": witness_region, "clusters": [cluster_2["arn"]]} |
| 30 | + ) |
| 31 | + print(f"Added {cluster_2["arn"]} as a peer of {cluster_1["arn"]}") |
| 32 | + |
| 33 | + # Now that multiRegionProperties is fully defined for both clusters |
| 34 | + # they'll begin the transition to ACTIVE |
| 35 | + print(f"Waiting for {cluster_1["arn"]} to become ACTIVE") |
| 36 | + client_1.get_waiter("cluster_active").wait( |
| 37 | + identifier=cluster_1["identifier"], |
| 38 | + WaiterConfig={ |
| 39 | + 'Delay': 10, |
| 40 | + 'MaxAttempts': 50 |
| 41 | + } |
| 42 | + ) |
| 43 | + |
| 44 | + print(f"Waiting for {cluster_2["arn"]} to become ACTIVE") |
| 45 | + client_2.get_waiter("cluster_active").wait( |
| 46 | + identifier=cluster_2["identifier"], |
| 47 | + WaiterConfig={ |
| 48 | + 'Delay': 10, |
| 49 | + 'MaxAttempts': 50 |
| 50 | + } |
| 51 | + ) |
| 52 | + |
| 53 | + return cluster_1, cluster_2 |
| 54 | + |
11 | 55 | except: |
12 | | - print("Unable to create multi-region cluster") |
| 56 | + print("Unable to create cluster") |
13 | 57 | raise |
14 | 58 |
|
| 59 | + |
15 | 60 | def main(): |
16 | | - region = "us-east-1" |
17 | | - client = boto3.client("dsql", region_name=region) |
18 | | - linkedRegionList = ["us-east-1", "us-east-2"] |
19 | | - witnessRegion = "us-west-2" |
20 | | - clusterProperties = { |
21 | | - "us-east-1": {"tags": {"Name": "Foo"}}, |
22 | | - "us-east-2": {"tags": {"Name": "Bar"}} |
23 | | - } |
24 | | - response = create_multi_region_clusters(client, linkedRegionList, witnessRegion, clusterProperties) |
25 | | - print("Linked Cluster Arns:", response['linkedClusterArns']) |
| 61 | + region_1 = os.environ.get("REGION_1", "us-east-1") |
| 62 | + region_2 = os.environ.get("REGION_2", "us-east-2") |
| 63 | + witness_region = os.environ.get("WITNESS_REGION", "us-west-2") |
| 64 | + (cluster_1, cluster_2) = create_multi_region_clusters(region_1, region_2, witness_region) |
| 65 | + print("Created multi region clusters:") |
| 66 | + print("Cluster id: " + cluster_1['arn']) |
| 67 | + print("Cluster id: " + cluster_2['arn']) |
26 | 68 |
|
27 | 69 |
|
28 | 70 | if __name__ == "__main__": |
|
0 commit comments