如何在 EC2 启动实例请求中获取 Public IP 地址?
How to obtain the Public IP Address on EC2 launch instance request?
我不知道我该怎么做:
runInstancesRequest.withImageId("ami-53170b32")
.withInstanceType("t2.micro")
.withMinCount(1)
.withMaxCount(1)
.withKeyName("mac")
.withSecurityGroupIds("sg-49025d2d");
RunInstancesResult runInstancesResult =
amazonEC2Client.runInstances(runInstancesRequest);
到目前为止一切正常。现在我想从最近启动的实例中获取 Public IP 地址。我该怎么做?
我试过了:
runInstancesResult.getReservation().getInstances().get(0).getPublicIpAddress()
但 IP 始终为空。
启动实例时,它会进入 Pending
状态并且还没有 Public IP 地址。您需要稍等片刻才能使用。
几秒钟后,使用最初返回的实例 ID 调用 DescribeInstances
,然后提取 PublicIpAddress
。
这是从 AWS Command-Line Interface (CLI):
进行的转储
$ aws ec2 run-instances --image-id ami-1500742f ...
{
"OwnerId": "123456789012",
"ReservationId": "r-0d8cc4a12a94faba7",
"Groups": [],
"Instances": [
{
"Monitoring": {
"State": "disabled"
},
"PublicDnsName": "",
"KernelId": "aki-c362fff9",
"State": {
"Code": 0,
"Name": "pending"
},
"EbsOptimized": false,
"LaunchTime": "2016-01-22T21:17:49.000Z",
"PrivateIpAddress": "172.31.12.208",
"ProductCodes": [],
"VpcId": "vpc-7d087014",
"StateTransitionReason": "",
"InstanceId": "i-0afe19e0d061b95b5",
...
}
$ aws ec2 describe-instances --instance-ids i-0afe19e0d061b95b5
{
"Reservations": [
{
"OwnerId": "123456789012",
"ReservationId": "r-0d8cc4a12a94faba7",
"Groups": [],
"Instances": [
{
"Monitoring": {
"State": "disabled"
},
"PublicDnsName": "ec2-52-62-35-146.ap-southeast-2.compute.amazonaws.com",
"RootDeviceType": "ebs",
"State": {
"Code": 16,
"Name": "running"
},
"EbsOptimized": false,
"LaunchTime": "2016-01-22T21:17:49.000Z",
"PublicIpAddress": "52.62.35.146",
"PrivateIpAddress": "172.31.12.208",
...
}
我不知道我该怎么做:
runInstancesRequest.withImageId("ami-53170b32")
.withInstanceType("t2.micro")
.withMinCount(1)
.withMaxCount(1)
.withKeyName("mac")
.withSecurityGroupIds("sg-49025d2d");
RunInstancesResult runInstancesResult =
amazonEC2Client.runInstances(runInstancesRequest);
到目前为止一切正常。现在我想从最近启动的实例中获取 Public IP 地址。我该怎么做?
我试过了:
runInstancesResult.getReservation().getInstances().get(0).getPublicIpAddress()
但 IP 始终为空。
启动实例时,它会进入 Pending
状态并且还没有 Public IP 地址。您需要稍等片刻才能使用。
几秒钟后,使用最初返回的实例 ID 调用 DescribeInstances
,然后提取 PublicIpAddress
。
这是从 AWS Command-Line Interface (CLI):
进行的转储$ aws ec2 run-instances --image-id ami-1500742f ...
{
"OwnerId": "123456789012",
"ReservationId": "r-0d8cc4a12a94faba7",
"Groups": [],
"Instances": [
{
"Monitoring": {
"State": "disabled"
},
"PublicDnsName": "",
"KernelId": "aki-c362fff9",
"State": {
"Code": 0,
"Name": "pending"
},
"EbsOptimized": false,
"LaunchTime": "2016-01-22T21:17:49.000Z",
"PrivateIpAddress": "172.31.12.208",
"ProductCodes": [],
"VpcId": "vpc-7d087014",
"StateTransitionReason": "",
"InstanceId": "i-0afe19e0d061b95b5",
...
}
$ aws ec2 describe-instances --instance-ids i-0afe19e0d061b95b5
{
"Reservations": [
{
"OwnerId": "123456789012",
"ReservationId": "r-0d8cc4a12a94faba7",
"Groups": [],
"Instances": [
{
"Monitoring": {
"State": "disabled"
},
"PublicDnsName": "ec2-52-62-35-146.ap-southeast-2.compute.amazonaws.com",
"RootDeviceType": "ebs",
"State": {
"Code": 16,
"Name": "running"
},
"EbsOptimized": false,
"LaunchTime": "2016-01-22T21:17:49.000Z",
"PublicIpAddress": "52.62.35.146",
"PrivateIpAddress": "172.31.12.208",
...
}