【问题标题】:How to create a new AWS instance using AWS Java SDK如何使用 AWS Java 开发工具包创建新的 AWS 实例
【发布时间】:2017-05-31 03:07:50
【问题描述】:

我正在尝试使用 AWS Java SDK 创建一个新的 AWS EC2 实例,但得到 “参数 groupId 的值 () 无效。值不能为空”。这是我的代码:

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

有什么建议吗?

【问题讨论】:

    标签: java amazon-web-services amazon-ec2


    【解决方案1】:

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

    PrivateIpAddresses ,监控是其他必填字段。

    我建议您尝试使用 AWS 控制台手动创建 EC2 实例,看看它要求的必需参数是什么?

    【讨论】:

    • VPC 可能是问题所在,因为我确实有其中之一(不管它是什么)。我如何包含它?我没有看到任何 .withVPC 选项。
    • 我使用 AWS 控制台创建了一个。唯一需要的是 AMI、类型和密钥对
    猜你喜欢
    • 2018-05-20
    • 2016-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多