3
votes

I want to create an ec2 instance, and use the "Waiter" functionality to wait until the Instance has a Public DNS Name - is this possible?

Here is my code:

$ec2 = new Aws\Ec2\Ec2Client([credentials]);

$result = $ec2->runInstances(array(
                'DryRun' => false,
                // ImageId is required
                'ImageId' => '[removed]',
                // MinCount is required
                'MinCount' => 1,
                // MaxCount is required
                'MaxCount' => 1,
                'KeyName' => '[removed]',
                'SecurityGroupIds' => array('[removed]'),

                'InstanceType' => 'r3.4xlarge',

                'Placement' => array(
                    'AvailabilityZone' => 'us-east-1a',

                ),
                'EbsOptimized' => false,
                'Monitoring' => array(
                    // Enabled is required
                    'Enabled' => true,
                ),

            ));

$arr = $result->toArray();

$instanceId = $arr['Instances'][0]['InstanceId'];

echo 'created instance '.$instanceId."\n";

$ec2->waitUntil('InstanceRunning', ['InstanceIds' => array($instanceId)]);          

$result = $ec2->describeInstances(array(
                'InstanceIds' => array($instanceId),
            ));
$dnsName = current($result->getPath('Reservations/*/Instances/*/PublicDnsName'));

The instance is created just fine but the last line fails, because the instance does not have a public dns name because an instance is considered 'running' before it has a dns name. Is there a waiter I can use to wait until it has a dns name?

Here are the waiters: http://docs.aws.amazon.com/aws-sdk-php/v3/api/api-ec2-2015-04-15.html#waiters

2
Have you tried sleep() ing?Kevin_Kinsey
I know I can sleep, or loop through until the dns name is available, just wanted to use the cool waiter function.James Bowler
Honestly, the waiters are just an abstraction over a loop with a call to sleep(), so that would be a valid approach. A lot of the waiters in the SDK came from customer pull requests against one or more of the AWS SDKs, so please open a PR if you come up with something!giaour
Interesting - thanks for the info.James Bowler

2 Answers

0
votes

There is no waiter function to get the public DNS name. 2 options I can think of:

1.) Use sleep or some sort of while loop / sleep method until you can get the PublicDnsName. This is kind of hacky and blocks your thread.

2.) A more elegant way is to make this whole thing asynchronous by using Queuing, or a PHP implementation of asynchronous.

0
votes

try $dnsName = $result['Reservations'][0]['Instances'][0]['PublicDnsName']

but if u want to ssh with dns-name

Blockquote

you must take the key pair *.pem file and ping the hostname using the ssh shell command in a loop until it connects. Once it's connectable, you return true. Just because the instance is running does not mean you can SSH into it. There is nothing in the API that allows you to know whether or not you can SSH into. The way to do this is with a custom waiter. The process of creating a custom waiter is described in the Custom Waiters section of the user guide. @skyzyx are you interested implementing this?1

Blockquote