Amazon AWS,使用PHP SDK 2为ec2实例创建一个名称标签


Amazon AWS, Creating a name tag for ec2 instance using PHP SDK 2

我使用的是最新版本的php SDK。下面是为现有实例分配名称标签的代码片段:

try {
    $result = $ec2->createTags(array(
        'Resources' => array($instanceid),
        'Tags' => array(
            'Name' => 'PWC_cwc'),
    )); 
} catch (Exception $e) {
    die("Failed to create tag name: ".$e."<br>");
}
输出:

在/Users/harry/Documents/workspace/BigData/vendor/Guzzle/Guzzle/Guzzle/Service/Command/AbstractCommand.php:394 Stack trace: #0

创建标签名称:异常'Guzzle'Service' exception 'ValidationException',消息'Validation errors: [Tags][name][tag]必须是object类型'

我猜我传递参数的方式有问题,但只是想不出正确的方法来做这个

createTags方法的API链接在这里:http://docs.aws.amazon.com/aws-sdk-php-2/latest/class-Aws.Ec2.Ec2Client.html#_createTags

您必须指定每个标签的'Key'和'Value'。

$args = array(
    'DryRun' => False,
    'Resources' => array($resource),
    'Tags' => array(
        array(
        'Key' => 'firstkey',
        'Value' => $firstkeyvalue),
        array(
        'Key' => 'secondkey',
        'Value' => $secondkeyvalue),
        array(
        'Key' => 'thirdkey',
        'Value' => $thirdkeyvalue)
        ));
$response = $ec2->createTags($args);

试试这个:

$result = $ec2->createTags(array(
    'Resources' => array($instanceid),
    'Tags' => array(
        'Tag' => array(
           'Key' => '<key>',
           'Value' => '<value>'
       )
    )
));

你需要在'Tags'数组中有一个'Tag'数组