Amazon EC2 Tips (8) AWS SDK for PHPのテスト<その2>

Embarcadero RadPHP XE2で AWS SDK for PHPを試してみた。

作成したアプリは、「EC2インスタンスのスタート・ストップ」および、「IPアドレスの割り当て」しかできない、
きわめてシンプルな内容。

画面イメージは以下の通り。
スタートボタンで開始、ストップボタンで停止、画面の下側でインスタンスの状態が確認できるようにした。

しかし、RadPHPはWinアプリのように画面にボタンなどをたくさん貼り付ける場合は、かなり便利かも...

■AWS SDK for PHPを使用する際のポイント

・AWS-SDKのライブラリーは、RadPHPのプロジェクトを保存したフォルダに配置し、sdk.class.phpを呼び出す際に
下記の様に指定した。

require_once("./aws-sdk/sdk.class.php");

・配布ファイルを保存するフォルダにも、AWS-SDKのライブラリーをコピーした。

その他に、実行ファイルをサーバーに転送し、実行してみて気づいたんですが、使用できるPHPに条件があったようです。

必須条件は、バージョン5.2以上で、cURLエクステンションが使用できることの様です。

■イベント処理のソース

同じ処理を何度も使い、ダラダラしたソースですが、テストなのでご勘弁...

◆フォームオープン処理 (インスタンスの状況を画面に表示する)

function mainCreate($sender, $params)
{
require_once("./aws-sdk/sdk.class.php");
define('instanceId','<インスタンスID>');
define('ipAddress','<公開IPアドレス>');

$ec2 = new AmazonEC2();
$ec2->set_region(AmazonEC2::REGION_APAC_NE1);
$options = array('InstanceId.1' => instanceId);

//インスタンス情報取得

$res = $ec2->describe_instances($options);
if($res->isOK()){
$info= $res->body->reservationSet->item->instancesSet->item;
$status= $info->instanceState->name;
$serverName= $info->tagSet->item->value;
$ipAddr= $info->ipAddress;

$this->Label_instanceid->Caption = instanceId;
$this->Label_servername->Caption = $serverName;
$this->Label_status->Caption = $status;
$this->Label_ipaddress->Caption = "none";
if($ipAddr){$this->Label_ipaddress->Caption = $ipAddr;}
}else{
$this->Label_instanceid->Caption = instanceId;
$this->Label_servername->Caption = "not found";
$this->Label_status->Caption = "";
$this->Label_ipaddress->Caption = "";
}
}

◆UPDATEボタン処理 (インスタンスの状況を画面に表示する)

function Button_statusClick($sender, $params)
{
require_once("./aws-sdk/sdk.class.php");
$ec2 = new AmazonEC2();
$ec2->set_region(AmazonEC2::REGION_APAC_NE1);
$options = array('InstanceId.1' => instanceId);

//インスタンス情報取得

$res = $ec2->describe_instances($options);
if($res->isOK()){
$info= $res->body->reservationSet->item->instancesSet->item;
$status= $info->instanceState->name;
$serverName= $info->tagSet->item->value;
$ipAddr= $info->ipAddress;

$this->Label_instanceid->Caption = instanceId;
$this->Label_servername->Caption = $serverName;
$this->Label_status->Caption = $status;
$this->Label_ipaddress->Caption = "none";
if($ipAddr){$this->Label_ipaddress->Caption = $ipAddr;}
}else{
$this->Label_instanceid->Caption = instanceId;
$this->Label_servername->Caption = "not found";
$this->Label_status->Caption = "";
$this->Label_ipaddress->Caption = "";
}
}

◆スタートボタン処理 (インスタンスのスタートとIPアドレスの割り当て)

function Button_startClick($sender, $params)
{
require_once("./aws-sdk/sdk.class.php");
$ec2 = new AmazonEC2();
$ec2->set_region(AmazonEC2::REGION_APAC_NE1);

//インスタンス・スタート

$res = $ec2->start_instances(instanceId);
if($res->isOK()){
$options = array('InstanceId.1' => instanceId);
//インスタンス情報取得

$res = $ec2->describe_instances($options);
$info= $res->body->reservationSet->item->instancesSet->item;
$status= $info->instanceState->name;
$serverName= $info->tagSet->item->value;
$ipAddr= $info->ipAddress;

$this->Label_instanceid->Caption = instanceId;
$this->Label_servername->Caption = $serverName;
$this->Label_status->Caption = $status;
$this->Label_ipaddress->Caption = "none";
if($ipAddr){$this->Label_ipaddress->Caption = $ipAddr;}

//5秒停止(ステータスが変わるのを待つ方法を試したが、待ちきれないの
//で、アバウトに停止させてます)
sleep(5);

//IPアドレス割り当て

$res = $ec2->associate_address(instanceId,ipAddress);
if($res->isOK()){
$res = $ec2->describe_instances($options);
$info= $res->body->reservationSet->item->instancesSet->item;
$ipAddr= $info->ipAddress;
$this->Label_ipaddress->Caption = "none";
if($ipAddr){$this->Label_ipaddress->Caption = $ipAddr;}
} else{
$this->Label_ipaddress->Caption = "NG";
}

}else{
$this->Label_instanceid->Caption = instanceId;
$this->Label_servername->Caption = "not start";
$this->Label_status->Caption = "";
$this->Label_ipaddress->Caption = "";
}

}

◆ストップボタン処理 (インスタンスの停止)

function Button_stopClick($sender, $params)
{
require_once("./aws-sdk/sdk.class.php");
$ec2 = new AmazonEC2();
$ec2->set_region(AmazonEC2::REGION_APAC_NE1);

//インスタンス・ストップ

$res = $ec2->stop_instances(instanceId);
if($res->isOK()){
$options = array('InstanceId.1' => instanceId);

//インスタンス情報取得

$res = $ec2->describe_instances($options);
$info= $res->body->reservationSet->item->instancesSet->item;
$status= $info->instanceState->name;
$serverName= $info->tagSet->item->value;
$ipAddr= $info->ipAddress;

$this->Label_instanceid->Caption = instanceId;
$this->Label_servername->Caption = $serverName;
$this->Label_status->Caption = $status;
$this->Label_ipaddress->Caption = "none";
if($ipAddr){$this->Label_ipaddress->Caption = $ipAddr;}

}else{
$this->Label_instanceid->Caption = instanceId;
$this->Label_servername->Caption = "not start";
$this->Label_status->Caption = "";
$this->Label_ipaddress->Caption = "";
}
}

Amazon EC2 Tips (8) AWS SDK for PHPのテスト<その2>” への2件のフィードバック

  1. RadPHP XE2 を使いこなされているようですごいですね。
    RadPHP XE2 の簡単な入門書的なサイト、もしくは、書籍をご存知ではないでしょうか?
    購入したのはいいのですが、使い方がいまいちよく分からず・・・;;

ohishi へ返信する コメントをキャンセル

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です