文档 AWS SDK 示例 GitHub 存储库中还有更多 S AWS DK 示例
本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
与 AWS SDK CreateDBSubnetGroup
配合使用
以下代码示例演示如何使用 CreateDBSubnetGroup
。
- Java
-
- 适用于 Java 的 SDK 2.x
-
注意
还有更多相关信息 GitHub。在 AWS 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 /** * Creates a new DB subnet group asynchronously. * * @param groupName the name of the subnet group to create * @return a CompletableFuture that, when completed, returns the Amazon Resource Name (ARN) of the created subnet group * @throws CompletionException if the operation fails, with a cause that may be a ServiceQuotaExceededException if the request would exceed the maximum quota */ public CompletableFuture<String> createSubnetGroupAsync(String groupName) { // Get the Amazon Virtual Private Cloud (VPC) where the Neptune cluster and resources will be created String vpcId = getDefaultVpcId(); logger.info("VPC is : " + vpcId); List<String> subnetList = getSubnetIds(vpcId); for (String subnetId : subnetList) { System.out.println("Subnet group:" +subnetId); } CreateDbSubnetGroupRequest request = CreateDbSubnetGroupRequest.builder() .dbSubnetGroupName(groupName) .dbSubnetGroupDescription("Subnet group for Neptune cluster") .subnetIds(subnetList) .build(); return getAsyncClient().createDBSubnetGroup(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 subnet group: " + exception.getMessage(), exception); } }) .thenApply(response -> { String name = response.dbSubnetGroup().dbSubnetGroupName(); String arn = response.dbSubnetGroup().dbSubnetGroupArn(); logger.info("Subnet group created: " + name); return arn; }); }
-
有关 API 的详细信息,请参阅《AWS SDK for Java 2.x API 参考》中的 “创建DBSubnet群组”。
-
- Python
-
- 适用于 Python 的 SDK(Boto3)
-
注意
还有更多相关信息 GitHub。在 AWS 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 def create_subnet_group(neptune_client, group_name: str): """ Creates a Neptune DB subnet group and returns its name and ARN. Args: neptune_client (boto3.client): The Neptune client object. group_name (str): The desired name of the subnet group. Returns: tuple(str, str): (subnet_group_name, subnet_group_arn) Raises: RuntimeError: For quota errors or other AWS-related failures. """ vpc_id = get_default_vpc_id() subnet_ids = get_subnet_ids(vpc_id) request = { 'DBSubnetGroupName': group_name, 'DBSubnetGroupDescription': 'My Neptune subnet group', 'SubnetIds': subnet_ids, 'Tags': [{'Key': 'Environment', 'Value': 'Dev'}] } try: response = neptune_client.create_db_subnet_group(**request) sg = response.get("DBSubnetGroup", {}) name = sg.get("DBSubnetGroupName") arn = sg.get("DBSubnetGroupArn") if not name or not arn: raise RuntimeError("Response missing subnet group name or ARN.") print(f"Subnet group created: {name}") print(f"ARN: {arn}") return name, arn except ClientError as e: code = e.response["Error"]["Code"] msg = e.response["Error"]["Message"] if code == "ServiceQuotaExceededException": print("Subnet group quota exceeded.") raise RuntimeError("Subnet group quota exceeded.") from e else: print(f"AWS error [{code}]: {msg}") raise RuntimeError(f"AWS error [{code}]: {msg}") from e except Exception as e: print(f"Unexpected error creating subnet group '{group_name}': {e}") raise RuntimeError(f"Unexpected error creating subnet group '{group_name}': {e}") from e
-
有关 API 的详细信息,请参阅 Python 版AWS SDK 中创建DBSubnet群组 (Boto3) API 参考。
-
CreateDBInstance
CreateGraph