我尝试将实例启动到子网中,默认设置为不分配公共IP。
使用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
使用Java SDK启动实例时如何做同样的事情?
RunInstancesRequest runInstancesRequest = new RunInstancesRequest();
runInstancesRequest.withImageId(kAmiId)
.withInstanceType(kInstanceType)
.withMinCount(1)
.withMaxCount(1)
.withKeyName(kKeyName)
.withSecurityGroupIds(kSecurityGroups)
.withMonitoring(false)
.withSubnetId(kSubnet);
让它像这样工作:
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);
答案 0 :(得分:1)
看起来你必须通过提供InstanceNetworkInterfaceSpecification
来设置它。像这样:
List<InstanceNetworkInterfaceSpecification> interfaces = new ArrayList<InstanceNetworkInterfaceSpecification>();
interfaces.add(new InstanceNetworkInterfaceSpecification().withAssociatePublicIpAddress(true));
runInstancesRequest.withNetworkInterfaces(interfaces);