LoginSignup
0
1

More than 5 years have passed since last update.

AWS SDK for PHP バージョン 3動かしてみた

Posted at

必要環境の確認
SDKのインストール
クレデンシャルの作成
は割愛

デプロイツールでよくあるEC2のRoleを元にデプロイ先のIPを取得する感じです。

require_once(realpath(__DIR__ ."/../vendor/autoload.php")); // 別の階層で利用するので

use Aws\Ec2\Ec2Client;

$keys = array(
        'key'   => '**********************',
        'secret' => '*********************************',
    );
$config = array(
    'credentials' => $keys,
    'region' => 'ap-northeast-1',
    'version' => 'latest',
);

$ec2 = new Ec2Client($config);
$instances = $ec2->describeInstances(array(
    'Filters' => array(
        array(
            'Name' => 'instance-state-name','Values' => array('running'),
        ),
    ),
));

$role = 'app'; // 探したいRole
$hosts = array();
$reservations = $instances['Reservations'];
foreach ($reservations as $reservation) {
    $instances = $reservation['Instances'];
    foreach ($instances as $instance) {
        foreach ($instance['Tags'] as $tag) {
            if ($tag['Key'] === 'Role') {
                if ($tag['Value'] === $role) {
                    $hosts[] = $instance['PrivateIpAddress'];
                    break;
                }
            }
        }
    }
}

var_dump($hosts);
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1