Utilizzo di DescribeDBInstances con un SDK AWS - Esempi di codice per SDK AWS

Sono disponibili altri esempi per SDK AWS nel repository GitHub della documentazione degli esempi per SDK AWS.

Utilizzo di DescribeDBInstances con un SDK AWS

Gli esempi di codice seguenti mostrano come utilizzare DescribeDBInstances.

Java
SDK per Java 2.x
Nota

Ulteriori informazioni su GitHub. Trova l’esempio completo e scopri di più sulla configurazione e l’esecuzione nel Repository di esempi di codice AWS.

/** * Checks the status of a Neptune instance recursively until the desired status is reached or a timeout occurs. * * @param instanceId the ID of the Neptune instance to check * @param desiredStatus the desired status of the Neptune instance * @param startTime the start time of the operation, used to calculate the elapsed time * @param future a {@link CompletableFuture} that will be completed when the desired status is reached */ private void checkStatusRecursive(String instanceId, String desiredStatus, long startTime, CompletableFuture<Void> future) { DescribeDbInstancesRequest request = DescribeDbInstancesRequest.builder() .dbInstanceIdentifier(instanceId) .build(); getAsyncClient().describeDBInstances(request) .whenComplete((response, exception) -> { if (exception != null) { Throwable cause = exception.getCause(); future.completeExceptionally( new CompletionException("Error checking Neptune instance status", cause) ); return; } List<DBInstance> instances = response.dbInstances(); if (instances.isEmpty()) { future.completeExceptionally(new RuntimeException("Instance not found: " + instanceId)); return; } String currentStatus = instances.get(0).dbInstanceStatus(); long elapsedSeconds = (System.currentTimeMillis() - startTime) / 1000; System.out.printf("\r Elapsed: %-20s Status: %-20s", formatElapsedTime((int) elapsedSeconds), currentStatus); System.out.flush(); if (desiredStatus.equalsIgnoreCase(currentStatus)) { System.out.printf("\r Neptune instance reached desired status '%s' after %s.\n", desiredStatus, formatElapsedTime((int) elapsedSeconds)); future.complete(null); } else { CompletableFuture.delayedExecutor(20, TimeUnit.SECONDS) .execute(() -> checkStatusRecursive(instanceId, desiredStatus, startTime, future)); } }); }
  • Per informazioni dettagliate sull’API, consulta DescribeDBInstances nella documentazione di riferimento dell’API AWS SDK for Java 2.x.

Python
SDK per Python (Boto3)
Nota

Ulteriori informazioni su GitHub. Trova l’esempio completo e scopri di più sulla configurazione e l’esecuzione nel Repository di esempi di codice AWS.

def check_instance_status(neptune_client, instance_id: str, desired_status: str): """ Polls the status of a Neptune DB instance until it reaches desired_status. Uses pagination via describe_db_instances — even for a single instance. Raises: ClientError: If describe_db_instances fails (e.g., instance not found). RuntimeError: If timeout expires before reaching desired status. """ paginator = neptune_client.get_paginator('describe_db_instances') start_time = time.time() while True: try: pages = paginator.paginate(DBInstanceIdentifier=instance_id) instances = [] for page in pages: instances.extend(page.get('DBInstances', [])) except ClientError as err: code = err.response["Error"]["Code"] message = err.response["Error"]["Message"] if code == "DBInstanceNotFound": print(f"Instance '{instance_id}' not found. Please verify the instance ID.") else: print(f"Failed to describe DB instance. {code}: {message}") raise current_status = instances[0].get('DBInstanceStatus') if instances else None elapsed = int(time.time() - start_time) print(f"\rElapsed: {format_elapsed_time(elapsed)} Status: {current_status}", end="", flush=True) if current_status and current_status.lower() == desired_status.lower(): print(f"\nInstance '{instance_id}' reached '{desired_status}' in {format_elapsed_time(elapsed)}.") return if elapsed > TIMEOUT_SECONDS: raise RuntimeError(f"Timeout waiting for '{instance_id}' to reach '{desired_status}'") time.sleep(POLL_INTERVAL_SECONDS)
  • Per informazioni dettagliate sull’API, consulta DescribeDBInstances nella documentazione di riferimento dell’API AWS SDK per Python (Boto3).