简体   繁体   English

EC2 Java SDK->使用公共IP启动实例

[英]EC2 java sdk -> launching an instance with a public ip

I'm trying to launch an instance into a subnet with a default setting of not assigning public IPs. 我正在尝试将实例启动到默认设置为不分配公共IP的子网中。

With the aws CLI, this is done by adding --associate-public-ip-address : 使用aws CLI,这可以通过添加--associate-public-ip-address

aws ec2 run-instances --image-id ami-xxx --count 1 --instance-type m4.large --key-name "xxx" --subnet-id subnet-xxx --security-group-ids sg-xxx --associate-public-ip-address

How to do the same thing when launching an instance using the Java SDK? 使用Java SDK启动实例时如何做同样的事情?

RunInstancesRequest runInstancesRequest = new RunInstancesRequest();

runInstancesRequest.withImageId(kAmiId)
                   .withInstanceType(kInstanceType)
                   .withMinCount(1)
                   .withMaxCount(1)
                   .withKeyName(kKeyName)
                   .withSecurityGroupIds(kSecurityGroups)
                   .withMonitoring(false)
                   .withSubnetId(kSubnet);

Got it working like this: 像这样工作:

InstanceNetworkInterfaceSpecification networkSpec = new InstanceNetworkInterfaceSpecification();
networkSpec.setDeviceIndex(0);
networkSpec.setSubnetId(kSubnet);
networkSpec.setGroups(Arrays.asList(kSecurityGroups));
networkSpec.setAssociatePublicIpAddress(true);

runInstancesRequest.withImageId(kAmiId)
                   .withInstanceType(kInstanceType)
                   .withMinCount(1)
                   .withMaxCount(1)
                   .withKeyName(kKeyName)
                   .withMonitoring(false)
                   .withNetworkInterfaces(networkSpec)
                   .withAdditionalInfo("--associate-public-ip-address");

RunInstancesResult result = mClient.runInstances(runInstancesRequest);

It looks like you have to set it by providing a InstanceNetworkInterfaceSpecification . 看来您必须通过提供InstanceNetworkInterfaceSpecification进行设置。 Something like this: 像这样:

List<InstanceNetworkInterfaceSpecification> interfaces = new ArrayList<InstanceNetworkInterfaceSpecification>();
interfaces.add(new InstanceNetworkInterfaceSpecification().withAssociatePublicIpAddress(true));

runInstancesRequest.withNetworkInterfaces(interfaces);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM