2017-05-27 157 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