1

我试图实现的主要目标是在 PHP 中迭代我正在运行的 EC2 实例。

使用 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\Result 做什么?

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 配置中遗漏了什么吗?请有人能帮我指出正确的方向吗?

PS 我在上面的例子中屏蔽了 API 密钥。

4

1 回答 1

0

EC2::DescribeInstances 接受一个过滤器数组,每个过滤器都有一个字符串Name和一个字符串数组Values。在您的 CLI 示例中,您为 提供了一些东西Values,而在您的 PHP 示例中,您提供了 a Value。SDK 无法识别此字段,将被忽略。有关更多信息,请参阅SDK API 文档

您的 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",
            "Values" => ["running"],
        ]
    ],
]);

var_dump($result);
于 2016-07-12T17:20:18.973 回答