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
2
3
4
5
6
7
<?php

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

// ...

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
$ec2 = \Aws\Ec2\Ec2Client::factory(['profile' => 'default', 'region' => 'eu-central-1']);

$response = $ec2->describeInstances([
    'Filters' => [
        ['Name' => 'tag:purpose', 'Values' => ['mhde-prod']],
        ['Name' => 'instance-state-name', 'Values' => ['running']]
    ]
]);

$application = new \TYPO3\Surf\Application\TYPO3\Flow('martin-helmich.de');

foreach ($response['Reservations'] as $reservation) {
    foreach ($reservation['Instances'] as $instance) {
        $node = new \TYPO3\Surf\Domain\Model\Node($instance['InstanceId']);
        $node->setHostname($instance['PublicDnsName']);
        $node->setOption('username', 'neos');
        $application->addNode($node);
    }
}

// ...

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

Comments