我試圖實現的主要目標是遍歷我在PHP中運行的EC2實例。AWS PHP SDK中的輸出不同於AWSCLI
這真的很容易得到使用bash腳本的數據,如下圖所示:
bash腳本:
#!/bin/bash
export AWS_ACCESS_KEY_ID="AKIDEXAMPLE"
export AWS_SECRET_ACCESS_KEY="wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY"
aws ec2 describe-instances --region="eu-west-1" --filter "Name=instance-state-name,Values=running"
擊輸出:
{
"Reservations": [
{
"OwnerId": "58728357357",
"ReservationId": "r-0e0283649826935",
"Instances": [
{
"SecurityGroups": [
{
"GroupId": "sg-2fe333148",
"GroupName": "WEB"
}
],
"PublicDnsName": "ec2-53-13-121-72.eu-west-1.compute.amazonaws.com",
"Architecture": "x86_64",
"LaunchTime": "2016-07-11T08:28:23.000Z",
"RootDeviceName": "/dev/sda1",
"BlockDeviceMappings": [
{
"Ebs": {
// ...
}
]
}
然而,當我嘗試下面的例子,使用相同的鍵,我會看到似乎是不可用的對象 - 或者至少該對象看起來像表示一個空的數據結構。
PHP文件:
<?php
require __DIR__ . "/vendor/autoload.php";
$settings = [
"version" => "latest",
"region" => "eu-west-1",
"credentials" => [
"key" => "AKIDEXAMPLE",
"secret" => "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY",
],
];
$client = new \Aws\Ec2\Ec2Client($settings);
$result = $client->describeInstances([
"Filters" => [
[
"Name" => "instance-state-name",
"Value" => "running",
]
],
]);
var_dump($result);
PHP輸出:
我究竟在我的意思是這個AWS \結果呢?
class Aws\Result#82 (1) {
private $data =>
array(2) {
'Reservations' =>
array(0) {
}
'@metadata' =>
array(4) {
'statusCode' =>
int(200)
'effectiveUri' =>
string(35) "https://ec2.eu-west-1.amazonaws.com"
'headers' =>
array(5) {
...
}
'transferStats' =>
array(1) {
...
}
}
}
}
我錯過了PHP配置中的某些東西嗎?請有人能幫助我指出正確的方向嗎?
P.S.我在上面的例子中掩蓋了API密鑰。
首先從問題中刪除密鑰ID和訪問關鍵,如果他們是在用前一個挑選他們。 – error2007s
你還希望PHP代碼應該給你什麼輸出? – error2007s
@ error2007s我希望至少能看到關於我的實例的_some_數據,就像我對bash示例所做的那樣。迭代'$ results'不會給我任何東西 - 第一個元素是一個空數組。示例顯示'$ results [「Reservations」]'來保存實例數據,但是我的是空的。 – Greg