0

I am writing below code to get public dns of newly created instance however i am getting null

DescribeInstancesResult describeInstancesRequest = amazonEC2Client.describeInstances(new DescribeInstancesRequest());
List<Reservation> reservations = describeInstancesRequest.getReservations();

for (Reservation reservation : reservations) {
    for (Instance instance1 : reservation.getInstances()) {

        dns = instance1.getPublicDnsName();
        if(!(dns.equals("")))
        {

            break;
        }


    }
}
System.out.println("value:" + dns);
Frederic Henri
  • 51,761
  • 10
  • 113
  • 139
aquaabhi
  • 3
  • 2
  • Is the instance created in VPC without a public IP? – datasage Oct 04 '15 at 19:36
  • yes, but after the instance u=is created, a public dns has been assigned to it – aquaabhi Oct 04 '15 at 19:38
  • Does this mean you are assigning an elastic IP to the instance after its been created? – datasage Oct 04 '15 at 19:43
  • I have created the instance using RunInstancesRequest() – aquaabhi Oct 04 '15 at 19:51
  • 1
    Do the instances have public DNS? If the instances are created in a VPC and a public IP is not assigned on create, then the value of null is correct. Instances created in classic always get a public IP, but instances created in VPC may not. – datasage Oct 04 '15 at 19:52
  • @datasage I believe that by default, DNS hostnames are enabled only for default VPCs and VPCs that you create using the VPC wizard in the VPC console (both public and private). only if you provide your VPC and you do not support dns (enableDnsSupport and enableDnsHostnames set to false) you will not have the dns. I doubt the OP did all that in this case ! – Frederic Henri Oct 05 '15 at 10:23

1 Answers1

2

For all non running instances, the dns name is null. You can check documentation for Instance class

This name is not available until the instance enters the running state

In case you run with a VPC

When you launch an instance into a VPC, Amazon provides the instance with public and private DNS hostnames only if DNS hostnames are enabled for the VPC. By default, DNS hostnames are enabled only for default VPCs and VPCs that you create using the VPC wizard in the VPC console.

Amazon supports the following VPC attributes to control DNS support. Be sure to set both attributes to true if you want your instances to have public DNS hostnames that are accessible from the Internet.

  • enableDnsHostnames
  • enableDnsSupport
Frederic Henri
  • 51,761
  • 10
  • 113
  • 139