我试图实现的主要目标是在 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 密钥。