Hallo! Dieser Artikel steht auch auf Deutsch zur Verfügung!

Surf In The Cloud: TYPO3 Surf-Deployments nach EC2

After a while, manually deploying my tiny site got a little tedious. As a solution, I decided to automate this deployment using TYPO3 Surf. Since I’m also working with Surf on the job, that task was not too difficult for me. However, the first version of my deployment script did not really “feel” good (you know that feeling when you have built something that works well, but just doesn’t “feel” good, don’t you?):

1 <?php
3 $node = new \TYPO3\Surf\Domain\Model\Node("i-54932495");
4 $node->setHostname("ec2-52-28-61-14.eu-central-1.compute.amazonaws.com");
5 $node->setOption("username", "neos");
7 // ...

What would happen, should I decide to delete my teeny-tiny-micro EC2 instance? Or if I decided to operate my site on two (teeny-tiny) micro instances? It appears a lot more elegant to simply retrieve the information on the target nodes directly from the EC2 API. For this, you can use the AWS SDK for php, which can be easily installed using Composer:

$ composer require aws/aws-sdk-php

Here it pays off that in TYPO3 Surf, deployment definitions are simply executable PHP files. That way, you can simply query the AWS API for all instances (optionally filtered by status and a certain tag) and add as a new node to the deployment:

 1 <?php
 2 $ec2 = \Aws\Ec2\Ec2Client::factory(['profile' => 'default', 'region' => 'eu-central-1']);
 4 $response = $ec2->describeInstances([
 5     'Filters' => [
 6         ['Name' => 'tag:purpose', 'Values' => ['mhde-prod']],
 7         ['Name' => 'instance-state-name', 'Values' => ['running']]
 8     ]
 9 ]);
11 $application = new \TYPO3\Surf\Application\TYPO3\Flow('martin-helmich.de');
13 foreach ($response['Reservations'] as $reservation) {
14     foreach ($reservation['Instances'] as $instance) {
15         $node = new \TYPO3\Surf\Domain\Model\Node($instance['InstanceId']);
16         $node->setHostname($instance['PublicDnsName']);
17         $node->setOption('username', 'neos');
18         $application->addNode($node);
19     }
20 }
22 // ...

And done! Now I never again have to configure new servers in the deployment configuration.