Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.
Gunakan DeleteVpc
dengan AWS SDK atau CLI
Contoh kode berikut menunjukkan cara menggunakanDeleteVpc
.
Contoh tindakan adalah kutipan kode dari program yang lebih besar dan harus dijalankan dalam konteks. Anda dapat melihat tindakan ini dalam konteks dalam contoh kode berikut:
- CLI
-
- AWS CLI
-
Untuk menghapus VPC
Contoh ini menghapus VPC yang ditentukan. Jika perintah berhasil, tidak ada output yang akan ditampilkan.
Perintah:
aws ec2 delete-vpc --vpc-id vpc-a01106c2
- PHP
-
- SDK untuk PHP
-
Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankannya di Repositori Contoh Kode AWS.
/**
* @param string $vpcId
* @return void
*/
public function deleteVpc(string $vpcId)
{
try {
$this->ec2Client->deleteVpc([
"VpcId" => $vpcId,
]);
}catch(Ec2Exception $caught){
echo "There was a problem deleting the VPC: {$caught->getAwsErrorMessage()}\n";
throw $caught;
}
}
- PowerShell
-
- Alat untuk PowerShell V4
-
Contoh 1: Contoh ini menghapus VPC yang ditentukan. Anda diminta untuk konfirmasi sebelum operasi berlangsung, kecuali jika Anda juga menentukan parameter Force.
Remove-EC2Vpc -VpcId vpc-12345678
Output:
Confirm
Are you sure you want to perform this action?
Performing operation "Remove-EC2Vpc (DeleteVpc)" on Target "vpc-12345678".
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):
- Alat untuk PowerShell V5
-
Contoh 1: Contoh ini menghapus VPC yang ditentukan. Anda diminta untuk konfirmasi sebelum operasi berlangsung, kecuali jika Anda juga menentukan parameter Force.
Remove-EC2Vpc -VpcId vpc-12345678
Output:
Confirm
Are you sure you want to perform this action?
Performing operation "Remove-EC2Vpc (DeleteVpc)" on Target "vpc-12345678".
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):
- Python
-
- SDK untuk Python (Boto3)
-
Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankannya di Repositori Contoh Kode AWS.
class VpcWrapper:
"""Encapsulates Amazon Elastic Compute Cloud (Amazon EC2) Amazon Virtual Private Cloud actions."""
def __init__(self, ec2_client: boto3.client):
"""
Initializes the VpcWrapper with an EC2 client.
:param ec2_client: A Boto3 Amazon EC2 client. This client provides low-level
access to AWS EC2 services.
"""
self.ec2_client = ec2_client
@classmethod
def from_client(cls) -> "VpcWrapper":
"""
Creates a VpcWrapper instance with a default EC2 client.
:return: An instance of VpcWrapper initialized with the default EC2 client.
"""
ec2_client = boto3.client("ec2")
return cls(ec2_client)
def delete(self, vpc_id: str) -> None:
"""
Deletes the specified VPC.
:param vpc_id: The ID of the VPC to delete.
"""
try:
self.ec2_client.delete_vpc(VpcId=vpc_id)
except ClientError as err:
logger.error(
"Couldn't delete VPC %s. Here's why: %s: %s",
vpc_id,
err.response["Error"]["Code"],
err.response["Error"]["Message"],
)
raise
Untuk daftar lengkap panduan pengembang AWS SDK dan contoh kode, lihatMembuat EC2 sumber daya Amazon menggunakan AWS SDK. Topik ini juga mencakup informasi tentang memulai dan detail tentang versi SDK sebelumnya.