使用public ip

时间:2016-07-28 10:05:30

标签: java amazon-ec2

我需要从我的java代码中启动ec2中的新实例。 我可以通过以下方式实现:

    RunInstancesRequest runInstancesRequest = new RunInstancesRequest();

runInstancesRequest
    .withImageId(myImageId)
    .withInstanceType(myInstanceType)
    .withMinCount(INSTANCES_COUNT)
    .withMaxCount(INSTANCES_COUNT)
    .withKeyName(myKeyName)
    .withSubnetId(mySubnetId)
    .withSecurityGroupIds(mySecurityGroupId)
    .withPlacement(myPlace)
;

RunInstancesResult runInstancesResult = amazonEC2Client.runInstances(runInstancesRequest);

它创建了一个新实例。但是这个实例有空字段" Public DNS"和#34;公共知识产权"。

我不需要弹性IP。如果所有新实例都有新地址,则可以。

那么,如何更改我的请求呢?

1 个答案:

答案 0 :(得分:1)

谢谢!我找到了解决方案:

RunInstancesRequest runInstancesRequest = new RunInstancesRequest();

InstanceNetworkInterfaceSpecification interfaceSpecification = new InstanceNetworkInterfaceSpecification()
    .withSubnetId(mySubnetId)
    .withAssociatePublicIpAddress(true)
    .withGroups(mySecurityGroupId)
    .withDeviceIndex(0);

runInstancesRequest
    .withImageId(myImageId)
    .withInstanceType(myInstanceType)
    .withMinCount(INSTANCES_COUNT)
    .withMaxCount(INSTANCES_COUNT)
    .withKeyName(myKeyName)
    //.withSubnetId(mySubnetId)
    //.withSecurityGroupIds(mySecurityGroupId)
    .withPlacement(myPlace)
    .withNetworkInterfaces(interfaceSpecification)
;

RunInstancesResult runInstancesResult = amazonEC2Client.runInstances(runInstancesRequest);

我不知道什么是“DeviceIndex”,为什么它是“0”,但它有效。