protected NodeMetadataBuilder buildInstance(
      final RunningInstance instance, NodeMetadataBuilder builder) {
    builder.providerId(instance.getId());
    builder.id(instance.getRegion() + "/" + instance.getId());
    String group = getGroupForInstance(instance);
    builder.group(group);
    // standard convention from aws-ec2, which might not be re-used outside.
    if (instance.getPrivateDnsName() != null)
      builder.hostname(instance.getPrivateDnsName().replaceAll("\\..*", ""));
    addCredentialsForInstance(builder, instance);
    builder.status(instanceToNodeStatus.get(instance.getInstanceState()));
    builder.backendStatus(instance.getRawState());

    // collect all ip addresses into one bundle in case the api mistakenly put a private address
    // into the public address field
    Builder<String> addressesBuilder = ImmutableSet.builder();
    if (Strings.emptyToNull(instance.getIpAddress()) != null)
      addressesBuilder.add(instance.getIpAddress());
    // Add dnsName (if available) to addresses, when the IPAddress is null
    // happens on Eucalyptus sometimes.
    else if (Strings.emptyToNull(instance.getDnsName()) != null)
      addressesBuilder.add(instance.getDnsName());
    if (Strings.emptyToNull(instance.getPrivateIpAddress()) != null)
      addressesBuilder.add(instance.getPrivateIpAddress());

    Set<String> addresses = addressesBuilder.build();

    builder.publicAddresses(filter(addresses, not(IsPrivateIPAddress.INSTANCE)));
    builder.privateAddresses(filter(addresses, IsPrivateIPAddress.INSTANCE));
    builder.hardware(parseHardware(instance));
    Location location = getLocationForAvailabilityZoneOrRegion(instance);
    builder.location(location);
    builder.imageId(instance.getRegion() + "/" + instance.getImageId());

    // extract the operating system from the image
    RegionAndName regionAndName = new RegionAndName(instance.getRegion(), instance.getImageId());
    try {
      Image image = imageMap.get().getUnchecked(regionAndName);
      if (image != null) builder.operatingSystem(image.getOperatingSystem());
    } catch (CacheLoader.InvalidCacheLoadException e) {
      logger.debug("image not found for %s: %s", regionAndName, e);
    } catch (UncheckedExecutionException e) {
      logger.debug("error getting image for %s: %s", regionAndName, e);
    }
    return builder;
  }
 private RunningInstance createInstance(String imageId) throws UnknownHostException {
   RunningInstance instance = null;
   while (instance == null) {
     try {
       System.out.printf("%d: running instance%n", System.currentTimeMillis());
       Reservation<? extends RunningInstance> reservation =
           client
               .getInstanceServices()
               .runInstancesInRegion(
                   null,
                   null, // allow
                   // ec2
                   // to
                   // chose
                   // an
                   // availability
                   // zone
                   imageId,
                   1, // minimum instances
                   1, // maximum instances
                   withKeyName(keyPair.getKeyName()) // key I created above
                       .asType(InstanceType.M1_SMALL) // smallest instance
                       // size
                       .withSecurityGroup(securityGroupName)); // group I
       // created
       // above
       instance = Iterables.getOnlyElement(reservation);
     } catch (HttpResponseException htpe) {
       if (htpe.getResponse().getStatusCode() == 400) continue;
       throw htpe;
     }
   }
   assertNotNull(instance.getId());
   assertEquals(instance.getInstanceState(), InstanceState.PENDING);
   instance = blockUntilWeCanSshIntoInstance(instance);
   return instance;
 }