本文整理汇总了Java中com.amazonaws.services.ec2.model.RunInstancesRequest.withKeyName方法的典型用法代码示例。如果您正苦于以下问题:Java RunInstancesRequest.withKeyName方法的具体用法?Java RunInstancesRequest.withKeyName怎么用?Java RunInstancesRequest.withKeyName使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.amazonaws.services.ec2.model.RunInstancesRequest
的用法示例。
在下文中一共展示了RunInstancesRequest.withKeyName方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: call
import com.amazonaws.services.ec2.model.RunInstancesRequest; //导入方法依赖的package包/类
@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);
}
示例2: createAndRunInstance
import com.amazonaws.services.ec2.model.RunInstancesRequest; //导入方法依赖的package包/类
private void createAndRunInstance(AmazonEC2Client ec2Client, Exchange exchange) {
String ami;
InstanceType instanceType;
int minCount;
int maxCount;
boolean monitoring;
String kernelId;
boolean ebsOptimized;
Collection securityGroups;
String keyName;
String clientToken;
Placement placement;
RunInstancesRequest request = new RunInstancesRequest();
if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.IMAGE_ID))) {
ami = exchange.getIn().getHeader(EC2Constants.IMAGE_ID, String.class);
request.withImageId(ami);
} else {
throw new IllegalArgumentException("AMI must be specified");
}
if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_TYPE))) {
instanceType = exchange.getIn().getHeader(EC2Constants.INSTANCE_TYPE, InstanceType.class);
request.withInstanceType(instanceType.toString());
} else {
throw new IllegalArgumentException("Instance Type must be specified");
}
if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_MIN_COUNT))) {
minCount = exchange.getIn().getHeader(EC2Constants.INSTANCE_MIN_COUNT, Integer.class);
request.withMinCount(minCount);
} else {
throw new IllegalArgumentException("Min instances count must be specified");
}
if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_MAX_COUNT))) {
maxCount = exchange.getIn().getHeader(EC2Constants.INSTANCE_MAX_COUNT, Integer.class);
request.withMaxCount(maxCount);
} else {
throw new IllegalArgumentException("Max instances count must be specified");
}
if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_MONITORING))) {
monitoring = exchange.getIn().getHeader(EC2Constants.INSTANCE_MONITORING, Boolean.class);
request.withMonitoring(monitoring);
}
if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_KERNEL_ID))) {
kernelId = exchange.getIn().getHeader(EC2Constants.INSTANCE_KERNEL_ID, String.class);
request.withKernelId(kernelId);
}
if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_EBS_OPTIMIZED))) {
ebsOptimized = exchange.getIn().getHeader(EC2Constants.INSTANCE_EBS_OPTIMIZED, Boolean.class);
request.withEbsOptimized(ebsOptimized);
}
if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_SECURITY_GROUPS))) {
securityGroups = exchange.getIn().getHeader(EC2Constants.INSTANCE_SECURITY_GROUPS, Collection.class);
request.withSecurityGroups(securityGroups);
}
if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCES_KEY_PAIR))) {
keyName = exchange.getIn().getHeader(EC2Constants.INSTANCES_KEY_PAIR, String.class);
request.withKeyName(keyName);
}
if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCES_CLIENT_TOKEN))) {
clientToken = exchange.getIn().getHeader(EC2Constants.INSTANCES_CLIENT_TOKEN, String.class);
request.withClientToken(clientToken);
}
if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCES_PLACEMENT))) {
placement = exchange.getIn().getHeader(EC2Constants.INSTANCES_PLACEMENT, Placement.class);
request.withPlacement(placement);
}
RunInstancesResult result;
try {
result = ec2Client.runInstances(request);
} catch (AmazonServiceException ase) {
LOG.trace("Run Instances command returned the error code {}", ase.getErrorCode());
throw ase;
}
LOG.trace("Creating and running instances with ami [{}] and instance type {}", ami, instanceType.toString());
Message message = getMessageForResponse(exchange);
message.setBody(result);
}
示例3: newRunInstancesRequest
import com.amazonaws.services.ec2.model.RunInstancesRequest; //导入方法依赖的package包/类
/**
* 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;
}
示例4: CreateServer
import com.amazonaws.services.ec2.model.RunInstancesRequest; //导入方法依赖的package包/类
public static String CreateServer(String keys, JSONObject endpointsAPI, String imageId, String typeId, String keypair)
{
String instanceId = null;
String accessKey = keys.split(";")[0];
String secretKey = keys.split(";")[1];
String regionEndpoint = (String) endpointsAPI.get("amazon-regionEndpoint");
try
{
// EC2 Client for given region and credentials
AmazonEC2 ec2 = new AmazonEC2Client(new BasicAWSCredentials(accessKey, secretKey));
ec2.setEndpoint(regionEndpoint);
RunInstancesRequest req = new RunInstancesRequest();
req.withInstanceType(typeId);
req.withImageId(imageId);
req.withMinCount(1).withMaxCount(1);
req.withSecurityGroups("securityGroup");
req.withKeyName(keypair);
// Execute run instance request and return ID of first (and only) instance
RunInstancesResult runInstances = ec2.runInstances(req);
instanceId = runInstances.getReservation().getInstances().get(0).getInstanceId();
}
catch (AmazonServiceException ase)
{
System.out.println("Caught an AmazonServiceException, which means your request made it " + "to AWS, but was rejected with an error response for some reason.");
System.out.println("Error Message: " + ase.getMessage());
System.out.println("HTTP Status Code: " + ase.getStatusCode());
System.out.println("AWS Error Code: " + ase.getErrorCode());
System.out.println("Error Type: " + ase.getErrorType());
System.out.println("Request ID: " + ase.getRequestId());
instanceId = "MSG_FAILED";
}
catch (AmazonClientException ace)
{
System.out.println("Caught an AmazonClientException, which means the client encountered " + "a serious internal problem while trying to communicate with AWS, " + "such as not being able to access the network.");
System.out.println("Error Message: " + ace.getMessage());
instanceId = "MSG_FAILED";
}
return instanceId;
}
示例5: launchNodes
import com.amazonaws.services.ec2.model.RunInstancesRequest; //导入方法依赖的package包/类
public List<Instance> launchNodes(final String amiId, final String instanceType, final int numberToStart,
final String userData, final boolean terminateOnShutdown) throws NodesCouldNotBeStartedException {
RunInstancesRequest runRequest = new RunInstancesRequest();
runRequest.withImageId(amiId).withInstanceType(instanceType).withMinCount(numberToStart)
.withMaxCount(numberToStart).withUserData(userData);
if (terminateOnShutdown) {
runRequest.withInstanceInitiatedShutdownBehavior("terminate");
}
log.info("Setting image id: " + runRequest.getImageId());
log.info("Setting instance type: " + runRequest.getInstanceType());
String subnetKey = awsProperties.getProperty(region + "_subnet_id");
if (subnetKey != null) {
log.info("Setting subnet: " + subnetKey);
runRequest.withSubnetId(subnetKey);
}
String securityGroupKey = awsProperties.getProperty(region + "_security_group");
if (securityGroupKey != null) {
String[] splitSecurityGroupdIds = securityGroupKey.split(",");
List securityGroupIdsAryLst = new ArrayList();
for (int i = 0; i < splitSecurityGroupdIds.length; i++) {
log.info("Setting security group(s): " + splitSecurityGroupdIds[i]);
securityGroupIdsAryLst.add(splitSecurityGroupdIds[i]);
}
runRequest.setSecurityGroupIds(securityGroupIdsAryLst);
}
String keyName = awsProperties.getProperty(region + "_key_name");
if (keyName != null) {
log.info("Setting keyname:" + keyName);
runRequest.withKeyName(keyName);
}
log.info("Sending run request to AWS...");
RunInstancesResult runInstancesResult = getResults(runRequest, 0);
log.info("Run request result returned. Adding tags");
// Tag the instances with the standard RMN AWS data
List<Instance> instances = runInstancesResult.getReservation().getInstances();
if (instances.size() == 0) {
throw new NodesCouldNotBeStartedException(String.format(
"Error starting up nodes -- count was zero and did not match expected count of %d", numberToStart));
}
associateTags(new Date().toString(), instances);
return instances;
}