簡體   English   中英

通過適用於PHP的AWS開發工具包啟動EC2實例時無法添加標簽(無CLI)

[英]Unable to add tags when launching EC2 instance via AWS SDK for PHP (no CLI)

我能夠使用適用於PHP的AWS開發工具包成功啟動新實例。 但是,我無法添加標簽(“名稱” =“新實例”)。 我嘗試了幾種方法,以下似乎是最合理的。 但是,刷新EC2儀表板后,可以看到已創建新實例,但是name標記仍然為空。

        $ec2Client = new Ec2Client([
            'region' => 'xx-xxxx-1',
            'version' => 'latest',
            'profile' => 'default'
        ]);
        // Launch an instance with the key pair and security group
        $result = $ec2Client->runInstances(array(
            'ImageId'           => 'ami-xxxxxxxx',
            'MinCount'          => 1,
            'MaxCount'          => 1,
            'InstanceType'      => 't2.large',
            'KeyName'           => 'xxxxxx',
            'SubnetId'          => 'subnet-xxxxxxxx',
            'VpcId'             => 'vpc-xxxxxxxx',
            'Tags'              => array(
                                        'Key' => 'Name',
                                        'Value' => 'New Instance',
                                    ),
            'SecurityGroups'[1] => 'sg-xxxxxxxx',
        ));

我不知道您使用的是哪個版本的SDK,但是從AWS v2 SDK文檔來看,沒有Tags鍵。

嘗試使用其他功能createTags 請注意, Tags是雙數組,與您的示例不同。

$ec2Client->createTags([
    'Resources' => [$result['Instances'][0]['InstanceId']],
    'Tags' => [
        ['Key' => '', 'Value' => ''],
        ['Key' => '', 'Value' => ''],
    ]
]);

感謝@Justinas指出使用版本2。但是,我使用的是版本3 SDK,並在https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-ec2中找到了答案-2016-11-15.html#runinstances

經過測試和工作后,可以在RunInstances操作下的TagSpecifications下完成Tag,如下所示:

'TagSpecifications' => [
    [
        'ResourceType' => 'instance',
        'Tags' => [
            [
                'Key' => 'Name',
                'Value' => 'New Instance Name',
            ],
        ],
    ],
],

有關TagSpecifications數組結構的更多信息,請參見: https ://docs.aws.amazon.com/aws-sdk-php/v3/api/api-ec2-2016-11-15.html#shape-tagspecification和此處的標簽結構: https : //docs.aws.amazon.com/aws-sdk-php/v3/api/api-ec2-2016-11-15.html#shape-tag

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM