如何在EC2启动实例请求中获取公共IP地址? [英] How to obtain the Public IP Address on EC2 launch instance request?
本文介绍了如何在EC2启动实例请求中获取公共IP地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不知道该怎么做:
runInstancesRequest.withImageId("ami-53170b32")
.withInstanceType("t2.micro")
.withMinCount(1)
.withMaxCount(1)
.withKeyName("mac")
.withSecurityGroupIds("sg-49025d2d");
RunInstancesResult runInstancesResult =
amazonEC2Client.runInstances(runInstancesRequest);
到目前为止,一切正常.现在,我想从最近启动的实例中获取公共IP地址.我该怎么办?
So far everything works fine. Now I want to get the Public IP Address from the recently started instance. How can I do that?
我尝试过:
runInstancesResult.getReservation().getInstances().get(0).getPublicIpAddress()
但IP始终为空.
推荐答案
启动实例时,它进入Pending
状态,并且尚没有公共IP地址.您将需要稍等片刻才能使用它.
When an instance is launched, it enters the Pending
state and does not yet have a Public IP address. You will need to wait a little bit for it to be available.
几秒钟后,使用最初返回的实例ID调用DescribeInstances
,然后提取PublicIpAddress
.
After a few seconds, call DescribeInstances
with the Instance ID originally returned, then extract the PublicIpAddress
.
这是 AWS命令行界面(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",
...
}
这篇关于如何在EC2启动实例请求中获取公共IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文