Utilízalo CreateDBInstance con un SDK AWS - AWS Ejemplos de código de SDK

Hay más ejemplos de AWS SDK disponibles en el GitHub repositorio de ejemplos de AWS Doc SDK.

Las traducciones son generadas a través de traducción automática. En caso de conflicto entre la traducción y la version original de inglés, prevalecerá la version en inglés.

Utilízalo CreateDBInstance con un SDK AWS

Los siguientes ejemplos de código muestran cómo utilizar CreateDBInstance.

Java
SDK para Java 2.x
nota

Hay más en marcha GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de 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; }); }
  • Para obtener más información sobre la API, consulta la sección Crear DBInstance en la referencia de la AWS SDK for Java 2.x API.

Python
SDK para Python (Boto3)
nota

Hay más información al respecto GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de 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
  • Para obtener más información sobre la API, consulta la referencia de la API Create DBInstance in AWS SDK for Python (Boto3).