2017-05-27 132 views
0

我试图使用AWS Java SDK创建新的AWS EC2实例,但得到“参数groupId的Value()无效。值不能为空” 。这里是我的代码:如何使用AWS Java SDK创建新的AWS实例

AWSCredentials credentials = null; 
try { 
    credentials = new ProfileCredentialsProvider().getCredentials(); 
} catch (Exception e) { 
    throw new AmazonClientException(
     "Cannot load the credentials from the credential profiles file. " + 
     "Please make sure that your credentials file is at the correct " + 
     "location (~/.aws/credentials), and is in valid format.", 
     e); 
} 

ec2 = AmazonEC2ClientBuilder.standard() 
    .withCredentials(new AWSStaticCredentialsProvider(credentials)) 
    .withRegion(Regions.US_WEST_2) 
    .build(); 

}

RunInstancesRequest runInstancesRequest = new RunInstancesRequest(); 
String ami_id = "ami-efd0428f";  //ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-20170414 
Collection<String> securityGroups = new ArrayList<>(); 
securityGroups.add("launch-wizard-1"); 
securityGroups.add("sg-9405c2f3"); 
runInstancesRequest.withImageId(ami_id) 
    .withInstanceType("t2.medium") 
    .withMinCount(1) 
    .withMaxCount(1) 
    .withKeyName("MyKeyName") 
    .withSecurityGroups(securityGroups); 

RunInstancesResult run_response = ec2.runInstances(runInstancesRequest); // fails here! 

String instance_id = run_response.getReservation().getReservationId(); 

Tag tag = new Tag() 
    .withKey("Name") 
    .withValue(tfCompanyName.getText()); 
Collection<Tag> tags = new ArrayList<>(); 
tags.add(tag); 

CreateTagsRequest tag_request = new CreateTagsRequest(); 
tag_request.setTags(tags); 

CreateTagsResult tag_response = ec2.createTags(tag_request); 

String s = String.format("Successfully started EC2 instance %s based on AMI %s",instance_id, ami_id); 
System.out.println(s); 

有什么建议?

回答

0

您可能还需要添加VPC详细信息。

PrivateIpAddresses,Monitoring是其他必填字段。

我建议您尝试使用AWS Console手动创建EC2实例,并查看它所要求的参数是什么?

+0

VPC的事情可能是问题,因为我确实有这些问题(不管它是什么)。我如何包含它?我没有看到任何.withVPC选项。 – user3217883

+0

我使用AWS控制台创建了一个。唯一需要的是AMI,Type和KeyPair – user3217883