Create a cluster in the current project, given an instance ID.

## Code sample

### C++


To learn how to install and use the client library for Bigtable, see
[Bigtable client libraries](https://docs.cloud.google.com/bigtable/docs/reference/libraries).


To authenticate to Bigtable, set up Application Default Credentials.
For more information, see

[Set up authentication for a local development environment](https://docs.cloud.google.com/docs/authentication/set-up-adc-local-dev-environment).

    namespace cbt = ::google::cloud::bigtable;
    namespace cbta = ::google::cloud::bigtable_admin;
    using ::google::cloud::future;
    using ::google::cloud::Location;
    using ::google::cloud::Project;
    using ::google::cloud::StatusOr;
    [](cbta::BigtableInstanceAdminClient instance_admin,
       std::string const& project_id, std::string const& instance_id,
       std::string const& cluster_id, std::string const& zone) {
      auto const project = Project(project_id);
      std::string instance_name = cbt::InstanceName(project_id, instance_id);

      google::bigtable::admin::v2::Cluster c;
      c.set_location(Location(project, zone).FullName());
      c.set_serve_nodes(3);
      c.set_default_storage_type(google::bigtable::admin::v2::HDD);

      future<StatusOr<google::bigtable::admin::v2::Cluster>> cluster_future =
          instance_admin.CreateCluster(instance_name, cluster_id, std::move(c));

      // Applications can wait asynchronously, in this example we just block.
      auto cluster = cluster_future.get();
      if (!cluster) throw std::move(cluster).status();
      std::cout << "Successfully created cluster " << cluster->name() << "\n";
    }

### C#


To learn how to install and use the client library for Bigtable, see
[Bigtable client libraries](https://docs.cloud.google.com/bigtable/docs/reference/libraries).


To authenticate to Bigtable, set up Application Default Credentials.
For more information, see

[Set up authentication for a local development environment](https://docs.cloud.google.com/docs/authentication/set-up-adc-local-dev-environment).

    // Create an additional cluster with cluster id "ssd-cluster2" with 3 nodes and location us-east1-d.
    // Additional cluster can only be created in PRODUCTION type instance.
    // Additional cluster must have same storage type as existing cluster.
    // Please read about routing_policy for more information on mutli cluster instances.
    // https://cloud.google.com/bigtable/docs/reference/admin/rpc/google.bigtable.admin.v2#google.bigtable.admin.v2.AppProfile.MultiClusterRoutingUseAny
    // Cluster to be created within the instance.
    Cluster myCluster2 = new Cluster
    {
        DefaultStorageType = StorageType.Ssd,
        LocationAsLocationName = new LocationName(projectId, zone2),
        ServeNodes = 3
    };
    // Initialize request argument(s).
    CreateClusterRequest request = new CreateClusterRequest
    {
        ParentAsInstanceName = new InstanceName(projectId, instanceId),
        ClusterId = "ssd-cluster2",
        Cluster = myCluster2
    };
    try
    {
        // Make the request
        Console.WriteLine("Waiting for operation to complete...");
        Operation<Cluster, CreateClusterMetadata> response = bigtableInstanceAdminClient.CreateCluster(request);
        // Poll until the returned long-running operation is complete
        Operation<Cluster, CreateClusterMetadata> completedResponse = response.PollUntilCompleted();

    }
    catch (Exception ex)
    {
        Console.WriteLine($"Exception creating additional cluster {request.ClusterId} in instance {instanceId}");
        Console.WriteLine(ex.Message);
    }

### Java


To learn how to install and use the client library for Bigtable, see
[Bigtable client libraries](https://docs.cloud.google.com/bigtable/docs/reference/libraries).


To authenticate to Bigtable, set up Application Default Credentials.
For more information, see

[Set up authentication for a local development environment](https://docs.cloud.google.com/docs/authentication/set-up-adc-local-dev-environment).

    try {
      String parent = "projects/" + projectId + "/instances/" + instanceId;
      Cluster clusterObj =
          Cluster.newBuilder()
              .setLocation("projects/" + projectId + "/locations/us-central1-c")
              .setServeNodes(3)
              .setDefaultStorageType(StorageType.SSD)
              .build();
      CreateClusterRequest request =
          CreateClusterRequest.newBuilder()
              .setParent(parent)
              .setClusterId(CLUSTER)
              .setCluster(clusterObj)
              .build();
      adminClient.createClusterAsync(request).get();
      System.out.printf("Cluster: %s created successfully%n", CLUSTER);
    } catch (Exception e) {
      System.err.println("Failed to add cluster: " + e.getMessage());
    }

### PHP


To learn how to install and use the client library for Bigtable, see
[Bigtable client libraries](https://docs.cloud.google.com/bigtable/docs/reference/libraries).


To authenticate to Bigtable, set up Application Default Credentials.
For more information, see

[Set up authentication for a local development environment](https://docs.cloud.google.com/docs/authentication/set-up-adc-local-dev-environment).

    use Google\ApiCore\ApiException;
    use Google\Cloud\Bigtable\Admin\V2\Client\BigtableInstanceAdminClient;
    use Google\Cloud\Bigtable\Admin\V2\Cluster;
    use Google\Cloud\Bigtable\Admin\V2\CreateClusterRequest;
    use Google\Cloud\Bigtable\Admin\V2\GetClusterRequest;
    use Google\Cloud\Bigtable\Admin\V2\GetInstanceRequest;
    use Google\Cloud\Bigtable\Admin\V2\ListClustersRequest;
    use Google\Cloud\Bigtable\Admin\V2\StorageType;

    /**
     * Create a cluster in an existing Bigtable instance
     *
     * @param string $projectId The Google Cloud project ID
     * @param string $instanceId The ID of the parent Bigtable instance
     * @param string $clusterId The ID of the cluster to be generated
     * @param string $locationId The Bigtable region ID where you want your cluster to reside
     */
    function create_cluster(
        string $projectId,
        string $instanceId,
        string $clusterId,
        string $locationId = 'us-east1-b'
    ): void {
        $instanceAdminClient = new BigtableInstanceAdminClient();

        $instanceName = $instanceAdminClient->instanceName($projectId, $instanceId);
        $clusterName = $instanceAdminClient->clusterName($projectId, $instanceId, $clusterId);

        printf('Adding Cluster to Instance %s' . PHP_EOL, $instanceId);
        try {
            $getInstanceRequest = (new GetInstanceRequest())
                ->setName($instanceName);
            $instanceAdminClient->getInstance($getInstanceRequest);
        } catch (ApiException $e) {
            if ($e->getStatus() === 'NOT_FOUND') {
                printf('Instance %s does not exists.' . PHP_EOL, $instanceId);
                return;
            } else {
                throw $e;
            }
        }
        printf('Listing Clusters:' . PHP_EOL);

        $storage_type = StorageType::SSD;
        $serve_nodes = 3;
        $listClustersRequest = (new ListClustersRequest())
            ->setParent($instanceName);

        $clustersBefore = $instanceAdminClient->listClusters($listClustersRequest)->getClusters();
        $clusters = $clustersBefore->getIterator();
        foreach ($clusters as $cluster) {
            print($cluster->getName() . PHP_EOL);
        }

        $cluster = new Cluster();
        $cluster->setServeNodes($serve_nodes);
        $cluster->setDefaultStorageType($storage_type);
        $cluster->setLocation(
            $instanceAdminClient->locationName(
                $projectId,
                $locationId
            )
        );
        try {
            $getClusterRequest = (new GetClusterRequest())
                ->setName($clusterName);
            $instanceAdminClient->getCluster($getClusterRequest);
            printf('Cluster %s already exists, aborting...', $clusterId);
        } catch (ApiException $e) {
            if ($e->getStatus() === 'NOT_FOUND') {
                $createClusterRequest = (new CreateClusterRequest())
                    ->setParent($instanceName)
                    ->setClusterId($clusterId)
                    ->setCluster($cluster);
                $operationResponse = $instanceAdminClient->createCluster($createClusterRequest);

                $operationResponse->pollUntilComplete();
                if ($operationResponse->operationSucceeded()) {
                    $result = $operationResponse->getResult();
                    printf('Cluster created: %s', $clusterId);
                } else {
                    $error = $operationResponse->getError();
                    printf('Cluster not created: %s', $error?->getMessage());
                }
            } else {
                throw $e;
            }
        }
    }

### Python


To learn how to install and use the client library for Bigtable, see
[Bigtable client libraries](https://docs.cloud.google.com/bigtable/docs/reference/libraries).


To authenticate to Bigtable, set up Application Default Credentials.
For more information, see

[Set up authentication for a local development environment](https://docs.cloud.google.com/docs/authentication/set-up-adc-local-dev-environment).

    from google.api_core.exceptions import NotFound

    from google.cloud import bigtable_admin_v2

    client = bigtable_admin_v2.https://docs.cloud.google.com/python/docs/reference/bigtable/latest/google.cloud.bigtable_admin_v2.services.bigtable_instance_admin.BigtableInstanceAdminClient.html()
    instance_path = client.https://docs.cloud.google.com/python/docs/reference/bigtable/latest/google.cloud.bigtable_admin_v2.services.bigtable_instance_admin.BigtableInstanceAdminClient.html#google_cloud_bigtable_admin_v2_services_bigtable_instance_admin_BigtableInstanceAdminClient_instance_path(project_id, instance_id)
    cluster_path = client.https://docs.cloud.google.com/python/docs/reference/bigtable/latest/google.cloud.bigtable_admin_v2.services.bigtable_instance_admin.BigtableInstanceAdminClient.html#google_cloud_bigtable_admin_v2_services_bigtable_instance_admin_BigtableInstanceAdminClient_cluster_path(project_id, instance_id, cluster_id)

    print("\nListing clusters...")
    clusters_response = client.https://docs.cloud.google.com/python/docs/reference/bigtable/latest/google.cloud.bigtable_admin_v2.services.bigtable_instance_admin.BigtableInstanceAdminClient.html#google_cloud_bigtable_admin_v2_services_bigtable_instance_admin_BigtableInstanceAdminClient_list_clusters(parent=instance_path)
    for cluster in clusters_response.clusters:
        print(cluster.name.split("/")[-1])

    new_cluster = bigtable_admin_v2.https://docs.cloud.google.com/python/docs/reference/bigtable/latest/google.cloud.bigtable_admin_v2.types.Cluster.html(
        location=client.https://docs.cloud.google.com/python/docs/reference/bigtable/latest/google.cloud.bigtable_admin_v2.services.bigtable_instance_admin.BigtableInstanceAdminClient.html#google_cloud_bigtable_admin_v2_services_bigtable_instance_admin_BigtableInstanceAdminClient_common_location_path(project_id, "us-central1-a"),
        serve_nodes=1,
        default_storage_type=bigtable_admin_v2.https://docs.cloud.google.com/python/docs/reference/bigtable/latest/google.cloud.bigtable_admin_v2.types.StorageType.html.SSD,
    )
    try:
        client.https://docs.cloud.google.com/python/docs/reference/bigtable/latest/google.cloud.bigtable_admin_v2.services.bigtable_instance_admin.BigtableInstanceAdminClient.html#google_cloud_bigtable_admin_v2_services_bigtable_instance_admin_BigtableInstanceAdminClient_get_cluster(name=cluster_path)
        print(f"\nCluster not created, as {cluster_id} already exists.")
    except NotFound:
        operation = client.https://docs.cloud.google.com/python/docs/reference/bigtable/latest/google.cloud.bigtable_admin_v2.services.bigtable_instance_admin.BigtableInstanceAdminClient.html#google_cloud_bigtable_admin_v2_services_bigtable_instance_admin_BigtableInstanceAdminClient_create_cluster(
            parent=instance_path, cluster_id=cluster_id, cluster=new_cluster
        )
        # Ensure the operation completes.
        operation.result(timeout=480)
        print(f"\nCluster created: {cluster_id}")

### Ruby


To learn how to install and use the client library for Bigtable, see
[Bigtable client libraries](https://docs.cloud.google.com/bigtable/docs/reference/libraries).


To authenticate to Bigtable, set up Application Default Credentials.
For more information, see

[Set up authentication for a local development environment](https://docs.cloud.google.com/docs/authentication/set-up-adc-local-dev-environment).

    # cluster_id       = "my-cluster"
    # cluster_location = "us-east1-b"
    job = instance.create_cluster(
      cluster_id,
      cluster_location,
      nodes:        3,
      storage_type: :SSD
    )

    job.wait_until_done!
    cluster = job.cluster

## What's next


To search and filter code samples for other Google Cloud products, see the
[Google Cloud sample browser](https://docs.cloud.google.com/docs/samples?product=bigtable).