List instances

List the instances in the current project.

Code sample

C#

To learn how to install and use the client library for Bigtable, see Bigtable client libraries.

// Lists instances in the project.
// Initialize request argument(s).
ListInstancesRequest listInstancesRequest = new ListInstancesRequest
{
    ParentAsProjectName = new ProjectName(projectId)
};
try
{
    // Make a request.
    Console.WriteLine("Waiting for operation to complete...");
    ListInstancesResponse instances = bigtableInstanceAdminClient.ListInstances(listInstancesRequest);
}
catch (Exception ex)
{
    Console.WriteLine($"Exception while requesting information about instances in {projectId} project");
    Console.WriteLine(ex.Message);
    return -1;
}
Console.WriteLine(new string('-', 50));

C++

To learn how to install and use the client library for Bigtable, see Bigtable client libraries.

namespace cbt = ::google::cloud::bigtable;
using ::google::cloud::StatusOr;
[](cbt::InstanceAdmin instance_admin) {
  StatusOr<cbt::InstanceList> instances = instance_admin.ListInstances();
  if (!instances) throw std::runtime_error(instances.status().message());
  for (auto const& instance : instances->instances) {
    std::cout << instance.name() << "\n";
  }
  if (!instances->failed_locations.empty()) {
    std::cout << "The Cloud Bigtable service reports that the following "
                 "locations are temporarily unavailable and no information "
                 "about instances in these locations can be obtained:\n";
    for (auto const& failed_location : instances->failed_locations) {
      std::cout << failed_location << "\n";
    }
  }
}

Java

To learn how to install and use the client library for Bigtable, see Bigtable client libraries.

try {
  List<Instance> instances = adminClient.listInstances();
  for (Instance instance : instances) {
    System.out.println(instance.getId());
  }
} catch (PartialListInstancesException e) {
  System.err.println("Failed to list instances: " + e.getMessage());
  System.err.println("The following zones are unavailable: " + e.getUnavailableZones());
  System.err.println("But the following instances are reachable: " + e.getInstances());
}

Node.js

To learn how to install and use the client library for Bigtable, see Bigtable client libraries.

const [instances] = await bigtable.getInstances();
instances.forEach(instance => {
  console.log(instance.id);
});

PHP

To learn how to install and use the client library for Bigtable, see Bigtable client libraries.

use Google\Cloud\Bigtable\Admin\V2\BigtableInstanceAdminClient;

/**
 * List Bigtable instances in a project
 *
 * @param string $projectId The Google Cloud project ID
 */
function list_instance(string $projectId): void
{
    $instanceAdminClient = new BigtableInstanceAdminClient();

    $projectName = $instanceAdminClient->projectName($projectId);

    printf('Listing Instances:' . PHP_EOL);

    $getInstances = $instanceAdminClient->listInstances($projectName)->getInstances();
    $instances = $getInstances->getIterator();

    foreach ($instances as $instance) {
        print($instance->getName() . PHP_EOL);
    }
}

Python

To learn how to install and use the client library for Bigtable, see Bigtable client libraries.

print("\nListing instances:")
for instance_local in client.list_instances()[0]:
    print(instance_local.instance_id)

Ruby

To learn how to install and use the client library for Bigtable, see Bigtable client libraries.

bigtable.instances.all do |instance|
  puts "Instance: #{instance.instance_id}"
end

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.