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);
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.