public static void main(String[] args) { String region = "us-east-1"; AWSCredentials credentials = new BasicAWSCredentials(awsAccessKeyId, awsSecretAccessKey); AmazonEC2 api = AmazonEC2ClientBuilder.standard().withRegion(region) .withCredentials(new AWSStaticCredentialsProvider(credentials)).build(); // no particular availability zone String availabilityZone = null; String instanceType = "t1.micro"; String imageId = "ami-3cf8b154"; List<String> securityGroups = Arrays.asList("webserver"); String keyPair = "instancekey"; String bootScript = Joiner.on("\n") .join(Arrays.asList("#!/bin/bash", "sudo apt-get update -qy", "sudo apt-get install -qy apache2")); int instanceCount = 50; String bidPrice = "0.001"; SpotPlacement placement = new SpotPlacement().withAvailabilityZone(availabilityZone); LaunchSpecification launchSpec = new LaunchSpecification().withInstanceType(instanceType).withImageId(imageId) .withPlacement(placement).withSecurityGroups(securityGroups).withKeyName(keyPair) .withUserData(AmazonApiUtils.base64Encode(bootScript)); RequestSpotInstancesRequest request = new RequestSpotInstancesRequest().withInstanceCount(instanceCount) .withType(SpotInstanceType.Persistent).withSpotPrice(bidPrice).withLaunchSpecification(launchSpec); RequestSpotInstancesResult result = api.requestSpotInstances(request); for (SpotInstanceRequest spotRequest : result.getSpotInstanceRequests()) { LOG.info("placed request: {}", spotRequest.getSpotInstanceRequestId()); } }
/** * Builds a {@code RequestSpotInstancesRequest}. * * @return the {@code RequestSpotInstancesRequest} */ @VisibleForTesting protected RequestSpotInstancesRequest newRequestSpotInstanceRequest(String virtualInstanceId) { String image = template.getImage(); String type = template.getType(); InstanceNetworkInterfaceSpecification network = getInstanceNetworkInterfaceSpecification(template); List<BlockDeviceMapping> deviceMappings = getBlockDeviceMappings(template); LaunchSpecification launchSpecification = new LaunchSpecification() .withImageId(image) .withInstanceType(type) .withNetworkInterfaces(network) .withBlockDeviceMappings(deviceMappings) .withEbsOptimized(template.isEbsOptimized()); if (template.getIamProfileName().isPresent()) { launchSpecification.withIamInstanceProfile(new IamInstanceProfileSpecification() .withName(template.getIamProfileName().get())); } if (template.getKeyName().isPresent()) { launchSpecification.withKeyName(template.getKeyName().get()); } SpotPlacement placement = null; if (template.getAvailabilityZone().isPresent()) { placement = new SpotPlacement().withAvailabilityZone(template.getAvailabilityZone().get()); } if (template.getPlacementGroup().isPresent()) { placement = (placement == null) ? new SpotPlacement().withGroupName(template.getPlacementGroup().get()) : placement.withGroupName(template.getPlacementGroup().get()); } launchSpecification.withPlacement(placement); Optional<String> userData = template.getUserData(); if (userData.isPresent()) { launchSpecification.withUserData(userData.get()); } LOG.info(">> Spot instance request type: {}, image: {}", type, image); RequestSpotInstancesRequest request = new RequestSpotInstancesRequest() .withSpotPrice(template.getSpotBidUSDPerHour().get().toString()) .withLaunchSpecification(launchSpecification) .withInstanceCount(1) .withClientToken(determineClientToken(virtualInstanceId, requestExpirationTime.getTime())) .withValidUntil(requestExpirationTime); Optional<Integer> blockDurationMinutes = template.getBlockDurationMinutes(); if (blockDurationMinutes.isPresent()) { request.withBlockDurationMinutes(blockDurationMinutes.get()); } return request; }
/** * Create a {@link SpotInstanceRequest} that may be associated with an * {@link Instance}. * * @param id * @param state * @param instanceId * @return */ public static SpotInstanceRequest spotRequest(String id, String state, String instanceId, Tag... tags) { return new SpotInstanceRequest().withSpotInstanceRequestId(id) .withLaunchSpecification(new LaunchSpecification().withInstanceType(InstanceType.M1Medium) .withPlacement(new SpotPlacement("us-east-1b"))) .withState(state).withInstanceId(instanceId).withTags(tags); }