Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.
Usare DeleteDBInstance
con un AWS SDK o CLI
I seguenti esempi di codice mostrano come utilizzareDeleteDBInstance
.
Gli esempi di operazioni sono estratti di codice da programmi più grandi e devono essere eseguiti nel contesto. È possibile visualizzare questa operazione nel contesto nel seguente esempio di codice:
- .NET
-
- AWS SDK for .NET
-
Nota
C'è altro su. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS
. /// <summary> /// Delete a particular DB instance. /// </summary> /// <param name="dbInstanceIdentifier">DB instance identifier.</param> /// <returns>DB instance object.</returns> public async Task<DBInstance> DeleteDBInstanceByIdentifierAsync(string dbInstanceIdentifier) { var response = await _amazonRDS.DeleteDBInstanceAsync( new DeleteDBInstanceRequest() { DBInstanceIdentifier = dbInstanceIdentifier, SkipFinalSnapshot = true, DeleteAutomatedBackups = true }); return response.DBInstance; }
-
Per API i dettagli, vedere D eleteDBInstance in AWS SDK for .NET APIReference.
-
- C++
-
- SDKper C++
-
Nota
C'è di più su. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS
. Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; Aws::RDS::RDSClient client(clientConfig); Aws::RDS::Model::DeleteDBInstanceRequest request; request.SetDBInstanceIdentifier(dbInstanceIdentifier); request.SetSkipFinalSnapshot(true); request.SetDeleteAutomatedBackups(true); Aws::RDS::Model::DeleteDBInstanceOutcome outcome = client.DeleteDBInstance(request); if (outcome.IsSuccess()) { std::cout << "DB instance deletion has started." << std::endl; instanceDeleting = true; std::cout << "Waiting for DB instance to delete before deleting the parameter group." << std::endl; } else { std::cerr << "Error with Aurora::DeleteDBInstance. " << outcome.GetError().GetMessage() << std::endl; result = false; }
-
Per API i dettagli, vedere D eleteDBInstance in AWS SDK for C++ APIReference.
-
- Go
-
- SDKper Go V2
-
Nota
C'è altro da fare. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS
. type DbClusters struct { AuroraClient *rds.Client } // DeleteInstance deletes a DB instance. func (clusters *DbClusters) DeleteInstance(ctx context.Context, instanceName string) error { _, err := clusters.AuroraClient.DeleteDBInstance(ctx, &rds.DeleteDBInstanceInput{ DBInstanceIdentifier: aws.String(instanceName), SkipFinalSnapshot: aws.Bool(true), DeleteAutomatedBackups: aws.Bool(true), }) if err != nil { log.Printf("Couldn't delete instance %v: %v\n", instanceName, err) return err } else { return nil } }
-
Per API i dettagli, vedere D eleteDBInstance
in AWS SDK for Go APIReference.
-
- Java
-
- SDKper Java 2.x
-
Nota
C'è di più su. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS
. public static void deleteDatabaseInstance(RdsClient rdsClient, String dbInstanceIdentifier) { try { DeleteDbInstanceRequest deleteDbInstanceRequest = DeleteDbInstanceRequest.builder() .dbInstanceIdentifier(dbInstanceIdentifier) .deleteAutomatedBackups(true) .skipFinalSnapshot(true) .build(); DeleteDbInstanceResponse response = rdsClient.deleteDBInstance(deleteDbInstanceRequest); System.out.println("The status of the database is " + response.dbInstance().dbInstanceStatus()); } catch (RdsException e) { System.out.println(e.getLocalizedMessage()); System.exit(1); } }
-
Per API i dettagli, vedere D eleteDBInstance in AWS SDK for Java 2.x APIReference.
-
- Kotlin
-
- SDKper Kotlin
-
Nota
c'è altro da fare. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS
. suspend fun deleteDBInstance(dbInstanceIdentifierVal: String) { val deleteDbInstanceRequest = DeleteDbInstanceRequest { dbInstanceIdentifier = dbInstanceIdentifierVal deleteAutomatedBackups = true skipFinalSnapshot = true } RdsClient { region = "us-west-2" }.use { rdsClient -> val response = rdsClient.deleteDbInstance(deleteDbInstanceRequest) print("The status of the database is ${response.dbInstance?.dbInstanceStatus}") } }
-
Per API i dettagli, vedi D eleteDBInstance
in AWS SDKfor Kotlin reference API.
-
- Python
-
- SDKper Python (Boto3)
-
Nota
C'è di più su. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS
. class AuroraWrapper: """Encapsulates Aurora DB cluster actions.""" def __init__(self, rds_client): """ :param rds_client: A Boto3 Amazon Relational Database Service (Amazon RDS) client. """ self.rds_client = rds_client @classmethod def from_client(cls): """ Instantiates this class from a Boto3 client. """ rds_client = boto3.client("rds") return cls(rds_client) def delete_db_instance(self, instance_id): """ Deletes a DB instance. :param instance_id: The ID of the DB instance to delete. :return: Data about the deleted DB instance. """ try: response = self.rds_client.delete_db_instance( DBInstanceIdentifier=instance_id, SkipFinalSnapshot=True, DeleteAutomatedBackups=True, ) db_inst = response["DBInstance"] except ClientError as err: logger.error( "Couldn't delete DB instance %s. Here's why: %s: %s", instance_id, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return db_inst
-
Per API i dettagli, vedere D eleteDBInstance in AWS SDKfor Python (Boto3) Reference. API
-
- Rust
-
- SDKper Rust
-
Nota
c'è altro da fare GitHub. Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS
. pub async fn clean_up(self) -> Result<(), Vec<ScenarioError>> { let mut clean_up_errors: Vec<ScenarioError> = vec![]; // Delete the instance. rds.DeleteDbInstance. let delete_db_instance = self .rds .delete_db_instance( self.db_instance_identifier .as_deref() .expect("instance identifier"), ) .await; if let Err(err) = delete_db_instance { let identifier = self .db_instance_identifier .as_deref() .unwrap_or("Missing Instance Identifier"); let message = format!("failed to delete db instance {identifier}"); clean_up_errors.push(ScenarioError::new(message, &err)); } else { // Wait for the instance to delete let waiter = Waiter::default(); while waiter.sleep().await.is_ok() { let describe_db_instances = self.rds.describe_db_instances().await; if let Err(err) = describe_db_instances { clean_up_errors.push(ScenarioError::new( "Failed to check instance state during deletion", &err, )); break; } let db_instances = describe_db_instances .unwrap() .db_instances() .iter() .filter(|instance| instance.db_cluster_identifier == self.db_cluster_identifier) .cloned() .collect::<Vec<DbInstance>>(); if db_instances.is_empty() { trace!("Delete Instance waited and no instances were found"); break; } match db_instances.first().unwrap().db_instance_status() { Some("Deleting") => continue, Some(status) => { info!("Attempting to delete but instances is in {status}"); continue; } None => { warn!("No status for DB instance"); break; } } } } // Delete the DB cluster. rds.DeleteDbCluster. let delete_db_cluster = self .rds .delete_db_cluster( self.db_cluster_identifier .as_deref() .expect("cluster identifier"), ) .await; if let Err(err) = delete_db_cluster { let identifier = self .db_cluster_identifier .as_deref() .unwrap_or("Missing DB Cluster Identifier"); let message = format!("failed to delete db cluster {identifier}"); clean_up_errors.push(ScenarioError::new(message, &err)); } else { // Wait for the instance and cluster to fully delete. rds.DescribeDbInstances and rds.DescribeDbClusters until both are not found. let waiter = Waiter::default(); while waiter.sleep().await.is_ok() { let describe_db_clusters = self .rds .describe_db_clusters( self.db_cluster_identifier .as_deref() .expect("cluster identifier"), ) .await; if let Err(err) = describe_db_clusters { clean_up_errors.push(ScenarioError::new( "Failed to check cluster state during deletion", &err, )); break; } let describe_db_clusters = describe_db_clusters.unwrap(); let db_clusters = describe_db_clusters.db_clusters(); if db_clusters.is_empty() { trace!("Delete cluster waited and no clusters were found"); break; } match db_clusters.first().unwrap().status() { Some("Deleting") => continue, Some(status) => { info!("Attempting to delete but clusters is in {status}"); continue; } None => { warn!("No status for DB cluster"); break; } } } } // Delete the DB cluster parameter group. rds.DeleteDbClusterParameterGroup. let delete_db_cluster_parameter_group = self .rds .delete_db_cluster_parameter_group( self.db_cluster_parameter_group .map(|g| { g.db_cluster_parameter_group_name .unwrap_or_else(|| DB_CLUSTER_PARAMETER_GROUP_NAME.to_string()) }) .as_deref() .expect("cluster parameter group name"), ) .await; if let Err(error) = delete_db_cluster_parameter_group { clean_up_errors.push(ScenarioError::new( "Failed to delete the db cluster parameter group", &error, )) } if clean_up_errors.is_empty() { Ok(()) } else { Err(clean_up_errors) } } pub async fn delete_db_instance( &self, instance_identifier: &str, ) -> Result<DeleteDbInstanceOutput, SdkError<DeleteDBInstanceError>> { self.inner .delete_db_instance() .db_instance_identifier(instance_identifier) .skip_final_snapshot(true) .send() .await } #[tokio::test] async fn test_scenario_clean_up() { let mut mock_rds = MockRdsImpl::default(); mock_rds .expect_delete_db_instance() .with(eq("MockInstance")) .return_once(|_| Ok(DeleteDbInstanceOutput::builder().build())); mock_rds .expect_describe_db_instances() .with() .times(1) .returning(|| { Ok(DescribeDbInstancesOutput::builder() .db_instances( DbInstance::builder() .db_cluster_identifier("MockCluster") .db_instance_status("Deleting") .build(), ) .build()) }) .with() .times(1) .returning(|| Ok(DescribeDbInstancesOutput::builder().build())); mock_rds .expect_delete_db_cluster() .with(eq("MockCluster")) .return_once(|_| Ok(DeleteDbClusterOutput::builder().build())); mock_rds .expect_describe_db_clusters() .with(eq("MockCluster")) .times(1) .returning(|id| { Ok(DescribeDbClustersOutput::builder() .db_clusters( DbCluster::builder() .db_cluster_identifier(id) .status("Deleting") .build(), ) .build()) }) .with(eq("MockCluster")) .times(1) .returning(|_| Ok(DescribeDbClustersOutput::builder().build())); mock_rds .expect_delete_db_cluster_parameter_group() .with(eq("MockParamGroup")) .return_once(|_| Ok(DeleteDbClusterParameterGroupOutput::builder().build())); let mut scenario = AuroraScenario::new(mock_rds); scenario.db_cluster_identifier = Some(String::from("MockCluster")); scenario.db_instance_identifier = Some(String::from("MockInstance")); scenario.db_cluster_parameter_group = Some( DbClusterParameterGroup::builder() .db_cluster_parameter_group_name("MockParamGroup") .build(), ); tokio::time::pause(); let assertions = tokio::spawn(async move { let clean_up = scenario.clean_up().await; assert!(clean_up.is_ok()); }); tokio::time::advance(Duration::from_secs(1)).await; // Wait for first Describe Instances tokio::time::advance(Duration::from_secs(1)).await; // Wait for second Describe Instances tokio::time::advance(Duration::from_secs(1)).await; // Wait for first Describe Cluster tokio::time::advance(Duration::from_secs(1)).await; // Wait for second Describe Cluster tokio::time::resume(); let _ = assertions.await; } #[tokio::test] async fn test_scenario_clean_up_errors() { let mut mock_rds = MockRdsImpl::default(); mock_rds .expect_delete_db_instance() .with(eq("MockInstance")) .return_once(|_| Ok(DeleteDbInstanceOutput::builder().build())); mock_rds .expect_describe_db_instances() .with() .times(1) .returning(|| { Ok(DescribeDbInstancesOutput::builder() .db_instances( DbInstance::builder() .db_cluster_identifier("MockCluster") .db_instance_status("Deleting") .build(), ) .build()) }) .with() .times(1) .returning(|| { Err(SdkError::service_error( DescribeDBInstancesError::unhandled(Box::new(Error::new( ErrorKind::Other, "describe db instances error", ))), Response::new(StatusCode::try_from(400).unwrap(), SdkBody::empty()), )) }); mock_rds .expect_delete_db_cluster() .with(eq("MockCluster")) .return_once(|_| Ok(DeleteDbClusterOutput::builder().build())); mock_rds .expect_describe_db_clusters() .with(eq("MockCluster")) .times(1) .returning(|id| { Ok(DescribeDbClustersOutput::builder() .db_clusters( DbCluster::builder() .db_cluster_identifier(id) .status("Deleting") .build(), ) .build()) }) .with(eq("MockCluster")) .times(1) .returning(|_| { Err(SdkError::service_error( DescribeDBClustersError::unhandled(Box::new(Error::new( ErrorKind::Other, "describe db clusters error", ))), Response::new(StatusCode::try_from(400).unwrap(), SdkBody::empty()), )) }); mock_rds .expect_delete_db_cluster_parameter_group() .with(eq("MockParamGroup")) .return_once(|_| Ok(DeleteDbClusterParameterGroupOutput::builder().build())); let mut scenario = AuroraScenario::new(mock_rds); scenario.db_cluster_identifier = Some(String::from("MockCluster")); scenario.db_instance_identifier = Some(String::from("MockInstance")); scenario.db_cluster_parameter_group = Some( DbClusterParameterGroup::builder() .db_cluster_parameter_group_name("MockParamGroup") .build(), ); tokio::time::pause(); let assertions = tokio::spawn(async move { let clean_up = scenario.clean_up().await; assert!(clean_up.is_err()); let errs = clean_up.unwrap_err(); assert_eq!(errs.len(), 2); assert_matches!(errs.first(), Some(ScenarioError {message, context: _}) if message == "Failed to check instance state during deletion"); assert_matches!(errs.get(1), Some(ScenarioError {message, context: _}) if message == "Failed to check cluster state during deletion"); }); tokio::time::advance(Duration::from_secs(1)).await; // Wait for first Describe Instances tokio::time::advance(Duration::from_secs(1)).await; // Wait for second Describe Instances tokio::time::advance(Duration::from_secs(1)).await; // Wait for first Describe Cluster tokio::time::advance(Duration::from_secs(1)).await; // Wait for second Describe Cluster tokio::time::resume(); let _ = assertions.await; }
-
Per API i dettagli, vedi D eleteDBInstance
in AWS SDKfor Rust API reference.
-
Per un elenco completo delle guide per AWS SDK gli sviluppatori e degli esempi di codice, consultaUtilizzo di questo servizio con un AWS SDK. Questo argomento include anche informazioni su come iniziare e dettagli sulle SDK versioni precedenti.