private CreateLaunchConfigurationRequest createLaunchConfigurationRequest(Formation formation) { Machine machine = formation.getMachine(); Scaling scaling = machine.getScaling(); Properties generalProperties = getMetaInformationProperties(formation); CloudInitScript initScript = getParallelCloudInitScript(machine, generalProperties); IamInstanceProfileSpecification profil = this.ec2Client.getIamInstanceProfileSpecification(getUserProperty(REPO_PROFILE)); return new CreateLaunchConfigurationRequest().withLaunchConfigurationName(scaling.getName()) .withInstanceType(machine.getInstanceType()).withImageId(machine.getImageId()) .withUserData(initScript.renderAsBase64()).withSecurityGroups(machine.getSecurityGroup()) .withIamInstanceProfile(profil.getName()) // the name NOT the ARN! .withKeyName(getUserProperty(EC2_INSTANCE_KEY)) .withInstanceMonitoring(new InstanceMonitoring().withEnabled(false)); }
/** * @inheritDoc */ @Override public String launchInstance( CreateInstanceOptions options, Identity identity ) { RunInstancesRequest request = new RunInstancesRequest(); if ( options.getInstanceProfileName() != null ) { request.setIamInstanceProfile( new IamInstanceProfileSpecification().withName( options.getInstanceProfileName() ) ); } request.setSubnetId( options.getWorkerOptions().getSubnetId() ); request.setSecurityGroupIds( options.getWorkerOptions().getSecurityGroupIds() ); if ( options.getUserData() != null ) { request.setUserData( Base64.encodeBase64String( options.getUserData().getBytes() ) ); } request.withMinCount( 1 ).withMaxCount( 1 ).withImageId( config.getAgentAmiId() ).withInstanceType( InstanceType.T1Micro ); request.setKeyName( options.getWorkerOptions().getKeyPairName() ); request.setClientToken( "launch-ec2-worker-" + contextProvider.getActivityExecutionContext().getWorkflowExecution().getWorkflowId() ); AmazonEC2 ec2 = ActivityUtils.createClient( AmazonEC2Client.class, identity ); RunInstancesResult result = ec2.runInstances( request ); return result.getReservation().getInstances().get( 0 ).getInstanceId(); }
public List<String> runInstances(int numberOfInstances) { if (numberOfInstances <= 0) return null; if (numberOfInstances > 20) { numberOfInstances = 20; logger.info("Can't run more than 20 instances."); } RunInstancesRequest request = new RunInstancesRequest(Configuration.EC2_IMAGE_ID, numberOfInstances, numberOfInstances); request.setInstanceType(Configuration.EC2_INSTANCE_TYPE); IamInstanceProfileSpecification prof = new IamInstanceProfileSpecification(); prof.setName(Configuration.EC2_IAM_PROFILE); // security concerns request.setIamInstanceProfile(prof); request.setUserData(getStartupScript()); // base64 request.setKeyName(Configuration.EC2_KEYPAIR_NAME); List<Instance> instances = _ec2.runInstances(request).getReservation().getInstances(); List<Tag> tags = new ArrayList<Tag>(); tags.add(new Tag(_tagKey, _defaultTag)); tags.add(new Tag("Name", _ofType.toString().toLowerCase())); logger.info("Launch instances: " + instances); List<String> instancesID = new ArrayList<String>(); for (Instance instance : instances) { instancesID.add(instance.getInstanceId()); } // tag the resources CreateTagsRequest tagreq = new CreateTagsRequest(instancesID, tags); _ec2.createTags(tagreq); return instancesID; }
private Instance runVirtualMachineLaunch(Formation formation) { Properties generalProperties = getMetaInformationProperties(formation); Machine machine = formation.getMachine(); CloudInitScript initScript = getParallelCloudInitScript(machine, generalProperties); initScript.save(); this.console.newLine(); // if ( true ) // return null; BlockDeviceMapping volumn = this.ec2Client.getBlockDeviceMapping("/dev/sda1", 20); IamInstanceProfileSpecification profil = this.ec2Client.getIamInstanceProfileSpecification(getUserProperty(REPO_PROFILE)); RunInstancesRequest request = new RunInstancesRequest().withInstanceType(machine.getInstanceType()) .withImageId(machine.getImageId()).withIamInstanceProfile(profil).withMinCount(1) .withMaxCount(1).withBlockDeviceMappings(volumn) .withUserData(initScript.renderAsBase64()) .withSecurityGroupIds(machine.getSecurityGroup()) .withKeyName(getUserProperty(EC2_INSTANCE_KEY)); String instanceId = runInstance(request); this.console.writeNl("Starting..."); waitForInstanceState(instanceId, "running"); assigneIpIfAvailable(instanceId, machine); assigneName(instanceId, machine); return this.ec2Client.getEC2InstanceById(instanceId); }
@Override public List<SpotInstanceRequest> call() { LaunchSpecification spec = new LaunchSpecification(); spec.withInstanceType(this.instanceTemplate.getInstanceType()); spec.withImageId(this.instanceTemplate.getAmiId()); InstanceNetworkInterfaceSpecification nic = new InstanceNetworkInterfaceSpecification(); nic.withDeviceIndex(0); // select a subnet at random nic.withSubnetId(randomSubnet()); nic.withAssociatePublicIpAddress(this.instanceTemplate.isAssignPublicIp()); nic.withGroups(this.instanceTemplate.getSecurityGroupIds()); spec.withNetworkInterfaces(nic); spec.withKeyName(this.instanceTemplate.getKeyPair()); spec.withIamInstanceProfile( new IamInstanceProfileSpecification().withArn(this.instanceTemplate.getIamInstanceProfileARN())); spec.withUserData(this.instanceTemplate.getEncodedUserData()); spec.withEbsOptimized(this.instanceTemplate.isEbsOptimized()); RequestSpotInstancesRequest spotRequest = new RequestSpotInstancesRequest().withInstanceCount(this.count) .withType(SpotInstanceType.Persistent).withSpotPrice(this.bidPrice).withLaunchSpecification(spec); RequestSpotInstancesResult result = getClient().getApi().requestSpotInstances(spotRequest); List<String> spotRequestIds = result.getSpotInstanceRequests().stream() .map(SpotInstanceRequest::getSpotInstanceRequestId).collect(Collectors.toList()); if (!this.instanceTemplate.getTags().isEmpty()) { tagRequests(spotRequestIds); } return awaitSpotRequests(spotRequestIds); }
@Override public List<Instance> call() { RunInstancesRequest request = new RunInstancesRequest(); request.withInstanceType(this.instanceTemplate.getInstanceType()); request.withImageId(this.instanceTemplate.getAmiId()); InstanceNetworkInterfaceSpecification nic = new InstanceNetworkInterfaceSpecification(); nic.withDeviceIndex(0); // select a subnet at random nic.withSubnetId(randomSubnet()); nic.withAssociatePublicIpAddress(this.instanceTemplate.isAssignPublicIp()); nic.withGroups(this.instanceTemplate.getSecurityGroupIds()); request.withNetworkInterfaces(nic); request.withKeyName(this.instanceTemplate.getKeyPair()); request.withIamInstanceProfile( new IamInstanceProfileSpecification().withArn(this.instanceTemplate.getIamInstanceProfileARN())); request.withUserData(this.instanceTemplate.getEncodedUserData()); request.withEbsOptimized(this.instanceTemplate.isEbsOptimized()); request.withMinCount(this.count).withMaxCount(this.count); if (!this.instanceTemplate.getTags().isEmpty()) { TagSpecification tagSpecifications = new TagSpecification().withResourceType(ResourceType.Instance); tagSpecifications.withTags(tags()); request.withTagSpecifications(tagSpecifications); } RunInstancesResult result = getClient().getApi().runInstances(request); List<Instance> launchedInstances = result.getReservation().getInstances(); List<String> instanceIds = launchedInstances.stream().map(Instance::getInstanceId).collect(Collectors.toList()); return awaitInstances(instanceIds); }
public static void run(AmazonEC2 machines, String pathToScript, int count, String profileArn) throws IOException { machines.runInstances( new RunInstancesRequest() .withImageId("ami-c7c0d6b3") // This used to be the official, Ireland running, 32 bit Amazon Machine Image. Or pick, for instance, [Ubuntu](http://cloud-images.ubuntu.com/locator/ec2/) .withInstanceType(InstanceType.T1Micro) // Smallest possible, cheapest. Be warned: Cc28xlarge can set you back 3.75$ per call per machine per hour... [Pricing](http://aws.amazon.com/fr/ec2/#pricing) .withMaxCount(count) .withMinCount(count) .withInstanceInitiatedShutdownBehavior(ShutdownBehavior.Terminate) .withIamInstanceProfile(new IamInstanceProfileSpecification().withArn(profileArn)) .withUserData(printBase64Binary(FileUtils.readFileToString(new File(pathToScript), "UTF-8").getBytes("UTF-8"))) ); }
/** * Builds a {@code RunInstancesRequest} starting from a template and a virtual instance ID. * Instances will be tagged as they're created. * * @param template the instance template * @param virtualInstanceId the virtual instance IDs * @param userDefinedTags user defined tags to attach to the instance */ @SuppressWarnings("ConstantConditions") private RunInstancesRequest newRunInstancesRequest(EC2InstanceTemplate template, String virtualInstanceId, List<Tag> userDefinedTags) { String image = template.getImage(); String type = template.getType(); InstanceNetworkInterfaceSpecification network = getInstanceNetworkInterfaceSpecification(template); List<BlockDeviceMapping> deviceMappings = getBlockDeviceMappings(template); LOG.info(">> Instance request type: {}, image: {}", type, image); List<Tag> tags = ec2TagHelper.getInstanceTags(template, virtualInstanceId, userDefinedTags); List<TagSpecification> tagSpecifications = Lists.newArrayList( new TagSpecification().withTags(tags).withResourceType(ResourceType.Instance), new TagSpecification().withTags(tags).withResourceType(ResourceType.Volume)); RunInstancesRequest request = new RunInstancesRequest() .withImageId(image) .withInstanceType(type) .withMaxCount(1) .withMinCount(1) .withClientToken(UUID.randomUUID().toString()) .withNetworkInterfaces(network) .withTagSpecifications(tagSpecifications) .withBlockDeviceMappings(deviceMappings) .withEbsOptimized(template.isEbsOptimized()); if (template.getIamProfileName().isPresent()) { request.withIamInstanceProfile(new IamInstanceProfileSpecification() .withName(template.getIamProfileName().get())); } if (template.getKeyName().isPresent()) { request.withKeyName(template.getKeyName().get()); } Placement placement = null; if (template.getAvailabilityZone().isPresent()) { placement = new Placement().withAvailabilityZone(template.getAvailabilityZone().get()); } if (template.getPlacementGroup().isPresent()) { placement = (placement == null) ? new Placement().withGroupName(template.getPlacementGroup().get()) : placement.withGroupName(template.getPlacementGroup().get()); } placement = (placement == null) ? new Placement().withTenancy(template.getTenancy()) : placement.withTenancy(template.getTenancy()); request.withPlacement(placement); Optional<String> userData = template.getUserData(); if (userData.isPresent()) { request.withUserData(userData.get()); } return request; }
/** * 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; }
private void createInstance(Context context, int count, String subnetId) throws Exception { String sgId = resource.securityGroup.remoteSecurityGroup.getGroupId(); RunInstancesRequest request = new RunInstancesRequest() .withKeyName(resource.keyPair.remoteKeyPair.getKeyName()) .withInstanceType(resource.instanceType) .withImageId(resource.ami.imageId()) .withSubnetId(subnetId) .withSecurityGroupIds(sgId) .withMinCount(count) .withMaxCount(count) .withUserData(Base64.encodeBase64String(Strings.bytes(userData(context.env)))); if (EBS.enableEBSOptimized(resource.instanceType)) { request.withEbsOptimized(true); } if (resource.instanceProfile != null) request.withIamInstanceProfile(new IamInstanceProfileSpecification() .withName(resource.instanceProfile.remoteInstanceProfile.getInstanceProfileName())); if (resource.ebs.rootVolumeSize != null) { request.getBlockDeviceMappings().add(new BlockDeviceMapping() .withDeviceName("/dev/sda1") .withEbs(new EbsBlockDevice().withVolumeSize(resource.ebs.rootVolumeSize).withVolumeType(resource.ebs.type))); } List<com.amazonaws.services.ec2.model.Instance> remoteInstances = AWS.ec2.runInstances(request, tags(context.env)); resource.remoteInstances.addAll(remoteInstances); for (com.amazonaws.services.ec2.model.Instance remoteInstance : remoteInstances) { String key = String.format("instance/%s/%s", resource.id, remoteInstance.getInstanceId()); StringBuilder builder = new StringBuilder(); builder.append("privateIP=").append(remoteInstance.getPrivateIpAddress()); if (resource.subnet == null || resource.subnet.type == SubnetType.PUBLIC) { builder.append(", publicDNS=").append(remoteInstance.getPublicDnsName()); } context.output(key, builder.toString()); } if (resource.elb != null) { List<String> instanceIds = remoteInstances.stream().map(com.amazonaws.services.ec2.model.Instance::getInstanceId).collect(Collectors.toList()); AWS.elb.attachInstances(resource.elb.remoteELB.getLoadBalancerName(), instanceIds, waitUntilInService); } }
public IamInstanceProfileSpecification getIamInstanceProfileSpecification(String name) { return new IamInstanceProfileSpecification().withName(name); }