1

It's relatively easy to create a new instance using PHP by using the runInstances() method.

$instance = $ec2->runInstances(array(
    'ImageId'        => AMI_ID,      // AMI ID
    'InstanceType'   => AMI_TYPE,    // m1.medium etc.
    'MinCount'       => 1,           // Minimum to create
    'MaxCount'       => 1,           // Maximum to create
    'SecurityGroups' => SEC_NAME,    // Security Group Name
    'KeyName'        => KEY_NAME     // Key Pair to use
))->toArray();                       // Get back our data in an array

However, the point of the API is to be able to do everything you can do with the front-end on the amazon website, in a tenth of the time and with your own code.

With that in mind, I need to do the following:

AWS Request Instances Wizard - Root Volume

On the front-end, I can change the Volume Size to, say, 40GB.

How can I ask for a 40GB Volume Size when creating a new Instance with PHP? It could even be run after the instance is created, as long as it's automatic - I should be able to do this programatically.

How can I achieve what I require using the AWS SDK for PHP 2?

4

2 回答 2

2

看起来你在正确的轨道上。在 的文档runInstances(),有一个参数BlockDeviceMappings包含另一个参数Ebs,该参数包含您要查找的参数VolumeSize。这是未经测试的代码。

$instance = $ec2->runInstances(array(
'ImageId'                => AMI_ID,      // AMI ID
'InstanceType'           => AMI_TYPE,    // m1.medium etc.
'MinCount'               => 1,           // Minimum to create
'MaxCount'               => 1,           // Maximum to create
'SecurityGroups'         => SEC_NAME,    // Security Group Name
'KeyName'                => KEY_NAME,    // Key Pair to use
'BlockDeviceMappings' => array(          // How block devices are mapped to instance
   array(
     'Ebs' => array(                     // EBS Volume Info
        array(
            'VolumeSize' => 40           // Volume Size
        )
    )
)
)
))->toArray();                           // Get back our data in an array
于 2013-09-17T14:14:38.740 回答
1

这与使用命令行工具的方式没有什么不同。首先,您需要确定将要启动的 AMI 的快照 ID。您可以通过查询 AMI 的属性来做到这一点。返回的值之一是快照 ID。然后,您通过指定将使用的快照 ID 和大小来使用该 ID 运行实例。

这里的文档:http: //docs.aws.amazon.com/AWSSDKforPHP/latest/index.html#m=AmazonEC2/run_instances

解释这需要与 BlockDeviceMapping 数组一起传递。然后在该阵列中,您需要使用“Ebs”并指定 SnapshotId 和 VolumeSize。

如果您在 runinstance 调用中传递所有这些参数,它将以您想要的大小启动。

于 2013-09-17T14:20:10.447 回答