Utilisation CreateDBInstance avec un AWS SDK - AWS Exemples de code SDK

D'autres exemples de AWS SDK sont disponibles dans le référentiel AWS Doc SDK Examples GitHub .

Les traductions sont fournies par des outils de traduction automatique. En cas de conflit entre le contenu d'une traduction et celui de la version originale en anglais, la version anglaise prévaudra.

Utilisation CreateDBInstance avec un AWS SDK

Les exemples de code suivants illustrent comment utiliser CreateDBInstance.

Java
SDK pour Java 2.x
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

/** * Creates a new Amazon Neptune DB instance asynchronously. * * @param dbInstanceId the identifier for the new DB instance * @param dbClusterId the identifier for the DB cluster that the new instance will be a part of * @return a {@link CompletableFuture} that completes with the identifier of the newly created DB instance * @throws CompletionException if the operation fails, with a cause of either: * - {@link ServiceQuotaExceededException} if the request would exceed the maximum quota, or * - a general exception with the failure message */ public CompletableFuture<String> createDBInstanceAsync(String dbInstanceId, String dbClusterId) { CreateDbInstanceRequest request = CreateDbInstanceRequest.builder() .dbInstanceIdentifier(dbInstanceId) .dbInstanceClass("db.r5.large") .engine("neptune") .dbClusterIdentifier(dbClusterId) .build(); return getAsyncClient().createDBInstance(request) .whenComplete((response, exception) -> { if (exception != null) { Throwable cause = exception.getCause(); if (cause instanceof ServiceQuotaExceededException) { throw new CompletionException("The operation was denied because the request would exceed the maximum quota.", cause); } throw new CompletionException("Failed to create Neptune DB instance: " + exception.getMessage(), exception); } }) .thenApply(response -> { String instanceId = response.dbInstance().dbInstanceIdentifier(); logger.info("Created Neptune DB Instance: " + instanceId); return instanceId; }); }
  • Pour plus de détails sur l'API, consultez Create DBInstance in AWS SDK for Java 2.x API Reference.

Python
SDK pour Python (Boto3)
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

def create_db_instance(neptune_client, db_instance_id: str, db_cluster_id: str) -> str: try: request = { 'DBInstanceIdentifier': db_instance_id, 'DBInstanceClass': 'db.r5.large', 'Engine': 'neptune', 'DBClusterIdentifier': db_cluster_id } print(f"Creating Neptune DB Instance: {db_instance_id}") response = neptune_client.create_db_instance(**request) instance = response.get('DBInstance') if not instance or 'DBInstanceIdentifier' not in instance: raise RuntimeError("Instance creation succeeded but no ID returned.") print(f"Waiting for DB Instance '{db_instance_id}' to become available...") waiter = neptune_client.get_waiter('db_instance_available') waiter.wait( DBInstanceIdentifier=db_instance_id, WaiterConfig={'Delay': 30, 'MaxAttempts': 40} ) print(f"DB Instance '{db_instance_id}' is now available.") return instance['DBInstanceIdentifier'] except ClientError as err: code = err.response["Error"]["Code"] message = err.response["Error"]["Message"] if code == "AccessDeniedException": print("Access denied. Please ensure you have the necessary permissions.") else: print(f"Couldn't create DB instance. Here's why: {code}: {message}") raise except Exception as e: print(f"Unexpected error creating DB instance '{db_instance_id}': {e}") raise RuntimeError(f"Unexpected error creating DB instance '{db_instance_id}': {e}") from e