AWS SDK와 CreateDBSubnetGroup 함께 사용 - AWS SDK 코드 예제

Doc AWS SDK 예제 GitHub 리포지토리에서 더 많은 SDK 예제를 사용할 수 있습니다. AWS

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

AWS SDK와 CreateDBSubnetGroup 함께 사용

다음 코드 예시는 CreateDBSubnetGroup의 사용 방법을 보여 줍니다.

Java
SDK for Java 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 세부 정보는 API 참조의 CreateDBSubnetGroupAWS SDK for Java 2.x 을 참조하세요.

Python
SDK for Python (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 세부 정보는 SDK for Python (Boto3) API 참조의 CreateDBSubnetGroup을 참조하세요. AWS