1111POLL_INTERVAL_SECONDS = 10
1212TIMEOUT_SECONDS = 1200 # 20 minutes
1313
14- def delete_db_subnet_group (neptune_client , subnet_group_name : str ):
15- request = {
16- 'DBSubnetGroupName' : subnet_group_name
17- }
18-
19- try :
20- neptune_client .delete_db_subnet_group (** request )
21- print (f" Deleting Subnet Group: { subnet_group_name } " )
22- except (botocore .ClientError , botocore .BotoCoreError ) as e :
23- print (f" Failed to delete subnet group '{ subnet_group_name } ': { e } " )
24-
25-
14+ # snippet-start:[neptune.python.delete.cluster.main]
2615def delete_db_cluster (neptune_client , cluster_id : str ):
2716 request = {
2817 'DBClusterIdentifier' : cluster_id ,
@@ -34,14 +23,13 @@ def delete_db_cluster(neptune_client, cluster_id: str):
3423 print (f" Deleting DB Cluster: { cluster_id } " )
3524 except Exception as e :
3625 print (f" Failed to delete DB Cluster '{ cluster_id } ': { e } " )
37-
26+ # snippet-end:[neptune.python.delete.cluster.main]
3827
3928def format_elapsed_time (seconds : int ) -> str :
4029 mins , secs = divmod (seconds , 60 )
4130 hours , mins = divmod (mins , 60 )
4231 return f"{ hours :02} :{ mins :02} :{ secs :02} "
4332
44-
4533def wait_until_instance_deleted (
4634 neptune_client ,
4735 instance_id : str ,
@@ -86,16 +74,37 @@ def wait_until_instance_deleted(
8674
8775 time .sleep (poll_interval_seconds )
8876
89-
77+ # snippet-start:[neptune.python.delete.instance.main]
9078def delete_db_instance (neptune_client , instance_id : str ):
79+ """
80+ Deletes a Neptune DB instance.
81+
82+ Args:
83+ neptune_client (boto3.client): The Neptune client object.
84+ instance_id (str): The ID of the Neptune DB instance to be deleted.
85+ """
9186 delete_db_instance_request = {
9287 'DBInstanceIdentifier' : instance_id ,
9388 'SkipFinalSnapshot' : True
9489 }
9590
9691 neptune_client .delete_db_instance (** delete_db_instance_request )
9792 print (f"Deleting DB Instance: { instance_id } " )
93+ # snippet-end:[neptune.python.delete.instance.main]
94+
95+ # snippet-start:[neptune.python.delete.subnet.group.main]
96+ def delete_db_subnet_group (neptune_client , subnet_group_name ):
97+ """
98+ Deletes a Neptune DB subnet group synchronously using Boto3.
9899
100+ :param subnet_group_name: The name of the DB subnet group to delete.
101+ """
102+ delete_group_request = {
103+ 'DBSubnetGroupName' : subnet_group_name
104+ }
105+ neptune_client .delete_db_subnet_group (** delete_group_request )
106+ print (f"️ Deleting Subnet Group: { subnet_group_name } " )
107+ # snippet-end:[neptune.python.delete.subnet.group.main]
99108
100109def wait_for_cluster_status (
101110 neptune_client ,
@@ -149,7 +158,7 @@ def wait_for_cluster_status(
149158
150159 time .sleep (poll_interval_seconds )
151160
152-
161+ # snippet-start:[neptune.python.start.cluster.main]
153162def start_db_cluster (neptune_client , cluster_identifier ):
154163 """
155164 Starts an Amazon Neptune DB cluster.
@@ -159,6 +168,8 @@ def start_db_cluster(neptune_client, cluster_identifier):
159168 cluster_identifier (str): The identifier of the DB cluster to start.
160169 """
161170
171+ time .sleep (30 )
172+
162173 # Create the request dictionary
163174 start_db_cluster_request = {
164175 'DBClusterIdentifier' : cluster_identifier
@@ -167,8 +178,9 @@ def start_db_cluster(neptune_client, cluster_identifier):
167178 # Call the API to start the DB cluster
168179 neptune_client .start_db_cluster (** start_db_cluster_request )
169180 print (f"DB Cluster started: { cluster_identifier } " )
181+ # snippet-end:[neptune.python.start.cluster.main]
170182
171-
183+ # snippet-start:[neptune.python.stop.cluster.main]
172184def stop_db_cluster (neptune_client , cluster_identifier : str ):
173185 """
174186 Stops an Amazon Neptune DB cluster.
@@ -186,8 +198,9 @@ def stop_db_cluster(neptune_client, cluster_identifier: str):
186198 # Call the API to stop the DB cluster
187199 neptune_client .stop_db_cluster (** stop_db_cluster_request )
188200 print (f"DB Cluster stopped: { cluster_identifier } " )
201+ # snippet-end:[neptune.python.stop.cluster.main]
189202
190-
203+ # snippet-start:[neptune.python.describe.cluster.main]
191204def describe_db_clusters (neptune_client , cluster_id : str ):
192205 """
193206 Describes the details of a specific Neptune DB cluster.
@@ -224,7 +237,7 @@ def describe_db_clusters(neptune_client, cluster_id: str):
224237 print (f"Preferred Backup Window: { cluster .get ('PreferredBackupWindow' )} " )
225238 print (f"Preferred Maintenance Window: { cluster .get ('PreferredMaintenanceWindow' )} " )
226239 print ("------" )
227-
240+ # snippet-end:[neptune.python.describe.cluster.main]
228241
229242def check_instance_status (neptune_client , instance_id : str , desired_status : str ):
230243 start_time = time .time ()
@@ -251,7 +264,7 @@ def check_instance_status(neptune_client, instance_id: str, desired_status: str)
251264
252265 time .sleep (POLL_INTERVAL_SECONDS )
253266
254-
267+ # snippet-start:[neptune.python.create.dbinstance.main]
255268def create_db_instance (neptune_client , db_instance_id : str , db_cluster_id : str ) -> str :
256269 create_db_instance_request = {
257270 'DBInstanceIdentifier' : db_instance_id ,
@@ -268,8 +281,9 @@ def create_db_instance(neptune_client, db_instance_id: str, db_cluster_id: str)
268281 instance_id = instance ['DBInstanceIdentifier' ]
269282 print (f"Created Neptune DB Instance: { instance_id } " )
270283 return instance_id
284+ # snippet-end:[neptune.python.create.dbinstance.main]
271285
272-
286+ # snippet-start:[neptune.python.create.cluster.main]
273287def create_db_cluster (neptune_client , db_name : str ) -> str :
274288 create_db_cluster_request = {
275289 'DBClusterIdentifier' : db_name ,
@@ -286,7 +300,7 @@ def create_db_cluster(neptune_client, db_name: str) -> str:
286300 cluster_id = cluster ['DBClusterIdentifier' ]
287301 print (f"DB Cluster created: { cluster_id } " )
288302 return cluster_id
289-
303+ # snippet-end:[neptune.python.create.cluster.main]
290304
291305def get_subnet_ids (vpc_id : str ) -> list [str ]:
292306 ec2_client = boto3 .client ('ec2' )
@@ -316,6 +330,7 @@ def get_default_vpc_id() -> str:
316330 return default_vpc_id
317331
318332
333+ # snippet-start:[neptune.python.create.subnet.main]
319334def create_subnet_group (neptune_client , group_name : str ):
320335 vpc_id = get_default_vpc_id ()
321336 subnet_ids = get_subnet_ids (vpc_id )
@@ -334,7 +349,7 @@ def create_subnet_group(neptune_client, group_name: str):
334349
335350 print (f"Subnet group created: { name } " )
336351 print (f"ARN: { arn } " )
337-
352+ # snippet-end:[neptune.python.create.subnet.main]
338353
339354def wait_for_input_to_continue ():
340355 while True :
@@ -410,6 +425,7 @@ def run_scenario(neptune_client, subnet_group_name: str, db_instance_id: str, cl
410425 delete_db_instance (neptune_client , db_instance_id )
411426 wait_until_instance_deleted (neptune_client , db_instance_id )
412427 delete_db_cluster (neptune_client , db_cluster_id )
428+ delete_db_subnet_group (neptune_client , subnet_group_name )
413429 print ("Neptune resources deleted successfully" )
414430
415431 print ("-" * 88 )
@@ -420,9 +436,9 @@ def main():
420436
421437 # Customize the following names to match your Neptune setup
422438 # (You must change these to unique values for your environment)
423- subnet_group_name = "neptuneSubnetGroup75 "
424- cluster_name = "neptuneCluster75 "
425- db_instance_id = "neptuneDB75 "
439+ subnet_group_name = "neptuneSubnetGroup78 "
440+ cluster_name = "neptuneCluster78 "
441+ db_instance_id = "neptuneDB78 "
426442
427443 print ("""
428444Amazon Neptune is a fully managed graph database service by AWS...
0 commit comments