簡體   English   中英

如何使用AWS Java SDK創建新的AWS實例

[英]How to create a new AWS instance using AWS Java SDK

我正在嘗試使用AWS Java SDK創建一個新的AWS EC2實例,但是獲取到“參數groupId的Value()無效。該值不能為空” 這是我的代碼:

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);

有什么建議么?

您可能還需要添加VPC詳細信息。

PrivateIpAddresses,Monitoring以及其他必填字段。

我建議您嘗試使用AWS Console手動創建EC2實例,並查看其要求的必需參數是什么?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM